]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/queuedaemon.php
Make attachment fit better in notice: drop text and link
[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', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
23
24 $shortoptions = 'fi:at:';
25 $longoptions = array('id=', 'foreground', 'all', 'threads=');
26
27 /**
28  * Attempts to get a count of the processors available on the current system
29  * to fan out multiple threads.
30  *
31  * Recognizes Linux and Mac OS X; others will return default of 1.
32  *
33  * @fixme move this to SpawningDaemon, but to get the default val for help
34  *        text we seem to need it before loading infrastructure
35  * @return intval
36  */
37 function getProcessorCount()
38 {
39     $cpus = 0;
40     switch (PHP_OS) {
41     case 'Linux':
42         $cpuinfo = file('/proc/cpuinfo');
43         foreach (file('/proc/cpuinfo') as $line) {
44             if (preg_match('/^processor\s+:\s+(\d+)\s?$/', $line)) {
45                 $cpus++;
46             }
47         }
48         break;
49     case 'Darwin':
50         $cpus = intval(shell_exec("/usr/sbin/sysctl -n hw.ncpu 2>/dev/null"));
51         break;
52     }
53     if ($cpus) {
54         return $cpus;
55     }
56     return 1;
57 }
58
59 $threads = getProcessorCount();
60 $helptext = <<<END_OF_QUEUE_HELP
61 Daemon script for running queued items.
62
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)
68
69
70 END_OF_QUEUE_HELP;
71
72 require_once INSTALLDIR.'/scripts/commandline.inc';
73
74 require_once(INSTALLDIR.'/lib/daemon.php');
75 require_once(INSTALLDIR.'/classes/Queue_item.php');
76 require_once(INSTALLDIR.'/classes/Notice.php');
77
78 /**
79  * Queue handling daemon...
80  *
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.
83  *
84  * We can then pass individual items through the QueueHandler subclasses
85  * they belong to.
86  */
87 class QueueDaemon extends SpawningDaemon
88 {
89     protected $allsites = false;
90
91     function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
92     {
93         parent::__construct($id, $daemonize, $threads);
94         $this->allsites = $allsites;
95     }
96
97     /**
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.
102      *
103      * @return boolean true on success, false on failure
104      */
105     function runThread()
106     {
107         $this->log(LOG_INFO, 'checking for queued notices');
108
109         $master = new QueueMaster($this->get_id(), $this->processManager());
110         $master->init($this->allsites);
111         try {
112             $master->service();
113         } catch (Exception $e) {
114             common_log(LOG_ERR, "Unhandled exception: " . $e->getMessage() . ' ' .
115                 str_replace("\n", " ", $e->getTraceAsString()));
116             return self::EXIT_ERR;
117         }
118
119         $this->log(LOG_INFO, 'finished servicing the queue');
120
121         $this->log(LOG_INFO, 'terminating normally');
122
123         return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
124     }
125 }
126
127 class QueueMaster extends IoMaster
128 {
129     protected $processManager;
130
131     function __construct($id, $processManager)
132     {
133         parent::__construct($id);
134         $this->processManager = $processManager;
135     }
136
137     /**
138      * Initialize IoManagers which are appropriate to this instance.
139      */
140     function initManagers()
141     {
142         $managers = array();
143         if (Event::handle('StartQueueDaemonIoManagers', array(&$managers))) {
144             $qm = QueueManager::get();
145             $qm->setActiveGroup('main');
146             $managers[] = $qm;
147             $managers[] = $this->processManager;
148         }
149         Event::handle('EndQueueDaemonIoManagers', array(&$managers));
150
151         foreach ($managers as $manager) {
152             $this->instantiate($manager);
153         }
154     }
155 }
156
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) {
162     $id = $args[0];
163 } else {
164     $id = null;
165 }
166
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'));
171 } else {
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');
175 }
176 if (!$threads) {
177     $threads = getProcessorCount();
178 }
179
180 common_log(LOG_INFO, sprintf('Launching QueueDaemon background process with %1$d threads.', $threads));
181
182 $daemonize = !(have_option('f') || have_option('--foreground'));
183 $all = have_option('a') || have_option('--all');
184
185 $daemon = new QueueDaemon($id, $daemonize, $threads, $all);
186 $daemon->runOnce();
187