3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Base class for daemon that can launch one or more processing threads,
22 * respawning them if they exit.
24 * This is mainly intended for indefinite workloads such as monitoring
25 * a queue or maintaining an IM channel.
27 * Child classes should implement the
29 * We can then pass individual items through the QueueHandler subclasses
30 * they belong to. We additionally can handle queues for multiple sites.
32 * @package QueueHandler
33 * @author Brion Vibber <brion@status.net>
35 abstract class SpawningDaemon extends Daemon
41 const EXIT_SHUTDOWN = 100;
42 const EXIT_RESTART = 101;
44 function __construct($id=null, $daemonize=true, $threads=1)
46 parent::__construct($daemonize);
51 $this->threads = $threads;
55 * Perform some actual work!
57 * @return int exit code; use self::EXIT_SHUTDOWN to request not to respawn.
59 public abstract function runThread();
62 * Spawn one or more background processes and let them start running.
63 * Each individual process will execute whatever's in the runThread()
64 * method, which should be overridden.
66 * Child processes will be automatically respawned when they exit.
68 * @todo possibly allow for not respawning on "normal" exits...
69 * though ParallelizingDaemon is probably better for workloads
70 * that have forseeable endpoints.
75 for ($i = 1; $i <= $this->threads; $i++) {
78 $this->log(LOG_ERROR, "Couldn't fork for thread $i; aborting\n");
80 } else if ($pid == 0) {
81 $this->initAndRunChild($i);
83 $this->log(LOG_INFO, "Spawned thread $i as pid $pid");
86 sleep(common_config('queue', 'spawndelay'));
89 $this->log(LOG_INFO, "Waiting for children to complete.");
90 while (count($children) > 0) {
92 $pid = pcntl_wait($status);
93 if ($pid > 0 && pcntl_wifexited($status)) {
94 $exitCode = pcntl_wexitstatus($status);
96 $i = array_search($pid, $children);
98 $this->log(LOG_ERR, "Unrecognized child pid $pid exited with status $exitCode");
101 unset($children[$i]);
103 if ($this->shouldRespawn($exitCode)) {
104 $this->log(LOG_INFO, "Thread $i pid $pid exited with status $exitCode; respawing.");
108 $this->log(LOG_ERROR, "Couldn't fork to respawn thread $i; aborting thread.\n");
109 } else if ($pid == 0) {
110 $this->initAndRunChild($i);
112 $this->log(LOG_INFO, "Respawned thread $i as pid $pid");
113 $children[$i] = $pid;
115 sleep(common_config('queue', 'spawndelay'));
117 $this->log(LOG_INFO, "Thread $i pid $pid exited with status $exitCode; closing out thread.");
121 $this->log(LOG_INFO, "All child processes complete.");
126 * Determine whether to respawn an exited subprocess based on its exit code.
127 * Otherwise we'll respawn all exits by default.
129 * @param int $exitCode
130 * @return boolean true to respawn
132 protected function shouldRespawn($exitCode)
134 if ($exitCode == self::EXIT_SHUTDOWN) {
135 // Thread requested a clean shutdown.
138 // Otherwise we should always respawn!
144 * Initialize things for a fresh thread, call runThread(), and
145 * exit at completion with appropriate return value.
147 protected function initAndRunChild($thread)
149 $this->set_id($this->get_id() . "." . $thread);
151 $exitCode = $this->runThread();
156 * Reconnect to the database for each child process,
157 * or they'll get very confused trying to use the
160 protected function resetDb()
162 // @fixme do we need to explicitly open the db too
163 // or is this implied?
164 global $_DB_DATAOBJECT;
165 unset($_DB_DATAOBJECT['CONNECTIONS']);
167 // Reconnect main memcached, or threads will stomp on
168 // each other and corrupt their requests.
169 $cache = common_memcache();
174 // Also reconnect memcached for status_network table.
175 if (!empty(Status_network::$cache)) {
176 Status_network::$cache->close();
177 Status_network::$cache = null;
181 function log($level, $msg)
183 common_log($level, get_class($this) . ' ('. $this->get_id() .'): '.$msg);
188 return strtolower(get_class($this).'.'.$this->get_id());