improvements to config

This commit is contained in:
Holger Reinhardt 2016-07-29 13:49:06 +02:00
parent b60fb0d712
commit afceda3d95
3 changed files with 137 additions and 15 deletions

View File

@ -1,23 +1,105 @@
<?php
namespace Hlgrrnhrdt\Resque\Console;
use Illuminate\Console\Command;
use Hlgrrnhrdt\Resque\ResqueManager;
use Illuminate\Console\Command as IlluminateCommand;
use Resque_Worker;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* WorkCommand
*
* @author Holger Reinhardt <hlgrrnhrdt@gmail.com>
* @author Holger Reinhardt <holger.reinhardt@aboutyou.de>
*/
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],
];
}

View File

@ -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;
}
}

View File

@ -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'));
});
}
}