Message:
The maintenance window is stored in the database and varies daily, so I can’t rely on Laravel’s php artisan down command for this use case.
I’ve tried multiple approaches—including a middleware and an event listener—to delay job processing during maintenance. The idea is to release jobs back to the queue if they fall within the active maintenance window. However, in all cases, the jobs end up being marked as failed, which is not the intended behavior.
Here’s the simplified event listener logic I used:|
<?php
declare(strict_types=1);
namespace App\Providers;
use App\Actions\Maintenance\CheckIfMaintenanceIsActive;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
class QueueMaintenanceServiceProvider extends ServiceProvider
{
public function boot(): void
{
Queue::before(function (JobProcessing $event) {
$this->handleMaintenanceDelay($event);
});
}
private function handleMaintenanceDelay(JobProcessing $event): void
{
$job = $event->job;
$payload = $job->payload();
$jobData = unserialize($payload['data']['command']);
if (! $this->shouldProcessJob($jobData, $payload)) {
$job->release(300);
}
}
}
Even though I’m calling $job->release(300), the job is still being marked as failed in Horizon. The goal is to gracefully re-queue the job for later execution once the maintenance window has ended—without flagging it as failed.