forked from systemsdk/docker-nginx-php-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbWaitDatabase.php
55 lines (44 loc) · 1.19 KB
/
DbWaitDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use DB;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
class DbWaitDatabase extends Command
{
/**
* Wait sleep time for db connection in seconds
*/
private const WAIT_SLEEP_TIME = 2;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:wait';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Waits for database availability.';
/**
* Execute the console command.
*/
public function handle(DB $database): int
{
for ($i = 0; $i < 60; $i += self::WAIT_SLEEP_TIME) {
try {
$database::select('SHOW TABLES');
$this->info('Connection to the database is ok!');
return 0;
} catch (QueryException $exception) {
$this->comment('Trying to connect to the database seconds:' . $i);
sleep(self::WAIT_SLEEP_TIME);
continue;
}
}
$this->error('Can not connect to the database');
return 1;
}
}