Skip to content

Instantly share code, notes, and snippets.

@ivanvermeyen
Created January 18, 2016 23:45
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ivanvermeyen/4a4ba5146d61d19c61dc to your computer and use it in GitHub Desktop.
Save ivanvermeyen/4a4ba5146d61d19c61dc to your computer and use it in GitHub Desktop.
Find any running "queue:listen" processes for a Laravel project and kill those. Fire "php artisan queue:stop" in your "current" project root. Intended to use with a deploy script when you are using non-daemon listeners and deploy releases. If you are using daemons, run "queue:restart" instead. You can use a process monitoring tool like Superviso…
<?php
namespace App\Console;
use App\Console\Commands\StopQueueListeners;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
StopQueueListeners::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
protected function schedule(Schedule $schedule)
{
//
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class StopQueueListeners extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'queue:stop';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Kill the queue:listen processes for this project.';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$pids = $this->getQueueListenerPids();
if (count($pids) === 0) {
$this->info('No queue listeners are currently running.');
return;
}
$this->stopQueueListeners($pids);
$this->info('All done.');
}
/**
* Get the PID's of queue listeners for the current project.
*
* @return array
*/
private function getQueueListenerPids()
{
$command = 'ps -u forge -eo pid,user,command | grep $(pwd) | grep "queue:listen" | grep -v grep | awk \'{print $1}\'';
exec($command, $pids);
return (array) $pids;
}
/**
* Kill queue listener processes.
*
* @param array $pids
*
* @return void
*/
private function stopQueueListeners(array $pids)
{
foreach ($pids as $pid) {
$this->info('Killing queue listener with PID #' . $pid);
exec('kill -TERM ' . $pid);
}
}
}
@trevorgehman
Copy link

Is this a graceful termination? Will it allow currently processing jobs to finish?

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