DEV Community

Cover image for Laravel Interruptible Jobs: Handle SIGTERM in Queue Jobs
devTalk
devTalk

Posted on Originally published at dev-talk.com

Laravel Interruptible Jobs: Handle SIGTERM in Queue Jobs

Last updated: 29 November 2026. Verified against the laravel/framework source at v13.44.1, with the signal behavior tested live using PHP 8.3 and pcntl; seeHow this was checked.

Every deploy that restarts queue workers has the same problem. Supervisor, systemd, Kubernetes or Horizon sends the worker a SIGTERM, but the job that's halfway through exporting 200,000 rows has no idea. It keeps going until it either finishes or gets killed outright when the stop timeout runs out, leaving a half-written file, a held lock, or a batch you can't tell how far got.

Laravel 13 has a small contract for exactly this: Illuminate\Contracts\Queue\Interruptible. Implement it on a job, and the worker tells that job the moment it receives a shutdown signal, so the job can stop at a safe point.

Which version you need

The contract arrived in Laravel 13.7.0 (April 2026), via pull request #59833, "Allow jobs to react to worker signals." A matching JobInterrupted event followed in 13.31.0 (September 2026, PR #61412). Check your version with php artisan --version. You also need the pcntl PHP extension, because the worker only registers signal handlers when it's loaded:

php -m | grep pcntl
Enter fullscreen mode Exit fullscreen mode

No pcntl, no notification. That's common on Windows and on some minimal PHP builds.

The contract

This is the whole interface, as it ships in the framework:

namespace Illuminate\Contracts\Queue;

interface Interruptible
{
    public function interrupted(int $signal): void;
}
Enter fullscreen mode Exit fullscreen mode

When a queue:work process receives SIGTERM, SIGINT or SIGQUIT, the worker marks itself to quit and then calls interrupted() on the job that's currently running. It's the same object instance that's executing handle(), so a property you set in interrupted() is visible inside handle() right away.

The part that's easy to get wrong

interrupted() is a notification, not a kill switch. It doesn't stop handle(). Looking at the worker source, after the signal the worker lets the current job run to completion and only exits before picking up the next one. If your job ignores the notification, nothing changes: it keeps running exactly as it did before you implemented the interface.

So the pattern is cooperative. Set a flag in interrupted(), and check it in handle() at points where stopping is safe.

A working example: a resumable export

<?php

namespace App\Jobs;

use App\Models\User;
use Illuminate\Contracts\Queue\Interruptible;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class ExportUsers implements ShouldQueue, Interruptible
{
    use Queueable;

    private bool $stopRequested = false;

    public function __construct(public int $exportId) {}

    public function interrupted(int $signal): void
    {
        // Keep this short: just record that we were asked to stop.
        $this->stopRequested = true;
    }

    public function handle(): void
    {
        $cursorKey = "export:{$this->exportId}:last_id";
        $lastId = Cache::get($cursorKey, 0);

        User::where('id', '>', $lastId)
            ->orderBy('id')
            ->chunkById(500, function ($users) use ($cursorKey) {
                foreach ($users as $user) {
                    // ... write the row to the export file ...
                }

                // Save progress after every completed chunk.
                Cache::put($cursorKey, $users->last()->id, now()->addDay());

                if ($this->stopRequested) {
                    Log::info("Export {$this->exportId} paused for shutdown");

                    return false; // stops chunkById()
                }
            });

        if ($this->stopRequested) {
            // Put the job back on the queue; a fresh worker resumes from the cursor.
            $this->release(5);

            return;
        }

        Cache::forget($cursorKey);
    }
}
Enter fullscreen mode Exit fullscreen mode

What each piece is doing:

  • interrupted()only sets a flag. It runs inside a signal handler, in the middle of whatever handle() was doing, so don't do slow work there: no queries, no HTTP calls.
  • The flag is checked between chunks, never mid-row, so the file and the saved cursor always agree.
  • return false inside a chunkById() callback stops the chunking.
  • $this->release(5) puts the job back on the queue with a 5-second delay. Because progress is in the cache, the next worker continues from where this one stopped instead of starting over. Released jobs still count against $tries, so set it high enough for jobs that may be interrupted several times across deploys.

Logging interruptions with the JobInterrupted event

On 13.31 or later, the worker also dispatches Illuminate\Queue\Events\JobInterrupted after notifying an interruptible job. It carries $connectionName, $job (the queue job wrapper) and $signal. That's handy if you want one place that records which jobs deploys are cutting short:

// In AppServiceProvider::boot()
use Illuminate\Queue\Events\JobInterrupted;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;

Event::listen(function (JobInterrupted $event) {
    Log::warning('Queue job interrupted', [
        'job' => $event->job->resolveName(),
        'connection' => $event->connectionName,
        'signal' => $event->signal,
    ]);
});
Enter fullscreen mode Exit fullscreen mode

The event only fires for jobs that implement Interruptible. For worker-level logging regardless of the job, there's also a separate WorkerInterrupted event.

Deployment details that decide whether this works

  • queue:restartdoes not trigger it. php artisan queue:restart works by writing a timestamp to the cache, and workers stop after their current job without receiving any signal. interrupted() only runs when the process actually gets SIGTERM, SIGINT or SIGQUIT: from supervisorctl stop or restart, systemctl stop, a Kubernetes pod shutdown, or Ctrl+C in a terminal.
  • Give the worker time to stop. Process managers send SIGTERM, wait, then send SIGKILL, and nothing runs after SIGKILL. Supervisor's stopwaitsecs, systemd's TimeoutStopSec and Kubernetes' terminationGracePeriodSeconds all need to be longer than the time your job takes to reach its next checkpoint.
  • Check the flag often enough. If your checkpoints are 60 seconds apart and the grace period is 30 seconds, the job gets killed anyway. Smaller chunks give you more places to stop.
  • Make the resume path idempotent. If a job is killed before it saves its cursor, the retry repeats some work. Design the writes so that repeating a chunk is harmless.

update log

  • 29 November 2026: first version, checked against v13.44.1.
  • To recheck next time: whether Laravel adds a built-in helper for checking interruption from inside handle(), and whether queue:restart starts notifying interruptible jobs.

Frequently asked questions

Does implementing Interruptible stop my job immediately?

No. The worker calls interrupted() and then waits for handle() to return before exiting. Your job has to check a flag and return, or call release(), for anything to change.

Why is interrupted() never called when I run queue:restart?

queue:restart doesn't send a signal. It sets a cache value that workers check between jobs. interrupted() only runs when the worker process itself receives SIGTERM, SIGINT or SIGQUIT.

Which Laravel version added the Interruptible interface?

Laravel 13.7.0. The JobInterrupted event was added later, in 13.31.0.

Does it work on Windows?

Not normally. The worker only listens for signals when the pcntl extension is loaded, and pcntl isn't available on Windows. Run workers on Linux, or in Docker or WSL, if you need graceful interruption.

Related Dev Talk articles


Originally published on DEV Talk.

Top comments (0)