2013-03-13 11:41:32 +00:00
|
|
|
#!/usr/bin/env php
|
2010-04-18 13:58:43 +00:00
|
|
|
<?php
|
2013-01-12 11:40:26 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
];
|
2013-01-12 11:40:26 +00:00
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2013-06-03 16:09:54 +00:00
|
|
|
if (file_exists($file)) {
|
|
|
|
require_once $file;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-12 11:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
);
|
2013-01-12 11:40:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Set which queues to monitor '*'
|
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
|
|
|
}
|
|
|
|
|
2014-05-05 15:11:20 +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');
|
2014-05-05 15:11:20 +00:00
|
|
|
|
2022-09-12 07:50:02 +00:00
|
|
|
/**
|
|
|
|
* REDIS_BACKEND_DB overrides default Redis DB
|
|
|
|
*/
|
2013-02-06 14:37:12 +00:00
|
|
|
$REDIS_BACKEND_DB = getenv('REDIS_BACKEND_DB');
|
2018-05-27 02:52:05 +00:00
|
|
|
if (!empty($REDIS_BACKEND)) {
|
2018-06-22 08:13:16 +00:00
|
|
|
if (empty($REDIS_BACKEND_DB)) {
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::setBackend($REDIS_BACKEND);
|
2018-06-22 08:13:16 +00:00
|
|
|
} else {
|
2021-02-18 23:23:32 +00:00
|
|
|
\Resque\Resque::setBackend($REDIS_BACKEND, $REDIS_BACKEND_DB);
|
2018-06-22 08:13:16 +00:00
|
|
|
}
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Set Logging level
|
2013-06-25 17:06:37 +00:00
|
|
|
$logLevel = false;
|
2018-05-29 10:20:56 +00:00
|
|
|
$LOGGING = getenv('LOGLEVEL');
|
2018-06-22 08:13:16 +00:00
|
|
|
if (!empty($LOGGING)) {
|
2018-05-29 10:20:56 +00:00
|
|
|
$logLevel = $LOGGING;
|
2010-04-18 13:58:43 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Bootstrap file
|
2012-03-01 05:33:26 +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
|
|
|
}
|
2012-03-01 05:33:26 +00:00
|
|
|
|
2013-06-03 16:09:54 +00:00
|
|
|
require_once $APP_INCLUDE;
|
2012-03-01 05:33:26 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 19:10:08 +00:00
|
|
|
// See if the APP_INCLUDE containes a logger object,
|
|
|
|
// If none exists, fallback to internal logger
|
2013-08-22 08:32:56 +00:00
|
|
|
if (!isset($logger) || !is_object($logger)) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$logger = new \Resque\Log($logLevel);
|
2013-06-25 19:10:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Determines if blocking or not
|
2013-06-03 16:09:54 +00:00
|
|
|
$BLOCKING = getenv('BLOCKING') !== FALSE;
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Interval to check for jobs
|
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
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Sets worker count
|
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
|
|
|
}
|
|
|
|
|
2021-02-18 23:23:32 +00:00
|
|
|
// Determines redis key prefix
|
2013-03-26 12:59:33 +00:00
|
|
|
$PREFIX = getenv('PREFIX');
|
2018-05-27 02:52:05 +00:00
|
|
|
if (!empty($PREFIX)) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$logger->log(\Psr\Log\LogLevel::INFO, 'Prefix set to {prefix}', ['prefix' => $PREFIX]);
|
|
|
|
\Resque\Redis::prefix($PREFIX);
|
2013-03-26 12:59:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 02:52:05 +00:00
|
|
|
if ($count > 1) {
|
|
|
|
for ($i = 0; $i < $count; ++$i) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$pid = \Resque\Resque::fork();
|
2018-05-27 02:52:05 +00:00
|
|
|
if ($pid === false || $pid === -1) {
|
2021-02-18 23:23:32 +00:00
|
|
|
$logger->log(\Psr\Log\LogLevel::EMERGENCY, 'Could not fork worker {count}', ['count' => $i]);
|
2013-06-25 17:06:37 +00:00
|
|
|
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);
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker($queues);
|
2013-06-25 19:10:08 +00:00
|
|
|
$worker->setLogger($logger);
|
2021-02-18 23:23:32 +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);
|
2021-02-18 23:23:32 +00:00
|
|
|
$worker = new \Resque\Worker($queues);
|
2013-06-25 19:10:08 +00:00
|
|
|
$worker->setLogger($logger);
|
2013-01-12 11:40:26 +00:00
|
|
|
|
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
|
|
|
}
|
2011-03-27 05:01:35 +00:00
|
|
|
|
2021-02-18 23:23:32 +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
|
|
|
}
|