Created
July 5, 2023 13:58
-
-
Save ziadoz/fb4b27755833a35877889ddb0319c7b1 to your computer and use it in GitHub Desktop.
Laravel - Keep Database Connection Alive in Artisan Commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
class MyCommand extends Command | |
{ | |
protected $signature = 'app:my-command'; | |
public function handle(): int | |
{ | |
// Ensure the MySQL connection stays alive during long running migrations. | |
// @see: https://twitter.com/lyrixx/status/1575127719624544258 | |
$this->trap(SIGALRM, function (): void { | |
DB::statement('SELECT 1'); | |
pcntl_alarm(30); | |
}); | |
pcntl_alarm(30); | |
// DB::statement('RUN SOME LONG OR SLOW SQL...'); | |
return self::SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment