gegmar /
cluber
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Console\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command; |
||
| 6 | use Illuminate\Support\Facades\DB; |
||
| 7 | |||
| 8 | class CheckDBConnection extends Command |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * The name and signature of the console command. |
||
| 12 | * |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | protected $signature = 'app:check-db-connection'; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The console command description. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $description = 'Checks if the database is reachable and ready to receive queries.'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new command instance. |
||
| 26 | * |
||
| 27 | * @return void |
||
| 28 | */ |
||
| 29 | public function __construct() |
||
| 30 | { |
||
| 31 | parent::__construct(); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Execute the console command. |
||
| 36 | * |
||
| 37 | * @return mixed |
||
| 38 | */ |
||
| 39 | public function handle() |
||
| 40 | { |
||
| 41 | try { |
||
| 42 | DB::statement('show tables'); |
||
| 43 | $this->info('Database connection available!'); |
||
| 44 | } catch( \Illuminate\Database\QueryException $e) { |
||
| 45 | $this->error('Database connection not available!'); |
||
| 46 | exit(1); // return a non-0-code to be used in shell scripts as a falsy exit state |
||
|
0 ignored issues
–
show
|
|||
| 47 | } |
||
| 48 | } |
||
| 49 | } |
||
| 50 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.