Changeset View
Standalone View
src/daemon/overseer/PhutilDaemonOverseer.php
Show All 11 Lines | |||||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | ||||
* distributed under the License is distributed on an "AS IS" BASIS, | * distributed under the License is distributed on an "AS IS" BASIS, | ||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
* limitations under the License. | * limitations under the License. | ||||
*/ | */ | ||||
/** | /** | ||||
* Oversees a daemon and restarts it if it fails. | * Oversees a daemon and restarts it if it fails. | ||||
akopanev: Yes, comments are good!
Testing phabricator.... | |||||
* | * | ||||
* @group daemon | * @group daemon | ||||
*/ | */ | ||||
class PhutilDaemonOverseer { | class PhutilDaemonOverseer { | ||||
eepUnsubmitted Not Done Inline ActionsGood use of 'class' keyword eep: Good use of 'class' keyword | |||||
briany12Unsubmitted Not Done Inline Actionsinline comment. briany12: inline comment. | |||||
eepUnsubmitted Not Done Inline ActionsYep. Big fan. eep: Yep. Big fan. | |||||
test13254Unsubmitted Not Done Inline ActionsDown with the Bourgeoisie class system! test13254: Down with the Bourgeoisie class system! | |||||
private $captureBufferSize = 65536; | private $captureBufferSize = 65536; | ||||
private $deadline; | private $deadline; | ||||
private $deadlineTimeout = 86400; | private $deadlineTimeout = 86400; | ||||
dheerendraUnsubmitted Not Done Inline ActionsI added this comment dheerendra: I added this comment | |||||
private $restartDelay = 60; | private $restartDelay = 60; | ||||
private $killDelay = 3; | private $killDelay = 3; | ||||
dheerendraUnsubmitted Not Done Inline ActionsMy comment dheerendra: My comment | |||||
private $daemon; | private $daemon; | ||||
private $argv; | private $argv; | ||||
_bhaisaabUnsubmitted Not Done Inline Actionsthis works, why private, mutable states oh! _bhaisaab: this works, why private, mutable states oh! | |||||
dheerendraUnsubmitted Not Done Inline ActionsRe comment dheerendra: Re comment | |||||
private $childPID; | private $childPID; | ||||
WouterSioenUnsubmitted Not Done Inline Actions? WouterSioen: ? | |||||
private $signaled; | private $signaled; | ||||
a.veretennikovUnsubmitted Not Done Inline ActionsHello world! :) a.veretennikov: Hello world! :) | |||||
private static $instance; | private static $instance; | ||||
private $traceMode; | private $traceMode; | ||||
private $traceMemory; | private $traceMemory; | ||||
private $daemonize; | private $daemonize; | ||||
lasseespeholtUnsubmitted Not Done Inline ActionsFoobar lasseespeholt: Foobar | |||||
private $phddir; | private $phddir; | ||||
private $conduit; | |||||
ganesanramkumarUnsubmitted Not Done Inline Actionstest ganesanramkumar: test | |||||
ganesanramkumarUnsubmitted Not Done Inline Actionstest1 ganesanramkumar: test1 | |||||
sunzhen1991Unsubmitted Not Done Inline Actionstest11 sunzhen1991: test11 | |||||
dheerendraUnsubmitted Not Done Inline ActionsI can reply too!!!!!! dheerendra: I can reply too!!!!!! | |||||
private $conduitURI; | private $conduitURI; | ||||
dheerendraUnsubmitted Not Done Inline Actions@sunzhen How can I repy you dheerendra: @sunzhen How can I repy you | |||||
public function __construct($daemon, array $argv) { | public function __construct($daemon, array $argv) { | ||||
$this->daemon = $daemon; | $this->daemon = $daemon; | ||||
$original_argv = $argv; | $original_argv = $argv; | ||||
$len = count($argv); | $len = count($argv); | ||||
for ($ii = 1; $ii < $len; $ii++) { | for ($ii = 1; $ii < $len; $ii++) { | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | public function __construct($daemon, array $argv) { | ||||
declare(ticks = 1); | declare(ticks = 1); | ||||
pcntl_signal(SIGUSR1, array($this, 'didReceiveKeepaliveSignal')); | pcntl_signal(SIGUSR1, array($this, 'didReceiveKeepaliveSignal')); | ||||
pcntl_signal(SIGINT, array($this, 'didReceiveTerminalSignal')); | pcntl_signal(SIGINT, array($this, 'didReceiveTerminalSignal')); | ||||
pcntl_signal(SIGTERM, array($this, 'didReceiveTerminalSignal')); | pcntl_signal(SIGTERM, array($this, 'didReceiveTerminalSignal')); | ||||
} | } | ||||
public function run() { | public function run() { | ||||
lukianovvaUnsubmitted Not Done Inline ActionsИ придраться-то не к чему :) lukianovva: И придраться-то не к чему :) | |||||
if (!$this->traceMode) { | if (!$this->traceMode) { | ||||
echo "Running daemon '{$this->daemon}' silently. Use '--trace' to ". | echo "Running daemon '{$this->daemon}' silently. Use '--trace' to ". | ||||
"produce debugging output.\n"; | "produce debugging output.\n"; | ||||
} | } | ||||
$root = phutil_get_library_root('phutil'); | $root = phutil_get_library_root('phutil'); | ||||
shaneduffyUnsubmitted Not Done Inline ActionsTihsihsidhs shaneduffy: Tihsihsidhs
| |||||
shaneduffyUnsubmitted Not Done Inline Actionstesting shaneduffy: testing | |||||
$root = dirname($root); | $root = dirname($root); | ||||
sharatjyUnsubmitted Not Done Inline Actionsfswfksjflksdjf sharatjy: fswfksjflksdjf | |||||
adil.atilganUnsubmitted Not Done Inline ActionsSeems like OK adil.atilgan: Seems like OK | |||||
$exec_dir = $root.'/scripts/daemon/exec/'; | $exec_dir = $root.'/scripts/daemon/exec/'; | ||||
satbirjhutiUnsubmitted Not Done Inline ActionsMUCH ROOT! SUCH COMMENT! satbirjhuti: MUCH ROOT! SUCH COMMENT! | |||||
// NOTE: PHP implements proc_open() by running 'sh -c'. On most systems this | |||||
// is bash, but on Ubuntu it's dash. When you proc_open() using bash, you | |||||
// get one new process (the command you ran). When you proc_open() using | |||||
113madixing2Unsubmitted Not Done Inline Actionsggg 113madixing2: ggg | |||||
// dash, you get two new processes: the command you ran and a parent | |||||
// "dash -c" (or "sh -c") process. This means that the child process's PID | |||||
// is actually the 'dash' PID, not the command's PID. To avoid this, use | |||||
// 'exec' to replace the shell process with the real process; without this, | |||||
taligahackUnsubmitted Not Done Inline Actionscomment for a comment? taligahack: **//comment for a comment?//** | |||||
tomlinsonryanUnsubmitted Not Done Inline Actionstest tomlinsonryan: test | |||||
puneetUnsubmitted Not Done Inline Actionscomment puneet: comment
| |||||
puneetUnsubmitted Not Done Inline Actionstest dsflksadjflkdsajflkdsajfldsajfldsafds puneet: test dsflksadjflkdsajflkdsajfldsajfldsafds | |||||
manakoUnsubmitted Not Done Inline Actionsyour comment is extremely helpful manako: your comment is extremely helpful | |||||
// the child will call posix_getppid(), be given the pid of the 'sh -c' | |||||
// process, and send it SIGUSR1 to keepalive which will terminate it | |||||
// immediately. We also won't be able to do process group management because | |||||
// the shell process won't properly posix_setsid() so the pgid of the child | |||||
// won't be meaningful. | |||||
$exec_daemon = './exec_daemon.php'; | $exec_daemon = './exec_daemon.php'; | ||||
$argv = $this->argv; | $argv = $this->argv; | ||||
array_unshift($argv, $exec_daemon, $this->daemon); | array_unshift($argv, 'exec', $exec_daemon, $this->daemon); | ||||
spanitzUnsubmitted Not Done Inline ActionsClosing parenthesis of a multi-line function call must be on a line by itself (at line 89) spanitz: Closing parenthesis of a multi-line function call must be on a line by itself (at line 89) | |||||
foreach ($argv as $k => $arg) { | foreach ($argv as $k => $arg) { | ||||
$argv[$k] = escapeshellarg($arg); | $argv[$k] = escapeshellarg($arg); | ||||
suwandi.cahyadiUnsubmitted Not Done Inline Actionstesting suwandi.cahyadi: testing | |||||
akopanevUnsubmitted Not Done Inline Actionstesting... akopanev: testing... | |||||
feynmanUnsubmitted Not Done Inline ActionsThis is a testing annotation feynman: This is a testing annotation | |||||
w31rd0Unsubmitted Not Done Inline Actionstest reply w31rd0: test reply | |||||
w31rd0Unsubmitted Not Done Inline Actionsreply to test reply w31rd0: reply to test reply | |||||
} | } | ||||
$command = implode(' ', $argv); | $command = implode(' ', $argv); | ||||
while (true) { | while (true) { | ||||
$this->logMessage('INIT', 'Starting process.'); | $this->logMessage('INIT', 'Starting process.'); | ||||
BYKUnsubmitted Not Done Inline ActionsAnother test BYK: Another test | |||||
ramkUnsubmitted Not Done Inline Actionstest ramk: test | |||||
suwandi.cahyadiUnsubmitted Not Done Inline Actionsreplying suwandi.cahyadi: replying
| |||||
galetzka.michaelUnsubmitted Not Done Inline Actionstest reply galetzka.michael: test reply | |||||
$future = new ExecFuture($command); | $future = new ExecFuture($command); | ||||
$future->setCWD($exec_dir); | $future->setCWD($exec_dir); | ||||
sharatjyUnsubmitted Not Done Inline Actionskkk sharatjy: kkk | |||||
$future->setStdoutSizeLimit($this->captureBufferSize); | $future->setStdoutSizeLimit($this->captureBufferSize); | ||||
$future->setStderrSizeLimit($this->captureBufferSize); | $future->setStderrSizeLimit($this->captureBufferSize); | ||||
$this->deadline = time() + $this->deadlineTimeout; | $this->deadline = time() + $this->deadlineTimeout; | ||||
jackdmfUnsubmitted Not Done Inline ActionsI like the idea of having the future executed... jackdmf: I like the idea of having the future executed... | |||||
$future->isReady(); | $future->isReady(); | ||||
$this->childPID = $future->getPID(); | $this->childPID = $future->getPID(); | ||||
do { | do { | ||||
do { | do { | ||||
if ($this->traceMemory) { | if ($this->traceMemory) { | ||||
cmodienUnsubmitted Not Done Inline Actionstoo deep cmodien: too deep | |||||
$memuse = number_format(memory_get_usage() / 1024, 1); | $memuse = number_format(memory_get_usage() / 1024, 1); | ||||
$this->logMessage('RAMS', 'Overseer Memory Usage: '.$memuse.' KB'); | $this->logMessage('RAMS', 'Overseer Memory Usage: '.$memuse.' KB'); | ||||
} | } | ||||
fabianbuchUnsubmitted Not Done Inline Actions
fabianbuch: * foo
* bar | |||||
// We need a shortish timeout here so we can run the tick handler | // We need a shortish timeout here so we can run the tick handler | ||||
// frequently in order to process signals. | // frequently in order to process signals. | ||||
$result = $future->resolve(1); | $result = $future->resolve(1); | ||||
RadeoslawPLUnsubmitted Not Done Inline ActionsLast line test RadeoslawPL: Last line test | |||||
testlegUnsubmitted Not Done Inline ActionsI expect you also mean foo and bar as fabian suggested above... It's definitely safer to use foo but if you want it to run fast and you're not worried by the occasional fooble meltdown then you can use bar here too. testleg: I expect you also mean foo and bar as fabian suggested above... It's definitely safer to use… | |||||
if ($this->traceMode || $this->conduit) { | if ($this->traceMode || $this->conduit) { | ||||
list($stdout, $stderr) = $future->read(); | list($stdout, $stderr) = $future->read(); | ||||
$stdout = trim($stdout); | $stdout = trim($stdout); | ||||
ulziibayUnsubmitted Not Done Inline ActionsMultiline comment shoo ulziibay: Multiline comment shoo | |||||
$stderr = trim($stderr); | $stderr = trim($stderr); | ||||
if (strlen($stdout)) { | if (strlen($stdout)) { | ||||
$this->logMessage('STDO', $stdout, $stdout); | $this->logMessage('STDO', $stdout, $stdout); | ||||
} | } | ||||
if (strlen($stderr)) { | if (strlen($stderr)) { | ||||
tommycrushUnsubmitted Not Done Inline Actionsyay tommycrush: yay | |||||
$this->logMessage('STDE', $stderr, $stderr); | $this->logMessage('STDE', $stderr, $stderr); | ||||
} | } | ||||
$future->discardBuffers(); | $future->discardBuffers(); | ||||
JimJamUnsubmitted Not Done Inline ActionsHit the road, buffers, and doncha come back no more, no more, no more, no more. JimJam: Hit the road, buffers, and doncha come back no more, no more, no more, no more. | |||||
} | } | ||||
if ($result !== null) { | if ($result !== null) { | ||||
list($err) = $result; | list($err) = $result; | ||||
if ($err) { | if ($err) { | ||||
$this->logMessage( | $this->logMessage( | ||||
'FAIL', | 'FAIL', | ||||
'Process exited with error '.$err.'.', | 'Process exited with error '.$err.'.', | ||||
tommycrushUnsubmitted Not Done Inline Actionsgah tommycrush: gah | |||||
$err); | $err); | ||||
} else { | } else { | ||||
$this->logMessage('DONE', 'Process exited successfully.'); | $this->logMessage('DONE', 'Process exited successfully.'); | ||||
} | } | ||||
break 2; | break 2; | ||||
} | } | ||||
} while (time() < $this->deadline); | } while (time() < $this->deadline); | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |
Yes, comments are good!
Testing phabricator....