4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $shortoptions = 'fi:at:';
24 $longoptions = array('id=', 'foreground', 'all', 'threads=');
27 * Attempts to get a count of the processors available on the current system
28 * to fan out multiple threads.
30 * Recognizes Linux and Mac OS X; others will return default of 1.
32 * @fixme move this to SpawningDaemon, but to get the default val for help
33 * text we seem to need it before loading infrastructure
36 function getProcessorCount()
41 $cpuinfo = file('/proc/cpuinfo');
42 foreach (file('/proc/cpuinfo') as $line) {
43 if (preg_match('/^processor\s+:\s+(\d+)\s?$/', $line)) {
49 $cpus = intval(shell_exec("/usr/sbin/sysctl -n hw.ncpu 2>/dev/null"));
58 $threads = getProcessorCount();
59 $helptext = <<<END_OF_QUEUE_HELP
60 Daemon script for running queued items.
62 -i --id Identity (default none)
63 -f --foreground Stay in the foreground (default background)
64 -a --all Handle queues for all local sites
65 (requires Stomp queue handler, status_network setup)
66 -t --threads=<n> Spawn <n> processing threads (default $threads)
71 require_once INSTALLDIR.'/scripts/commandline.inc.php';
73 require_once(INSTALLDIR.'/lib/daemon.php');
74 require_once(INSTALLDIR.'/classes/Queue_item.php');
75 require_once(INSTALLDIR.'/classes/Notice.php');
78 * Queue handling daemon...
80 * The queue daemon by default launches in the background, at which point
81 * it'll pass control to the configured QueueManager class to poll for updates.
83 * We can then pass individual items through the QueueHandler subclasses
86 class QueueDaemon extends SpawningDaemon
88 protected $allsites = false;
90 function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
92 parent::__construct($id, $daemonize, $threads);
93 $this->allsites = $allsites;
97 * Setup and start of run loop for this queue handler as a daemon.
98 * Most of the heavy lifting is passed on to the QueueManager's service()
99 * method, which passes control on to the QueueHandler's handle()
100 * method for each item that comes in on the queue.
102 * @return boolean true on success, false on failure
106 $this->log(LOG_INFO, 'checking for queued notices');
108 $master = new QueueMaster($this->get_id(), $this->processManager());
109 $master->init($this->allsites);
112 } catch (Exception $e) {
113 common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
114 str_replace("\n", " ", $e->getTraceAsString()));
115 return self::EXIT_ERR;
118 $this->log(LOG_INFO, 'finished servicing the queue');
120 $this->log(LOG_INFO, 'terminating normally');
122 return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
126 class QueueMaster extends IoMaster
128 protected $processManager;
130 function __construct($id, $processManager)
132 parent::__construct($id);
133 $this->processManager = $processManager;
137 * Initialize IoManagers which are appropriate to this instance.
139 function initManagers()
142 if (Event::handle('StartQueueDaemonIoManagers', array(&$managers))) {
143 $qm = QueueManager::get();
144 $qm->setActiveGroup('main');
146 $managers[] = $this->processManager;
148 Event::handle('EndQueueDaemonIoManagers', array(&$managers));
150 foreach ($managers as $manager) {
151 $this->instantiate($manager);
156 if (have_option('i')) {
157 $id = get_option_value('i');
158 } else if (have_option('--id')) {
159 $id = get_option_value('--id');
160 } else if (count($args) > 0) {
166 if (have_option('t')) {
167 $threads = intval(get_option_value('t'));
168 } else if (have_option('--threads')) {
169 $threads = intval(get_option_value('--threads'));
174 $threads = getProcessorCount();
177 $daemonize = !(have_option('f') || have_option('--foreground'));
178 $all = have_option('a') || have_option('--all');
180 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);