Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Created July 5, 2023 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadoz/fb4b27755833a35877889ddb0319c7b1 to your computer and use it in GitHub Desktop.
Save ziadoz/fb4b27755833a35877889ddb0319c7b1 to your computer and use it in GitHub Desktop.
Laravel - Keep Database Connection Alive in Artisan Commands
<?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