Skip to content

Instantly share code, notes, and snippets.

@BenCavens
Created August 2, 2021 08:28
Show Gist options
  • Save BenCavens/810758e74718a981c4cd2d2cf532407e to your computer and use it in GitHub Desktop.
Save BenCavens/810758e74718a981c4cd2d2cf532407e to your computer and use it in GitHub Desktop.
Laravel queue monitoring on shared hosting
<?php
namespace App\Queue;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class DatabaseQueueMonitorCommand extends Command
{
protected $signature = 'queue:db-monitor';
protected $description = 'Check if our database queue is still running';
public function handle()
{
/**
* Because we use a database queue, we check if the jobs table still contains any
* old records. This means that the queue has been stalled.
*/
$records = DB::table('jobs')->where('created_at', '<', Carbon::now()->subMinutes(5)->getTimestamp())->get();
if (! $records->isEmpty()) {
report('Queue jobs table should be emptied by now but it is not! Please check your queue worker.');
$this->warn('Queue jobs table should be emptied by now but it is not! Please check your queue worker.');
return;
}
$this->info('Queue jobs are looking good.');
}
}
<?php
namespace App\Providers;
use App\System\Queue\Console\DatabaseQueueMonitorCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Queue\Events\JobFailed;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
class QueueServiceProvider extends ServiceProvider
{
public function boot()
{
Queue::failing(function (JobFailed $event) {
report($event);
});
if ($this->app->runningInConsole()) {
$this->commands([
DatabaseQueueMonitorCommand::class,
]);
}
$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->command('queue:work --stop-when-empty')->everyMinute()->withoutOverlapping(10);
$schedule->command('queue:restart')->hourly();
$schedule->command('queue:db-monitor')->everyTenMinutes();
});
}
public function register()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment