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/