diff --git a/app/src/Application/Bootloader/AppBootloader.php b/app/src/Application/Bootloader/AppBootloader.php index 8ef758d..aa58f50 100644 --- a/app/src/Application/Bootloader/AppBootloader.php +++ b/app/src/Application/Bootloader/AppBootloader.php @@ -9,8 +9,10 @@ use App\Application\HTTP\Interceptor\StringToIntParametersInterceptor; use App\Application\HTTP\Interceptor\UuidParametersConverterInterceptor; use App\Application\Ide\UrlTemplate; +use App\Interfaces\Console\RegisterModulesCommand; use Spiral\Boot\EnvironmentInterface; use Spiral\Bootloader\DomainBootloader; +use Spiral\Console\Bootloader\ConsoleBootloader; use Spiral\Core\CoreInterface; final class AppBootloader extends DomainBootloader @@ -46,4 +48,13 @@ protected static function defineInterceptors(): array JsonResourceInterceptor::class, ]; } + + public function init(ConsoleBootloader $console): void + { + $console->addSequence( + name: RegisterModulesCommand::SEQUENCE, + sequence: 'database:check-connection', + header: 'Check database connection', + ); + } } diff --git a/app/src/Interfaces/Console/CheckDatabaseConnectionCommand.php b/app/src/Interfaces/Console/CheckDatabaseConnectionCommand.php new file mode 100644 index 0000000..d152bb4 --- /dev/null +++ b/app/src/Interfaces/Console/CheckDatabaseConnectionCommand.php @@ -0,0 +1,38 @@ +getDriver()->connect(); + $this->info('Database connection is OK'); + break; + } catch (\Throwable $e) { + $tries++; + $this->error($e->getMessage()); + $delay = (int) ($multiplier * $tries); + $this->error('Cannot connect to the database. Retrying in ' . $delay . ' second...'); + \sleep($delay); + } + } while (true); + + return self::SUCCESS; + } +}