php-resque/bin/resque

122 lines
3.2 KiB
Plaintext
Raw Permalink Normal View History

2013-03-13 11:41:32 +00:00
#!/usr/bin/env php
2010-04-18 13:58:43 +00:00
<?php
// Find and initialize Composer
2018-05-27 02:52:05 +00:00
$files = [
2013-06-03 16:09:54 +00:00
__DIR__ . '/../../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
__DIR__ . '/../../../../autoload.php',
__DIR__ . '/../vendor/autoload.php',
2018-05-27 02:52:05 +00:00
];
$found = false;
foreach ($files as $file) {
2013-06-03 16:09:54 +00:00
if (file_exists($file)) {
require_once $file;
break;
}
}
if (!class_exists('Composer\Autoload\ClassLoader', false)) {
2013-06-03 16:09:54 +00:00
die(
'You need to set up the project dependencies using the following commands:' . PHP_EOL .
2018-05-27 02:52:05 +00:00
'curl -s http://getcomposer.org/installer | php' . PHP_EOL .
'php composer.phar install' . PHP_EOL
2013-06-03 16:09:54 +00:00
);
}
2010-12-11 01:30:28 +00:00
$QUEUE = getenv('QUEUE');
2018-05-27 02:52:05 +00:00
if (empty($QUEUE)) {
2013-06-03 16:09:54 +00:00
die("Set QUEUE env var containing the list of queues to work.\n");
2010-04-18 13:58:43 +00:00
}
/**
* REDIS_BACKEND can have simple 'host:port' format or use a DSN-style format like this:
* - redis://user:pass@host:port
*
* Note: the 'user' part of the DSN URI is required but is not used.
*/
2010-12-11 01:30:28 +00:00
$REDIS_BACKEND = getenv('REDIS_BACKEND');
// A redis database number
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
2018-05-27 02:52:05 +00:00
if (!empty($REDIS_BACKEND)) {
2013-06-03 16:09:54 +00:00
if (empty($REDIS_BACKEND_DB))
Resque::setBackend($REDIS_BACKEND);
else
Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
2010-04-18 13:58:43 +00:00
}
$logLevel = false;
$LOGGING = getenv('LOGLEVEL');
if (!empty($LOGGING) ) {
$logLevel = $LOGGING;
2010-04-18 13:58:43 +00:00
}
$APP_INCLUDE = getenv('APP_INCLUDE');
2018-05-27 02:52:05 +00:00
if ($APP_INCLUDE) {
if (!file_exists($APP_INCLUDE)) {
die('APP_INCLUDE (' . $APP_INCLUDE . ") does not exist.\n");
2013-06-03 16:09:54 +00:00
}
2013-06-03 16:09:54 +00:00
require_once $APP_INCLUDE;
}
// See if the APP_INCLUDE containes a logger object,
// If none exists, fallback to internal logger
if (!isset($logger) || !is_object($logger)) {
$logger = new Resque_Log($logLevel);
}
2013-06-03 16:09:54 +00:00
$BLOCKING = getenv('BLOCKING') !== FALSE;
2010-04-18 13:58:43 +00:00
$interval = 5;
2010-12-11 01:30:28 +00:00
$INTERVAL = getenv('INTERVAL');
2018-05-27 02:52:05 +00:00
if (!empty($INTERVAL)) {
2013-06-03 16:09:54 +00:00
$interval = $INTERVAL;
2010-04-18 13:58:43 +00:00
}
$count = 1;
2010-12-11 01:30:28 +00:00
$COUNT = getenv('COUNT');
2018-05-27 02:52:05 +00:00
if (!empty($COUNT) && $COUNT > 1) {
2013-06-03 16:09:54 +00:00
$count = $COUNT;
2010-04-18 13:58:43 +00:00
}
$PREFIX = getenv('PREFIX');
2018-05-27 02:52:05 +00:00
if (!empty($PREFIX)) {
$logger->log(Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', ['prefix' => $PREFIX]);
Resque_Redis::prefix($PREFIX);
}
2018-05-27 02:52:05 +00:00
if ($count > 1) {
for ($i = 0; $i < $count; ++$i) {
2013-06-03 16:09:54 +00:00
$pid = Resque::fork();
2018-05-27 02:52:05 +00:00
if ($pid === false || $pid === -1) {
$logger->log(Psr\Log\LogLevel::EMERGENCY, 'Could not fork worker {count}', ['count' => $i]);
die();
2018-05-27 02:52:05 +00:00
} elseif (!$pid) {
// Child, start the worker
2013-06-03 16:09:54 +00:00
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker->setLogger($logger);
2018-05-27 02:52:05 +00:00
$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', ['worker' => $worker]);
2013-06-03 16:09:54 +00:00
$worker->work($interval, $BLOCKING);
break;
}
}
2018-05-27 02:52:05 +00:00
} else {
// Start a single worker
2013-06-03 16:09:54 +00:00
$queues = explode(',', $QUEUE);
$worker = new Resque_Worker($queues);
$worker->setLogger($logger);
2013-06-03 16:09:54 +00:00
$PIDFILE = getenv('PIDFILE');
if ($PIDFILE) {
file_put_contents($PIDFILE, getmypid()) or
2018-05-27 02:52:05 +00:00
die('Could not write PID information to ' . $PIDFILE);
2013-06-03 16:09:54 +00:00
}
2018-05-27 02:52:05 +00:00
$logger->log(Psr\Log\LogLevel::NOTICE, 'Starting worker {worker}', ['worker' => $worker]);
2013-06-03 16:09:54 +00:00
$worker->work($interval, $BLOCKING);
2010-04-18 13:58:43 +00:00
}