Skip to content

[13.x] Allow jobs to react to worker signals#59833

Merged
taylorotwell merged 4 commits intolaravel:13.xfrom
jackbayliss:13.x-ability-to-signal-to-job
Apr 24, 2026
Merged

[13.x] Allow jobs to react to worker signals#59833
taylorotwell merged 4 commits intolaravel:13.xfrom
jackbayliss:13.x-ability-to-signal-to-job

Conversation

@jackbayliss
Copy link
Copy Markdown
Contributor

@jackbayliss jackbayliss commented Apr 23, 2026

When a worker retrieves a signal like SIGTERM, you can have a set amount of time before it gets killed.

In managed infrastructure, this is usually 10 seconds to 1 minute. (I can set it)

But, right now, the Job is oblivious its going be killed... so the worker knows, but the job doesnt.

So, I thought it would be nice to be able to react from inside the job, if this does happen.

See a basic example video here (audio on) (I changed the method name since):

Details
Screen.Recording.2026-04-23.at.16.36.21.mov

TLDR is you implement the Interruptible interface on the job, and put your logic in the interrupted method which looks something like:

Details
namespace App\Jobs;

use Illuminate\Contracts\Queue\Interruptible;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class SignalJob implements ShouldQueue, Interruptible // MUST IMPLEMENT THIS
{
    use Queueable;

    protected bool $stop = false;

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        while(!$this->stop) {
            sleep(1);
            echo "I am running doing something kinda important...\n";
        }
    }

    public function interrupted(int $signal): void // MUST HAVE A METHOD WITH THE LOGIC
    {
        $this->stop = true;
        echo "I have been told to stop!\n";
        echo $signal;
    }
}

Granted, this may not be something you want to maintain, or you may see a nicer way of passing the signal in, but I thought I'd give it a go! Could probably use some love, but no drama if you dont dig it.

This may need some additional thought and poking as I'm not super 100% knowledge on the queue logic.

@github-actions
Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@rforced
Copy link
Copy Markdown

rforced commented Apr 23, 2026

I just want to say thank you for all the queuing improvements you've made over the past months, we heavily utilize queues and these changes are helping a lot 😄

@jackbayliss
Copy link
Copy Markdown
Contributor Author

@rforced Thanks! I usually just think about stuff I'd want and give it a whirl. We use queues heavily for all sorts, so this felt like a gap in my head 👍🏻

@jackbayliss jackbayliss marked this pull request as ready for review April 23, 2026 18:28
Copy link
Copy Markdown
Contributor

@shaedrich shaedrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome idea! 🚀


interface Interruptible
{
public function interrupted(int $signal): void;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could make this more specific:

Suggested change
public function interrupted(int $signal): void;
/**
* @param SIG* $signal
*/
public function interrupted(int $signal): void;

or

Suggested change
public function interrupted(int $signal): void;
/**
* @param SIGQUIT|SIGTERM|SIGINT $signal
*/
public function interrupted(int $signal): void;

@weshooper
Copy link
Copy Markdown
Contributor

Love it, thanks Jack 🚀

set after debounced

oops

Update Worker.php

rename to make sense
@jackbayliss jackbayliss force-pushed the 13.x-ability-to-signal-to-job branch from ca4e577 to 97a0090 Compare April 24, 2026 09:02
@taylorotwell taylorotwell merged commit 86cb6af into laravel:13.x Apr 24, 2026
52 checks passed
@taylorotwell
Copy link
Copy Markdown
Member

Nice one!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants