From afceda3d95e1f340cd23f9632e61eb1f93a6cf62 Mon Sep 17 00:00:00 2001 From: Holger Reinhardt Date: Fri, 29 Jul 2016 13:49:06 +0200 Subject: [PATCH] improvements to config --- src/Console/WorkCommand.php | 104 +++++++++++++++++++++++++++++++--- src/ResqueManager.php | 35 +++++++++++- src/ResqueServiceProvider.php | 13 +++-- 3 files changed, 137 insertions(+), 15 deletions(-) diff --git a/src/Console/WorkCommand.php b/src/Console/WorkCommand.php index 1470a5a..391c099 100644 --- a/src/Console/WorkCommand.php +++ b/src/Console/WorkCommand.php @@ -1,23 +1,105 @@ + * @author Holger Reinhardt */ -class WorkCommand extends Command +class WorkCommand extends IlluminateCommand { + /** + * The console command name. + * + * @var string + */ protected $name = 'resque:work'; + /** + * @var \Hlgrrnhrdt\Resque\ResqueManager + */ + private $manager; + + /** + * @param \Hlgrrnhrdt\Resque\ResqueManager $manager + */ + public function __construct(ResqueManager $manager) + { + parent::__construct(); + $this->manager = $manager; + } + + /** + * Execute the console command. + * + * @return int + */ public function fire() { $queue = $this->option('queue'); - $interval = $this->option('interval'); - $count = $this->option('count'); + $interval = (int)$this->option('interval'); + $count = (int)$this->option('count'); + + $queues = explode(',', $queue); + $logLevel = $this->getLogLevel(); + + if ($count > 1) { + for ($i = 0; $i < $count; $i++) { + try { + $pid = $this->manager->fork(); + } catch (\RuntimeException $exception) { + $this->error($exception->getMessage()); + + return 1; + } + + if (0 === $pid) { + $this->startWorker($queues, $interval, $logLevel); + } + } + } else { + $this->startWorker($queues, $interval, $logLevel); + } + + return 0; + } + + /** + * @param array $queues + * @param int $logLevel + * @param int $interval + */ + private function startWorker(array $queues, $interval = 5, $logLevel = Resque_Worker::LOG_NONE) + { + $worker = $this->manager->startWorker($queues, $interval, $logLevel); + $this->info(sprintf('Starting worker %s', $worker)); + } + + /** + * @return int + */ + protected function getLogLevel() + { + switch ($this->verbosity) { + case OutputInterface::VERBOSITY_VERBOSE: + $logLevel = Resque_Worker::LOG_NORMAL; + break; + + case OutputInterface::VERBOSITY_VERY_VERBOSE: + $logLevel = Resque_Worker::LOG_VERBOSE; + break; + + default: + $logLevel = Resque_Worker::LOG_NONE; + } + + return $logLevel; } /** @@ -26,9 +108,15 @@ class WorkCommand extends Command protected function getOptions() { return [ - ['queue', null, InputOption::VALUE_IS_ARRAY & InputOption::VALUE_OPTIONAL, '', 'default'], - ['interval', null, InputOption::VALUE_OPTIONAL, '', 5], - ['count', null, InputOption::VALUE_OPTIONAL, '', 1], + [ + 'queue', + null, + InputOption::VALUE_IS_ARRAY & InputOption::VALUE_OPTIONAL, + 'The queue to work on', + 'default', + ], + ['interval', null, InputOption::VALUE_OPTIONAL, 'The queue to work on', 5], + ['count', null, InputOption::VALUE_OPTIONAL, 'The queue to work on', 1], ]; } diff --git a/src/ResqueManager.php b/src/ResqueManager.php index 21a8f4e..fc5f20b 100644 --- a/src/ResqueManager.php +++ b/src/ResqueManager.php @@ -2,6 +2,7 @@ namespace Hlgrrnhrdt\Resque; use Resque; +use Resque_Worker; use RuntimeException; /** @@ -12,13 +13,25 @@ use RuntimeException; class ResqueManager { /** - * @var PhpResque + * @var Resque */ - private $resque; + protected $resque; - public function __construct(Resque $resque) + /** + * @var bool + */ + protected $trackStatus = false; + + /** + * ResqueManager constructor. + * + * @param \Resque $resque + * @param bool $trackStatus + */ + public function __construct(Resque $resque, $trackStatus = false) { $this->resque = $resque; + $this->trackStatus = $trackStatus; } /** @@ -85,4 +98,20 @@ class ResqueManager return $pid; } + + /** + * @param array $queues + * @param int $interval + * @param int $logLevel + * + * @return \Resque_Worker + */ + public function startWorker(array $queues, $interval = 5, $logLevel = Resque_Worker::LOG_NONE) + { + $worker = new Resque_Worker($queues); + $worker->logLevel = $logLevel; + $worker->work($interval); + + return $worker; + } } diff --git a/src/ResqueServiceProvider.php b/src/ResqueServiceProvider.php index c7c5774..0b5b60f 100644 --- a/src/ResqueServiceProvider.php +++ b/src/ResqueServiceProvider.php @@ -3,6 +3,7 @@ namespace Hlgrrnhrdt\Resque; use Config; use Hlgrrnhrdt\Resque\Console\WorkCommand; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\ServiceProvider; use Resque; @@ -24,20 +25,24 @@ class ResqueServiceProvider extends ServiceProvider $this->registerWorkCommand(); } + + protected function registerManager() { - $this->app->singleton(ResqueManager::class, function ($app) { + $this->app->singleton('resque.manager', function (Application $app) { $config = $app['config']['resque.connection']; + $resque = new Resque(); $resque->setBackend(implode(':', [$config['host'], $config['port']]), $config['db']); - return new ResqueManager($resque); + + return new ResqueManager($resque, $app['config']['resque.trackStatus']); }); } protected function registerWorkCommand() { - $this->app->singleton('command.resque.work', function () { - return new WorkCommand(); + $this->app->singleton('command.resque.work', function (Application $app) { + return new WorkCommand($app->make('resque.manager')); }); } }