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', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
24 $shortoptions = 'fi:at:';
25 $longoptions = array('id=', 'foreground', 'all', 'threads=');
28 * Attempts to get a count of the processors available on the current system
29 * to fan out multiple threads.
31 * Recognizes Linux and Mac OS X; others will return default of 1.
33 * @fixme move this to SpawningDaemon, but to get the default val for help
34 * text we seem to need it before loading infrastructure
37 function getProcessorCount()
42 $cpuinfo = file('/proc/cpuinfo');
43 foreach (file('/proc/cpuinfo') as $line) {
44 if (preg_match('/^processor\s+:\s+(\d+)\s?$/', $line)) {
50 $cpus = intval(shell_exec("/usr/sbin/sysctl -n hw.ncpu 2>/dev/null"));
59 $threads = getProcessorCount();
60 $helptext = <<<END_OF_QUEUE_HELP
61 Daemon script for running queued items.
63 -i --id Identity (default none)
64 -f --foreground Stay in the foreground (default background)
65 -a --all Handle queues for all local sites
66 (requires Stomp queue handler, status_network setup)
67 -t --threads=<n> Spawn <n> processing threads (default $threads)
72 require_once INSTALLDIR.'/scripts/commandline.inc';
74 require_once(INSTALLDIR.'/lib/daemon.php');
75 require_once(INSTALLDIR.'/classes/Queue_item.php');
76 require_once(INSTALLDIR.'/classes/Notice.php');
79 * Queue handling daemon...
81 * The queue daemon by default launches in the background, at which point
82 * it'll pass control to the configured QueueManager class to poll for updates.
84 * We can then pass individual items through the QueueHandler subclasses
87 class QueueDaemon extends SpawningDaemon
89 protected $allsites = false;
91 function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
93 parent::__construct($id, $daemonize, $threads);
94 $this->allsites = $allsites;
98 * Setup and start of run loop for this queue handler as a daemon.
99 * Most of the heavy lifting is passed on to the QueueManager's service()
100 * method, which passes control on to the QueueHandler's handle()
101 * method for each item that comes in on the queue.
103 * @return boolean true on success, false on failure
107 $this->log(LOG_INFO, 'checking for queued notices');
109 $master = new QueueMaster($this->get_id(), $this->processManager());
110 $master->init($this->allsites);
113 } catch (Exception $e) {
114 common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
115 str_replace("\n", " ", $e->getTraceAsString()));
116 return self::EXIT_ERR;
119 $this->log(LOG_INFO, 'finished servicing the queue');
121 $this->log(LOG_INFO, 'terminating normally');
123 return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
127 class QueueMaster extends IoMaster
129 protected $processManager;
131 function __construct($id, $processManager)
133 parent::__construct($id);
134 $this->processManager = $processManager;
138 * Initialize IoManagers which are appropriate to this instance.
140 function initManagers()
143 if (Event::handle('StartQueueDaemonIoManagers', array(&$managers))) {
144 $qm = QueueManager::get();
145 $qm->setActiveGroup('main');
147 $managers[] = $this->processManager;
149 Event::handle('EndQueueDaemonIoManagers', array(&$managers));
151 foreach ($managers as $manager) {
152 $this->instantiate($manager);
157 if (have_option('i')) {
158 $id = get_option_value('i');
159 } else if (have_option('--id')) {
160 $id = get_option_value('--id');
161 } else if (count($args) > 0) {
167 if (have_option('t')) {
168 $threads = intval(get_option_value('t'));
169 } else if (have_option('--threads')) {
170 $threads = intval(get_option_value('--threads'));
172 //If there is no argument for number of threads
173 //Try reading a config option for the number
174 $threads = common_config('queue','threads');
177 $threads = getProcessorCount();
180 common_log(LOG_INFO, sprintf('Launching QueueDaemon background process with %1$d threads.', $threads));
182 $daemonize = !(have_option('f') || have_option('--foreground'));
183 $all = have_option('a') || have_option('--all');
185 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);