123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
- $shortoptions = 'fi:at:';
- $longoptions = array('id=', 'foreground', 'all', 'threads=');
- function getProcessorCount()
- {
- $cpus = 0;
- switch (PHP_OS) {
- case 'Linux':
- $cpuinfo = file('/proc/cpuinfo');
- foreach (file('/proc/cpuinfo') as $line) {
- if (preg_match('/^processor\s+:\s+(\d+)\s?$/', $line)) {
- $cpus++;
- }
- }
- break;
- case 'Darwin':
- $cpus = intval(shell_exec("/usr/sbin/sysctl -n hw.ncpu 2>/dev/null"));
- break;
- }
- if ($cpus) {
- return $cpus;
- }
- return 1;
- }
- $threads = getProcessorCount();
- $helptext = <<<END_OF_QUEUE_HELP
- Daemon script for running queued items.
- -i --id Identity (default none)
- -f --foreground Stay in the foreground (default background)
- -a --all Handle queues for all local sites
- (requires Stomp queue handler, status_network setup)
- -t --threads=<n> Spawn <n> processing threads (default $threads)
- END_OF_QUEUE_HELP;
- require_once INSTALLDIR.'/scripts/commandline.inc';
- require_once(INSTALLDIR.'/lib/daemon.php');
- require_once(INSTALLDIR.'/classes/Queue_item.php');
- require_once(INSTALLDIR.'/classes/Notice.php');
- class QueueDaemon extends SpawningDaemon
- {
- protected $allsites = false;
- function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
- {
- parent::__construct($id, $daemonize, $threads);
- $this->allsites = $allsites;
- }
-
- function runThread()
- {
- $this->log(LOG_INFO, 'checking for queued notices');
- $master = new QueueMaster($this->get_id(), $this->processManager());
- $master->init($this->allsites);
- try {
- $master->service();
- } catch (Exception $e) {
- common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
- str_replace("\n", " ", $e->getTraceAsString()));
- return self::EXIT_ERR;
- }
- $this->log(LOG_INFO, 'finished servicing the queue');
- $this->log(LOG_INFO, 'terminating normally');
- return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
- }
- }
- class QueueMaster extends IoMaster
- {
- protected $processManager;
- function __construct($id, $processManager)
- {
- parent::__construct($id);
- $this->processManager = $processManager;
- }
-
- function initManagers()
- {
- $managers = array();
- if (Event::handle('StartQueueDaemonIoManagers', array(&$managers))) {
- $qm = QueueManager::get();
- $qm->setActiveGroup('main');
- $managers[] = $qm;
- $managers[] = $this->processManager;
- }
- Event::handle('EndQueueDaemonIoManagers', array(&$managers));
- foreach ($managers as $manager) {
- $this->instantiate($manager);
- }
- }
- }
- if (have_option('i')) {
- $id = get_option_value('i');
- } else if (have_option('--id')) {
- $id = get_option_value('--id');
- } else if (count($args) > 0) {
- $id = $args[0];
- } else {
- $id = null;
- }
- if (have_option('t')) {
- $threads = intval(get_option_value('t'));
- } else if (have_option('--threads')) {
- $threads = intval(get_option_value('--threads'));
- } else {
- $threads = 0;
- }
- if (!$threads) {
- $threads = getProcessorCount();
- }
- $daemonize = !(have_option('f') || have_option('--foreground'));
- $all = have_option('a') || have_option('--all');
- $daemon = new QueueDaemon($id, $daemonize, $threads, $all);
- $daemon->runOnce();
|