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 $this->log(LOG_INFO, 'finished servicing the queue');
116 $this->log(LOG_INFO, 'terminating normally');
122 class QueueMaster extends IoMaster
125 * Initialize IoManagers for the currently configured site
126 * which are appropriate to this instance.
128 function initManagers()
131 if (Event::handle('StartQueueDaemonIoManagers', array(&$classes))) {
132 $classes[] = 'QueueManager';
134 Event::handle('EndQueueDaemonIoManagers', array(&$classes));
136 foreach ($classes as $class) {
137 $this->instantiate($class);
142 if (have_option('i')) {
143 $id = get_option_value('i');
144 } else if (have_option('--id')) {
145 $id = get_option_value('--id');
146 } else if (count($args) > 0) {
152 if (have_option('t')) {
153 $threads = intval(get_option_value('t'));
154 } else if (have_option('--threads')) {
155 $threads = intval(get_option_value('--threads'));
160 $threads = getProcessorCount();
163 $daemonize = !(have_option('f') || have_option('--foreground'));
164 $all = have_option('a') || have_option('--all');
166 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);