Skip to content

Instantly share code, notes, and snippets.

@jdforsythe
Created May 5, 2016 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jdforsythe/b8c9bd46250ee23daa9de15d19495f07 to your computer and use it in GitHub Desktop.
Save jdforsythe/b8c9bd46250ee23daa9de15d19495f07 to your computer and use it in GitHub Desktop.
Laravel 5 Artisan Process Entire Queue and Exit Command
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
use Illuminate\Queue\Worker;
use Illuminate\Contracts\Queue\Job;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ProcessQueueAndExit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'queue:workall {connection?} {--queue=} {--daemon} {--delay=} {--force} {--memory=} {--sleep=} {--tries=}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Process all jobs on a queue and exit';
/**
* The queue worker instance.
*
* @var \Illuminate\Queue\Worker
*/
protected $worker;
/**
* Create a new command instance.
*
* @param \Illuminate\Queue\Worker $worker
* @return void
*/
public function __construct(Worker $worker)
{
parent::__construct();
$this->worker = $worker;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if ($this->downForMaintenance() && ! $this->option('daemon')) {
return $this->worker->sleep($this->option('sleep'));
}
$queue = $this->option('queue');
$delay = $this->option('delay');
// the memory limit is the amount of memory we will allow the script to occupy
// before killing it
$memory = $this->option('memory');
$connection = $this->argument('connection');
// keep processing until there are no more jobs returned
do {
$response = $this->runWorker(
$connection, $queue, $delay, $memory, $this->option('daemon')
);
if (! is_null($response['job'])) {
$this->writeOutput($response['job'], $response['failed']);
}
} while (! is_null($response['job']));
}
/**
* Run the worker instance
*
* @param string $connection
* @param string $queue
* @param int $delay
* @param int $memory
* @param bool $daemon
* @return array
*/
protected function runWorker($connection, $queue, $delay, $memory, $daemon = false)
{
if ($daemon) {
$this->worker->setCache($this->laravel['cache']->driver());
$this->worker->setDaemonExceptionHandler(
$this->laravel['Illuminate\Contracts\Debug\ExceptionHandler']
);
return $this->worker->daemon(
$connection, $queue, $delay, $memory,
$this->option('sleep'), $this->option('tries')
);
}
return $this->worker->pop(
$connection, $queue, $delay,
$this->option('sleep'), $this->option('tries')
);
}
/**
* Write the status output for the queue worker.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @param bool $failed
* @return void
*/
protected function writeOutput(Job $job, $failed)
{
if ($failed) {
$this->output->writeln('<error>['.Carbon::now()->format('Y-m-d H:i:s').'] Failed:</error> '.$job->getName());
} else {
$this->output->writeln('<info>['.Carbon::now()->format('Y-m-d H:i:s').'] Processed:</info> '.$job->getName());
}
}
/**
* Determine if the worker should run in maintenance mode.
*
* @return bool
*/
protected function downForMaintenance()
{
if ($this->option('force')) {
return false;
}
return $this->laravel->isDownForMaintenance();
}
}
@uaibo
Copy link

uaibo commented Mar 9, 2017

this doesn't seem to process ALL the jobs. it only processes 1 job (from jobs table) and then exits. I would like to process all jobs on the jobs table. any idea?

@fimdomeio
Copy link

This apparently no longer works since worker->pop is no longer part of the api.

@garsaud
Copy link

garsaud commented Apr 17, 2017

I made a fix of this script, for it to work on Laravel 5.4
Now all the jobs are processed, it marks the failures as 'failed', and it exits.
I am using it in a cron task.

@mojtabaahn
Copy link

tried @sae-reel 's fixed code for Laravel 5.4.
works great!

@dmitrydrynov
Copy link

dmitrydrynov commented May 24, 2018

me too, please

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