3 * StatusNet, the distributed open-source microblogging tool
5 * Base class for making daemons that can do several tasks in parallel.
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
38 * Daemon able to spawn multiple child processes to do work in parallel
42 * @author Zach Copley <zach@status.net>
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class ParallelizingDaemon extends Daemon
50 private $_children = array();
51 private $_interval = 0; // seconds
52 private $_max_children = 0; // maximum number of children
53 private $_debug = false;
58 * @param string $id the name/id of this daemon
59 * @param int $interval sleep this long before doing everything again
60 * @param int $max_children maximum number of child processes at a time
61 * @param boolean $debug debug output flag
67 function __construct($id = null, $interval = 60, $max_children = 2,
70 parent::__construct(true); // daemonize
72 $this->_interval = $interval;
73 $this->_max_children = $max_children;
74 $this->_debug = $debug;
89 if (isset($this->_debug)) {
90 echo $this->name() . " - Debugging output enabled.\n";
95 $objects = $this->getObjects();
97 foreach ($objects as $o) {
99 // Fork a child for each object
104 die ($this->name() . ' - Couldn\'t fork!');
111 if (isset($this->_debug)) {
113 " - Forked new child - pid $pid.\n";
117 $this->_children[] = $pid;
123 // Do something with each object
125 $this->childTask($o);
130 // Remove child from ps list as it finishes
132 while (($c = pcntl_wait($status, WNOHANG OR WUNTRACED)) > 0) {
134 if (isset($this->_debug)) {
135 echo $this->name() . " - Child $c finished.\n";
138 $this->removePs($this->_children, $c);
141 // Wait! We have too many damn kids.
143 if (sizeof($this->_children) >= $this->_max_children) {
145 if (isset($this->_debug)) {
146 echo $this->name() . " - Too many children. Waiting...\n";
149 if (($c = pcntl_wait($status, WUNTRACED)) > 0) {
151 if (isset($this->_debug)) {
153 " - Finished waiting for child $c.\n";
156 $this->removePs($this->_children, $c);
161 // Remove all children from the process list before restarting
162 while (($c = pcntl_wait($status, WUNTRACED)) > 0) {
164 if (isset($this->_debug)) {
165 echo $this->name() . " - Child $c finished.\n";
168 $this->removePs($this->_children, $c);
173 if (isset($this->_debug)) {
174 echo $this->name() . ' - Waiting ' . $this->_interval .
175 " secs before running again.\n";
178 if ($this->_interval > 0) {
179 sleep($this->_interval);
186 * Remove a child process from the list of children
188 * @param array &$plist array of processes
189 * @param int $ps process id
194 function removePs(&$plist, $ps)
196 for ($i = 0; $i < sizeof($plist); $i++) {
197 if ($plist[$i] == $ps) {
199 $plist = array_values($plist);
206 * Get a list of objects to work on in parallel
208 * @return array An array of objects to work on
211 function getObjects()
213 die('Implement ParallelizingDaemon::getObjects().');
217 * Do something with each object in parallel
219 * @param mixed $object data to work on
224 function childTask($object)
226 die("Implement ParallelizingDaemon::childTask($object).");