3 * Laconica, 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@controlyourself.ca>
25 * @author Evan Prodromou <evan@controlyourself.ca>
26 * @copyright 2009 Control Yourself, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://laconi.ca/
31 if (!defined('LACONICA')) {
38 * Daemon able to spawn multiple child processes to do work in parallel
42 * @author Zach Copley <zach@controlyourself.ca>
43 * @author Evan Prodromou <evan@controlyourself.ca>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://laconi.ca/
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!');
110 if (isset($this->_debug)) {
112 " (parent) forked new child - pid $pid.\n";
116 $this->_children[] = $pid;
122 // Do something with each object
123 $this->childTask($o);
128 // Remove child from ps list as it finishes
129 while (($c = pcntl_wait($status, WNOHANG OR WUNTRACED)) > 0) {
131 if (isset($this->_debug)) {
132 echo $this->name() . " child $c finished.\n";
135 $this->removePs($this->_children, $c);
138 // Wait! We have too many damn kids.
139 if (sizeof($this->_children) >= $this->_max_children) {
141 if (isset($this->_debug)) {
142 echo $this->name() . " - Too many children. Waiting...\n";
145 if (($c = pcntl_wait($status, WUNTRACED)) > 0) {
147 if (isset($this->_debug)) {
149 " - Finished waiting for child $c.\n";
152 $this->removePs($this->_children, $c);
157 // Remove all children from the process list before restarting
158 while (($c = pcntl_wait($status, WUNTRACED)) > 0) {
160 if (isset($this->_debug)) {
161 echo $this->name() . " child $c finished.\n";
164 $this->removePs($this->_children, $c);
169 if (isset($this->_debug)) {
170 echo $this->name() . ' - Waiting ' . $this->_interval .
171 " secs before running again.\n";
174 if ($this->_interval > 0) {
175 sleep($this->_interval);
182 * Remove a child process from the list of children
184 * @param array &$plist array of processes
185 * @param int $ps process id
190 function removePs(&$plist, $ps)
192 for ($i = 0; $i < sizeof($plist); $i++) {
193 if ($plist[$i] == $ps) {
195 $plist = array_values($plist);
202 * Get a list of objects to work on in parallel
204 * @return array An array of objects to work on
207 function getObjects()
209 die('Implement ParallelizingDaemon::getObjects().');
213 * Do something with each object in parallel
215 * @param mixed $object data to work on
220 function childTask($object)
222 die("Implement ParallelizingDaemon::childTask($object).");