summaryrefslogtreecommitdiff
path: root/lib/spawningdaemon.php
diff options
context:
space:
mode:
authorZach Copley <zach@status.net>2010-01-27 14:27:22 -0800
committerZach Copley <zach@status.net>2010-01-27 14:27:22 -0800
commit78079f34e273357d03ceee13269f9a388e66c4e3 (patch)
tree8f29f6fb7de5b6b6627a7eba3e16b86fa9b2ae1f /lib/spawningdaemon.php
parent656d95418c6d7f8b884c4c8af14ad6952032ace6 (diff)
parent2494d3fa25a44b3cacf85c594683675ae9e6d0cb (diff)
Merge branch 'testing' into -1.9.x
* testing: (130 commits) HTTP auth provided is evaluated even if it's not required Rename rc3to09.sql to rc3torc4.sql to avoid confusion if we add a last-minute change after this! Add new oauth tables and modifications to 'consumer' table for rc4 Centred leaderboard ad camelcase the uap param names move leaderboard to after the header Moved rectangle ad into aside and leaderboard to the right in header. Aligning wide skyscraper to the right instead of left CSS ids and classes fixed in UAPPlugin wrong height for rectangle in BlankAd Add the moved BlankAdPlugin make BlankAd dir and change to use a 1x1 image move BlankAdPlugin to its own dir Add BlankAdPlugin to test ad layout in different themes make uapplugin an abstract class move UAP plugin to core Lowercased switch cases in UAP Plugin Plugin for Universal Ad Package. Outputs four most widely used ad types. Add persistent:true property to Stomp messages so ActiveMQ doesn't decide to discard them even though persistence is enabled on the broker. :) (Thanks Aric!) quick fix: use common_path() on realtime update JS so it works with the new JS path code (will pull from main server for now) ... Conflicts: actions/apioauthaccesstoken.php actions/apioauthauthorize.php actions/apioauthrequesttoken.php actions/editapplication.php actions/newapplication.php lib/apiauth.php lib/queuemanager.php lib/router.php
Diffstat (limited to 'lib/spawningdaemon.php')
-rw-r--r--lib/spawningdaemon.php58
1 files changed, 44 insertions, 14 deletions
diff --git a/lib/spawningdaemon.php b/lib/spawningdaemon.php
index 8baefe88e..b1961d688 100644
--- a/lib/spawningdaemon.php
+++ b/lib/spawningdaemon.php
@@ -36,6 +36,11 @@ abstract class SpawningDaemon extends Daemon
{
protected $threads=1;
+ const EXIT_OK = 0;
+ const EXIT_ERR = 1;
+ const EXIT_SHUTDOWN = 100;
+ const EXIT_RESTART = 101;
+
function __construct($id=null, $daemonize=true, $threads=1)
{
parent::__construct($daemonize);
@@ -49,7 +54,7 @@ abstract class SpawningDaemon extends Daemon
/**
* Perform some actual work!
*
- * @return boolean true on success, false on failure
+ * @return int exit code; use self::EXIT_SHUTDOWN to request not to respawn.
*/
public abstract function runThread();
@@ -84,23 +89,30 @@ abstract class SpawningDaemon extends Daemon
while (count($children) > 0) {
$status = null;
$pid = pcntl_wait($status);
- if ($pid > 0) {
+ if ($pid > 0 && pcntl_wifexited($status)) {
+ $exitCode = pcntl_wexitstatus($status);
+
$i = array_search($pid, $children);
if ($i === false) {
- $this->log(LOG_ERR, "Unrecognized child pid $pid exited!");
+ $this->log(LOG_ERR, "Unrecognized child pid $pid exited with status $exitCode");
continue;
}
unset($children[$i]);
- $this->log(LOG_INFO, "Thread $i pid $pid exited.");
-
- $pid = pcntl_fork();
- if ($pid < 0) {
- $this->log(LOG_ERROR, "Couldn't fork to respawn thread $i; aborting thread.\n");
- } else if ($pid == 0) {
- $this->initAndRunChild($i);
+
+ if ($this->shouldRespawn($exitCode)) {
+ $this->log(LOG_INFO, "Thread $i pid $pid exited with status $exitCode; respawing.");
+
+ $pid = pcntl_fork();
+ if ($pid < 0) {
+ $this->log(LOG_ERROR, "Couldn't fork to respawn thread $i; aborting thread.\n");
+ } else if ($pid == 0) {
+ $this->initAndRunChild($i);
+ } else {
+ $this->log(LOG_INFO, "Respawned thread $i as pid $pid");
+ $children[$i] = $pid;
+ }
} else {
- $this->log(LOG_INFO, "Respawned thread $i as pid $pid");
- $children[$i] = $pid;
+ $this->log(LOG_INFO, "Thread $i pid $pid exited with status $exitCode; closing out thread.");
}
}
}
@@ -109,6 +121,24 @@ abstract class SpawningDaemon extends Daemon
}
/**
+ * Determine whether to respawn an exited subprocess based on its exit code.
+ * Otherwise we'll respawn all exits by default.
+ *
+ * @param int $exitCode
+ * @return boolean true to respawn
+ */
+ protected function shouldRespawn($exitCode)
+ {
+ if ($exitCode == self::EXIT_SHUTDOWN) {
+ // Thread requested a clean shutdown.
+ return false;
+ } else {
+ // Otherwise we should always respawn!
+ return true;
+ }
+ }
+
+ /**
* Initialize things for a fresh thread, call runThread(), and
* exit at completion with appropriate return value.
*/
@@ -116,8 +146,8 @@ abstract class SpawningDaemon extends Daemon
{
$this->set_id($this->get_id() . "." . $thread);
$this->resetDb();
- $ok = $this->runThread();
- exit($ok ? 0 : 1);
+ $exitCode = $this->runThread();
+ exit($exitCode);
}
/**