parallelizingdaemon.php 6.07 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Base class for making daemons that can do several tasks in parallel.
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Daemon
23
 * @package   StatusNet
24 25
 * @author    Zach Copley <zach@status.net>
 * @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/
29 30
 */

31
if (!defined('STATUSNET') && !defined('LACONICA')) {
32 33 34 35 36 37 38 39 40
    exit(1);
}

declare(ticks = 1);

/**
 * Daemon able to spawn multiple child processes to do work in parallel
 *
 * @category Daemon
41
 * @package  StatusNet
42 43
 * @author   Zach Copley <zach@status.net>
 * @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/
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 */

class ParallelizingDaemon extends Daemon
{
    private $_children     = array();
    private $_interval     = 0; // seconds
    private $_max_children = 0; // maximum number of children
    private $_debug        = false;

    /**
     *  Constructor
     *
     * @param string  $id           the name/id of this daemon
     * @param int     $interval     sleep this long before doing everything again
     * @param int     $max_children maximum number of child processes at a time
     * @param boolean $debug        debug output flag
     *
     * @return void
     *
     **/

    function __construct($id = null, $interval = 60, $max_children = 2,
                         $debug = null)
    {
        parent::__construct(true); // daemonize

        $this->_interval     = $interval;
        $this->_max_children = $max_children;
        $this->_debug        = $debug;

        if (isset($id)) {
            $this->set_id($id);
        }
    }

    /**
     * Run the daemon
     *
     * @return void
     */

    function run()
    {
        if (isset($this->_debug)) {
90
            echo $this->name() . " - Debugging output enabled.\n";
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        }

        do {

            $objects = $this->getObjects();

            foreach ($objects as $o) {

                // Fork a child for each object

                $pid = pcntl_fork();

                if ($pid == -1) {
                    die ($this->name() . ' - Couldn\'t fork!');
                }

                if ($pid) {

                    // Parent
110

111 112
                    if (isset($this->_debug)) {
                        echo $this->name() .
113
                          " - Forked new child - pid $pid.\n";
114 115 116 117 118 119 120 121 122 123

                    }

                    $this->_children[] = $pid;

                } else {

                    // Child

                    // Do something with each object
124

125 126 127 128 129 130
                    $this->childTask($o);

                    exit();
                }

                // Remove child from ps list as it finishes
131

132 133 134
                while (($c = pcntl_wait($status, WNOHANG OR WUNTRACED)) > 0) {

                    if (isset($this->_debug)) {
135
                        echo $this->name() . " - Child $c finished.\n";
136 137 138 139 140 141
                    }

                    $this->removePs($this->_children, $c);
                }

                // Wait! We have too many damn kids.
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
                if (sizeof($this->_children) >= $this->_max_children) {

                    if (isset($this->_debug)) {
                        echo $this->name() . " - Too many children. Waiting...\n";
                    }

                    if (($c = pcntl_wait($status, WUNTRACED)) > 0) {

                        if (isset($this->_debug)) {
                            echo $this->name() .
                              " - Finished waiting for child $c.\n";
                        }

                        $this->removePs($this->_children, $c);
                    }
                }
            }

            // Remove all children from the process list before restarting
            while (($c = pcntl_wait($status, WUNTRACED)) > 0) {

                if (isset($this->_debug)) {
165
                    echo $this->name() . " - Child $c finished.\n";
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                }

                $this->removePs($this->_children, $c);
            }

            // Rest for a bit

            if (isset($this->_debug)) {
                echo $this->name() . ' - Waiting ' . $this->_interval .
                  " secs before running again.\n";
            }

            if ($this->_interval > 0) {
                sleep($this->_interval);
            }

        } while (true);
    }

    /**
     * Remove a child process from the list of children
     *
     * @param array &$plist array of processes
     * @param int   $ps     process id
     *
     * @return void
     */

    function removePs(&$plist, $ps)
    {
        for ($i = 0; $i < sizeof($plist); $i++) {
            if ($plist[$i] == $ps) {
                unset($plist[$i]);
                $plist = array_values($plist);
                break;
            }
        }
    }

    /**
     * Get a list of objects to work on in parallel
     *
     * @return array An array of objects to work on
     */

    function getObjects()
    {
        die('Implement ParallelizingDaemon::getObjects().');
    }

    /**
     * Do something with each object in parallel
     *
     * @param mixed $object data to work on
     *
     * @return void
     */

    function childTask($object)
    {
        die("Implement ParallelizingDaemon::childTask($object).");
    }

}