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';
73 require_once(INSTALLDIR.'/lib/daemon.php');
74 require_once(INSTALLDIR.'/classes/Queue_item.php');
75 require_once(INSTALLDIR.'/classes/Notice.php');
77 define('CLAIM_TIMEOUT', 1200);
80 * Queue handling daemon...
82 * The queue daemon by default launches in the background, at which point
83 * it'll pass control to the configured QueueManager class to poll for updates.
85 * We can then pass individual items through the QueueHandler subclasses
88 class QueueDaemon extends SpawningDaemon
90 protected $allsites = false;
92 function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
94 parent::__construct($id, $daemonize, $threads);
95 $this->all = $allsites;
99 * Setup and start of run loop for this queue handler as a daemon.
100 * Most of the heavy lifting is passed on to the QueueManager's service()
101 * method, which passes control on to the QueueHandler's handle()
102 * method for each item that comes in on the queue.
104 * @return boolean true on success, false on failure
108 $this->log(LOG_INFO, 'checking for queued notices');
110 $master = new QueueMaster($this->get_id());
111 $master->init($this->all);
114 } catch (Exception $e) {
115 common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
116 str_replace("\n", " ", $e->getTraceAsString()));
117 return self::EXIT_ERR;
120 $this->log(LOG_INFO, 'finished servicing the queue');
122 $this->log(LOG_INFO, 'terminating normally');
124 return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
128 class QueueMaster extends IoMaster
131 * Initialize IoManagers for the currently configured site
132 * which are appropriate to this instance.
134 function initManagers()
137 if (Event::handle('StartQueueDaemonIoManagers', array(&$classes))) {
138 $classes[] = 'QueueManager';
140 Event::handle('EndQueueDaemonIoManagers', array(&$classes));
142 foreach ($classes as $class) {
143 $this->instantiate($class);
148 if (have_option('i')) {
149 $id = get_option_value('i');
150 } else if (have_option('--id')) {
151 $id = get_option_value('--id');
152 } else if (count($args) > 0) {
158 if (have_option('t')) {
159 $threads = intval(get_option_value('t'));
160 } else if (have_option('--threads')) {
161 $threads = intval(get_option_value('--threads'));
166 $threads = getProcessorCount();
169 $daemonize = !(have_option('f') || have_option('--foreground'));
170 $all = have_option('a') || have_option('--all');
172 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);