Page MenuHome GnuPG

No OneTemporary

diff --git a/scripts/daemon/exec/exec_daemon.php b/scripts/daemon/exec/exec_daemon.php
index 4435ee5..addbf49 100755
--- a/scripts/daemon/exec/exec_daemon.php
+++ b/scripts/daemon/exec/exec_daemon.php
@@ -1,56 +1,59 @@
#!/usr/bin/env php
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-$sid = posix_setsid();
-if ($sid <= 0) {
- throw new Exception('Failed to create new process session!');
+if (!posix_isatty(STDOUT)) {
+ $sid = posix_setsid();
+ if ($sid <= 0) {
+ throw new Exception('Failed to create new process session!');
+ }
}
+
$root = dirname(dirname(dirname(dirname(__FILE__))));
require_once $root.'/scripts/__init_script__.php';
$load = array();
$len = count($argv);
for ($ii = 1; $ii < $len; $ii++) {
$value = $argv[$ii];
$matches = null;
if ($value == '--') {
break;
} else if (preg_match('/^--load-phutil-library=(.*)$/', $value, $matches)) {
$load[] = $matches[1];
unset($argv[$ii]);
}
}
$argv = array_values($argv);
if ($load) {
phutil_require_module('phutil', 'filesystem');
foreach ($load as $library) {
$library = Filesystem::resolvePath($library);
phutil_load_library($library);
}
}
phutil_require_module('phutil', 'symbols');
$daemon = $argv[1];
PhutilSymbolLoader::loadClass($daemon);
$daemon = newv($daemon, array($argv));
$daemon->execute();
diff --git a/src/daemon/base/PhutilDaemon.php b/src/daemon/base/PhutilDaemon.php
index 12469b9..1579297 100644
--- a/src/daemon/base/PhutilDaemon.php
+++ b/src/daemon/base/PhutilDaemon.php
@@ -1,68 +1,68 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Scaffolding for implementing robust background processing scripts.
*
* @group daemon
*/
-class PhutilDaemon {
+abstract class PhutilDaemon {
private $argv;
private static $sighandlerInstalled;
final public function __construct(array $argv) {
declare(ticks = 1);
$this->argv = $argv;
if (!self::$sighandlerInstalled) {
self::$sighandlerInstalled = true;
- pcntl_signal(SIGINT, __CLASS__.'::__exitOnSignal');
- pcntl_signal(SIGTERM, __CLASS__.'::__exitOnSignal');
+ pcntl_signal(SIGINT, __CLASS__.'::exitOnSignal');
+ pcntl_signal(SIGTERM, __CLASS__.'::exitOnSignal');
}
}
final public function stillWorking() {
posix_kill(posix_getppid(), SIGUSR1);
}
- public static function __exitOnSignal($signo) {
+ public static function exitOnSignal($signo) {
// Normally, PHP doesn't invoke destructors when existing in response to
// a signal. This forces it to do so, so we have a fighting chance of
// releasing any locks on our way out.
exit(128 + $signo);
}
final protected function getArgv() {
return $this->argv;
}
final public function execute() {
$this->willRun();
$this->run();
}
protected function willRun() {
}
abstract protected function run();
}
diff --git a/src/daemon/overseer/PhutilDaemonOverseer.php b/src/daemon/overseer/PhutilDaemonOverseer.php
index 15783d4..8314cbc 100644
--- a/src/daemon/overseer/PhutilDaemonOverseer.php
+++ b/src/daemon/overseer/PhutilDaemonOverseer.php
@@ -1,141 +1,141 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Oversees a daemon and restarts it if it fails.
*
* @group daemon
*/
class PhutilDaemonOverseer {
private $captureBufferSize = 65536;
private $deadline;
private $deadlineTimeout = 300;
private $restartDelay = 30;
private $killDelay = 3;
private $daemon;
private $argv;
private $childPID;
private $signaled;
private static $instance;
public function __construct($daemon, array $argv) {
$this->daemon = $daemon;
$this->argv = array_slice($argv, 1);
if (self::$instance) {
throw new Exception(
"You may not instantiate more than one Overseer per process.");
}
self::$instance = $this;
declare(ticks = 1);
pcntl_signal(SIGUSR1, array($this, 'didReceiveKeepaliveSignal'));
pcntl_signal(SIGINT, array($this, 'didReceiveTerminalSignal'));
pcntl_signal(SIGTERM, array($this, 'didReceiveTerminalSignal'));
}
public function run() {
$root = phutil_get_library_root('phutil');
$root = dirname($root);
$exec_daemon = $root.'/scripts/daemon/exec/exec_daemon.php';
$argv = $this->argv;
array_unshift($argv, $exec_daemon, $this->daemon);
foreach ($argv as $k => $arg) {
$argv[$k] = escapeshellarg($arg);
}
$command = 'exec '.implode(' ', $argv);
while (true) {
$this->logMessage('[INIT] Starting process.');
$future = new ExecFuture($command);
$future->setStdoutSizeLimit($this->captureBufferSize);
$future->setStderrSizeLimit($this->captureBufferSize);
$this->deadline = time() + $this->deadlineTimeout;
$future->isReady();
$this->childPID = $future->getPID();
do {
do {
// $memuse = number_format(memory_get_usage() / 1024, 1);
// $this->logMessage('[STAT] Memory Usage: '.$memuse.' KB');
// We need a shortish timeout here so we can run the tick handler
// frequently in order to process signals.
$result = $future->resolve(1);
if ($result !== null) {
list($err) = $result;
if ($err) {
- $this->logMessage('[FAIL] Process edited with error '.$err.'.');
+ $this->logMessage('[FAIL] Process exited with error '.$err.'.');
} else {
$this->logMessage('[DONE] Process exited successfully.');
}
break 2;
}
} while (time() < $this->deadline);
$this->logMessage('[HANG] Hang detected. Restarting process.');
$this->annihilateProcessGroup();
} while (false);
$this->logMessage('[WAIT] Waiting to restart process.');
sleep($this->restartDelay);
}
}
public function didReceiveKeepaliveSignal($signo) {
$this->deadline = time() + $this->deadlineTimeout;
}
public function didReceiveTerminalSignal($signo) {
if ($this->signaled) {
exit(128 + $signo);
}
echo "\n>>> Shutting down...\n";
$this->signaled = true;
$this->annihilateProcessGroup();
exit(128 + $signo);
}
private function logMessage($message) {
echo date('Y-m-d g:i:s A').' '.$message."\n";
}
private function annihilateProcessGroup() {
$pid = $this->childPID;
$pgid = posix_getpgid($pid);
if ($pid && $pgid) {
exec("kill -TERM -- -{$pgid}");
sleep($this->killDelay);
exec("kill -KILL -- -{$pgid}");
$this->childPID = null;
}
}
}
diff --git a/src/daemon/torture/base/PhutilTortureTestDaemon.php b/src/daemon/torture/base/PhutilTortureTestDaemon.php
index 99a409a..7748b72 100644
--- a/src/daemon/torture/base/PhutilTortureTestDaemon.php
+++ b/src/daemon/torture/base/PhutilTortureTestDaemon.php
@@ -1,26 +1,26 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Daemons which misbehave in specific ways.
*
* @group testcase
*/
-class PhutilTortureTestDaemon extends PhutilDaemon {
+abstract class PhutilTortureTestDaemon extends PhutilDaemon {
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Jan 20, 1:34 AM (1 d, 11 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
2a/7e/d05ff18702baf6290b954134cc48

Event Timeline