Skip to content

Commit

Permalink
Added locale resque start scripts for resque and resque-scheduler.
Browse files Browse the repository at this point in the history
After talking to @danhunsaker who explained that the scripts in php-resque were mend as examples rather than actual starter scripts. I added these to BBCResqueBundle it self.

This allows us to customize the start process.

You can see the discussion here: chrisboulton/php-resque#123
  • Loading branch information
ulrik nielsen committed Aug 15, 2013
1 parent 594b9d5 commit 492593b
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Command/StartScheduledWorkerCommand.php
Expand Up @@ -52,7 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$env['REDIS_BACKEND_DB'] = $redisDatabase;
}

$workerCommand = 'php '.$this->getContainer()->getParameter('bcc_resque.resque.vendor_dir').'/chrisboulton/php-resque-scheduler/resque-scheduler.php';
$workerCommand = 'php '.__DIR__.'/../bin/resque-scheduler';

if (!$input->getOption('foreground')) {
$logFile = $this->getContainer()->getParameter(
Expand All @@ -63,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

// In windows: When you pass an environment to CMD it replaces the old environment
// That means we create a lot of problems with respect to user accounts and missing vars
// this is a workaround where we add the vars to the existing environment.
// this is a workaround where we add the vars to the existing environment.
if (defined('PHP_WINDOWS_VERSION_BUILD'))
{
foreach($env as $key => $value)
Expand Down
11 changes: 6 additions & 5 deletions Command/StartWorkerCommand.php
Expand Up @@ -58,9 +58,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
if (0 !== $m = (int) $input->getOption('memory-limit')) {
$opt = sprintf('-d memory_limit=%dM', $m);
}
$workerCommand = strtr('php %opt% %dir%/chrisboulton/php-resque/resque.php', array(

$workerCommand = strtr('php %opt% %dir%/resque', array(
'%opt%' => $opt,
'%dir%' => $this->getContainer()->getParameter('bcc_resque.resque.vendor_dir'),
'%dir%' => __DIR__.'/../bin',
));

if (!$input->getOption('foreground')) {
Expand All @@ -69,11 +70,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
'%logs_dir%' => $this->getContainer()->getParameter('kernel.logs_dir'),
));
}


// In windows: When you pass an environment to CMD it replaces the old environment
// That means we create a lot of problems with respect to user accounts and missing vars
// this is a workaround where we add the vars to the existing environment.
// this is a workaround where we add the vars to the existing environment.
if (defined('PHP_WINDOWS_VERSION_BUILD'))
{
foreach($env as $key => $value)
Expand Down
81 changes: 81 additions & 0 deletions bin/resque
@@ -0,0 +1,81 @@
#!/usr/bin/env php
<?php

$QUEUE = getenv('QUEUE');
if (empty($QUEUE)) {
die("Set QUEUE env var containing the list of queues to work.\n");
}

require_once __DIR__."/../../../../../autoload.php";

$REDIS_BACKEND = getenv('REDIS_BACKEND');
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
if (!empty($REDIS_BACKEND)) {
if (empty($REDIS_BACKEND_DB)) {
Resque::setBackend($REDIS_BACKEND);
} else {
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
}
}

$logLevel = 0;
$LOGGING = getenv('LOGGING');
$VERBOSE = getenv('VERBOSE');
$VVERBOSE = getenv('VVERBOSE');
if (!empty($LOGGING) || !empty($VERBOSE)) {
$logLevel = Resque_Worker::LOG_NORMAL;
} else if (!empty($VVERBOSE)) {
$logLevel = Resque_Worker::LOG_VERBOSE;
}

$APP_INCLUDE = getenv('APP_INCLUDE');
if ($APP_INCLUDE) {
if (!file_exists($APP_INCLUDE)) {
die('APP_INCLUDE ('.$APP_INCLUDE.") does not exist.\n");
}

require_once $APP_INCLUDE;
}

$interval = 5;
$INTERVAL = getenv('INTERVAL');
if (!empty($INTERVAL)) {
$interval = $INTERVAL;
}

$count = 1;
$COUNT = getenv('COUNT');
if (!empty($COUNT) && $COUNT > 1) {
$count = $COUNT;
}

if ($count > 1) {
for ($i = 0; $i < $count; ++$i) {
$pid = pcntl_fork();
if ($pid == -1) {
die("Could not fork worker ".$i."\n");
} else if (!$pid) {
// Child, start the worker
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker->logLevel = $logLevel;
fwrite(STDOUT, '*** Starting worker '.$worker."\n");
$worker->work($interval);
break;
}
}
} else {
// Start a single worker
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker->logLevel = $logLevel;

$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
file_put_contents($PIDFILE, getmypid()) or
die('Could not write PID information to ' . $PIDFILE);
}

fwrite(STDOUT, '*** Starting worker '.$worker."\n");
$worker->work($interval);
}
60 changes: 60 additions & 0 deletions bin/resque-scheduler
@@ -0,0 +1,60 @@
#!/usr/bin/env php
<?php

require_once __DIR__."/../../../../../autoload.php";

// Look for an environment variable with
$RESQUE_PHP = getenv('RESQUE_PHP');
if (!empty($RESQUE_PHP)) {
require_once $RESQUE_PHP;
}

$REDIS_BACKEND = getenv('REDIS_BACKEND');
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
if (!empty($REDIS_BACKEND)) {
if (empty($REDIS_BACKEND_DB)) {
Resque::setBackend($REDIS_BACKEND);
} else {
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
}
}

// Set log level for resque-scheduler
$logLevel = 0;
$LOGGING = getenv('LOGGING');
$VERBOSE = getenv('VERBOSE');
$VVERBOSE = getenv('VVERBOSE');
if (!empty($LOGGING) || !empty($VERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_NORMAL;
} else if (!empty($VVERBOSE)) {
$logLevel = ResqueScheduler_Worker::LOG_VERBOSE;
}

// Check for jobs every $interval seconds
$interval = 5;
$INTERVAL = getenv('INTERVAL');
if (!empty($INTERVAL)) {
$interval = $INTERVAL;
}

// Load the user's application if one exists
$APP_INCLUDE = getenv('APP_INCLUDE');
if ($APP_INCLUDE) {
if (!file_exists($APP_INCLUDE)) {
die('APP_INCLUDE ('.$APP_INCLUDE.") does not exist.\n");
}

require_once $APP_INCLUDE;
}

$worker = new ResqueScheduler_Worker();
$worker->logLevel = $logLevel;

$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
file_put_contents($PIDFILE, getmypid()) or
die('Could not write PID information to ' . $PIDFILE);
}

fwrite(STDOUT, "*** Starting scheduler worker\n");
$worker->work($interval);

0 comments on commit 492593b

Please sign in to comment.