]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/queuedaemon.php
Merge branch 'testing'
[quix0rs-gnu-social.git] / scripts / queuedaemon.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'fi:at:';
24 $longoptions = array('id=', 'foreground', 'all', 'threads=');
25
26 /**
27  * Attempts to get a count of the processors available on the current system
28  * to fan out multiple threads.
29  *
30  * Recognizes Linux and Mac OS X; others will return default of 1.
31  *
32  * @fixme move this to SpawningDaemon, but to get the default val for help
33  *        text we seem to need it before loading infrastructure
34  * @return intval
35  */
36 function getProcessorCount()
37 {
38     $cpus = 0;
39     switch (PHP_OS) {
40     case 'Linux':
41         $cpuinfo = file('/proc/cpuinfo');
42         foreach (file('/proc/cpuinfo') as $line) {
43             if (preg_match('/^processor\s+:\s+(\d+)\s?$/', $line)) {
44                 $cpus++;
45             }
46         }
47         break;
48     case 'Darwin':
49         $cpus = intval(shell_exec("/usr/sbin/sysctl -n hw.ncpu 2>/dev/null"));
50         break;
51     }
52     if ($cpus) {
53         return $cpus;
54     }
55     return 1;
56 }
57
58 $threads = getProcessorCount();
59 $helptext = <<<END_OF_QUEUE_HELP
60 Daemon script for running queued items.
61
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)
67
68
69 END_OF_QUEUE_HELP;
70
71 require_once INSTALLDIR.'/scripts/commandline.inc';
72
73 require_once(INSTALLDIR.'/lib/daemon.php');
74 require_once(INSTALLDIR.'/classes/Queue_item.php');
75 require_once(INSTALLDIR.'/classes/Notice.php');
76
77 define('CLAIM_TIMEOUT', 1200);
78
79 /**
80  * Queue handling daemon...
81  *
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.
84  *
85  * We can then pass individual items through the QueueHandler subclasses
86  * they belong to.
87  */
88 class QueueDaemon extends SpawningDaemon
89 {
90     protected $allsites = false;
91
92     function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
93     {
94         parent::__construct($id, $daemonize, $threads);
95         $this->all = $allsites;
96     }
97
98     /**
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.
103      *
104      * @return boolean true on success, false on failure
105      */
106     function runThread()
107     {
108         $this->log(LOG_INFO, 'checking for queued notices');
109
110         $master = new QueueMaster($this->get_id());
111         $master->init($this->all);
112         try {
113             $master->service();
114         } catch (Exception $e) {
115             common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
116                 str_replace("\n", " ", $e->getTraceAsString()));
117             return self::EXIT_ERR;
118         }
119
120         $this->log(LOG_INFO, 'finished servicing the queue');
121
122         $this->log(LOG_INFO, 'terminating normally');
123
124         return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
125     }
126 }
127
128 class QueueMaster extends IoMaster
129 {
130     /**
131      * Initialize IoManagers for the currently configured site
132      * which are appropriate to this instance.
133      */
134     function initManagers()
135     {
136         $classes = array();
137         if (Event::handle('StartQueueDaemonIoManagers', array(&$classes))) {
138             $classes[] = 'QueueManager';
139         }
140         Event::handle('EndQueueDaemonIoManagers', array(&$classes));
141
142         foreach ($classes as $class) {
143             $this->instantiate($class);
144         }
145     }
146 }
147
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) {
153     $id = $args[0];
154 } else {
155     $id = null;
156 }
157
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'));
162 } else {
163     $threads = 0;
164 }
165 if (!$threads) {
166     $threads = getProcessorCount();
167 }
168
169 $daemonize = !(have_option('f') || have_option('--foreground'));
170 $all = have_option('a') || have_option('--all');
171
172 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);
173 $daemon->runOnce();
174