Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheSource's avatar

Gracefully stop the queue-worker in docker.

Hi all,

A crosspost here, since nobody can help me on stackoverflow... I'm running the artisan queue:work command via a docker image. When deploying a new version this docker image needs to be stopped en restarted. Only the SIGTERM signal doesn't seem to be picked up by the worker, as docker returns a 137 exit code instead of 0. Can anybody help me with this?

https://stackoverflow.com/questions/62446226/how-to-stop-the-laravel-queueworker-gracefully-running-as-a-docker-image

0 likes
1 reply
Skaro's avatar

I know that it's too late, but maby anybody find this useful.

For laravel 9.25

Extend WorkCommand:

<?php

namespace App\Console\Commands;

use Illuminate\Queue\Console\WorkCommand;
use Symfony\Component\Console\Command\SignalableCommandInterface;

class QueueWorkCommand extends WorkCommand implements SignalableCommandInterface
{

    public function getSubscribedSignals(): array
    {
        return [SIGTERM];
    }

    public function handleSignal(int $signal): void
    {
        if (SIGTERM === $signal) {
            $this->call('queue:restart');
        }
    }
}

Inside \App\Providers\AppServiceProvider::boot() method bind or singleton our QueueWorkCommand:

$this->app->bind(QueueWorkCommand::class, function ($app) {
    return new QueueWorkCommand($app['queue.worker'], $app['cache.store']);
});

And of course in Dockerfile don't forget install pcntl extension:

RUN docker-php-ext-configure pcntl --enable-pcntl && docker-php-ext-install pcntl

CMD ["php", "artisan", "queue:work"]

And then when you stop your container, it handle SIGTERM signal and run queue:restart command. This command will instuct all queue workers to gracefully exit after they finish processing their current job.

About queue:restart command https://laravel.com/docs/9.x/queues#queue-workers-and-deployment

About how to handle SIGTERM inside docker container https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/

If your container can't handle SIGTERM signal it will wait 10 seconds before killing it, because docker stop command has option --time, -t with defoult value 10 second: https://docs.docker.com/engine/reference/commandline/stop/

3 likes

Please or to participate in this conversation.