2017-12-14 11:38:51 -05:00
|
|
|
#!/usr/bin/env php
|
2016-11-27 15:57:18 -05:00
|
|
|
<?php
|
2016-11-27 17:52:21 -05:00
|
|
|
/**
|
2018-03-18 23:24:09 -04:00
|
|
|
* @file bin/daemon.php
|
2017-11-19 17:00:43 -05:00
|
|
|
* @brief Run the worker from a daemon.
|
2016-11-27 17:52:21 -05:00
|
|
|
*
|
|
|
|
* This script was taken from http://php.net/manual/en/function.pcntl-fork.php
|
|
|
|
*/
|
2016-11-27 15:57:18 -05:00
|
|
|
function shutdown() {
|
|
|
|
posix_kill(posix_getpid(), SIGHUP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array("start", $_SERVER["argv"])) {
|
|
|
|
$mode = "start";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array("stop", $_SERVER["argv"])) {
|
|
|
|
$mode = "stop";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array("status", $_SERVER["argv"])) {
|
|
|
|
$mode = "status";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($mode)) {
|
|
|
|
die("Please use either 'start', 'stop' or 'status'.\n");
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:38:51 -05:00
|
|
|
if (empty($_SERVER["argv"][0])) {
|
|
|
|
die("Unexpected script behaviour. This message should never occur.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the base directory
|
|
|
|
$directory = dirname($_SERVER["argv"][0]);
|
|
|
|
|
|
|
|
if (substr($directory, 0, 1) != "/") {
|
|
|
|
$directory = $_SERVER["PWD"]."/".$directory;
|
|
|
|
}
|
|
|
|
$directory = realpath($directory."/..");
|
|
|
|
|
2018-04-21 08:10:53 -04:00
|
|
|
@include($directory."/config/.htconfig.php");
|
2016-11-27 15:57:18 -05:00
|
|
|
|
|
|
|
if (!isset($pidfile)) {
|
|
|
|
die('Please specify a pid file in the variable $pidfile in the .htconfig.php. For example:'."\n".
|
|
|
|
'$pidfile = "/path/to/daemon.pid";'."\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($mode, array("stop", "status"))) {
|
|
|
|
$pid = @file_get_contents($pidfile);
|
|
|
|
|
|
|
|
if (!$pid) {
|
|
|
|
die("Pidfile wasn't found. Is the daemon running?\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mode == "status") {
|
|
|
|
if (posix_kill($pid, 0)) {
|
|
|
|
die("Daemon process $pid is running.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink($pidfile);
|
|
|
|
|
|
|
|
die("Daemon process $pid isn't running.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mode == "stop") {
|
|
|
|
posix_kill($pid, SIGTERM);
|
|
|
|
|
|
|
|
unlink($pidfile);
|
|
|
|
|
|
|
|
die("Worker daemon process $pid was killed.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "Starting worker daemon.\n";
|
|
|
|
|
|
|
|
if (isset($a->config['php_path'])) {
|
|
|
|
$php = $a->config['php_path'];
|
|
|
|
} else {
|
|
|
|
$php = "php";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch over to daemon mode.
|
|
|
|
if ($pid = pcntl_fork())
|
|
|
|
return; // Parent
|
|
|
|
|
|
|
|
fclose(STDIN); // Close all of the standard
|
|
|
|
fclose(STDOUT); // file descriptors as we
|
|
|
|
fclose(STDERR); // are running as a daemon.
|
|
|
|
|
|
|
|
register_shutdown_function('shutdown');
|
|
|
|
|
|
|
|
if (posix_setsid() < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ($pid = pcntl_fork())
|
|
|
|
return; // Parent
|
|
|
|
|
|
|
|
$pid = getmypid();
|
|
|
|
file_put_contents($pidfile, $pid);
|
|
|
|
|
|
|
|
// Now running as a daemon.
|
|
|
|
while (true) {
|
2016-11-27 18:58:26 -05:00
|
|
|
// Just to be sure that this script really runs endlessly
|
|
|
|
set_time_limit(0);
|
|
|
|
|
2017-11-19 17:00:43 -05:00
|
|
|
// Call the worker
|
2018-03-18 23:25:21 -04:00
|
|
|
$cmdline = $php.' bin/worker.php';
|
2016-11-27 15:57:18 -05:00
|
|
|
|
2017-12-14 11:38:51 -05:00
|
|
|
$executed = false;
|
|
|
|
|
|
|
|
if (function_exists('proc_open')) {
|
|
|
|
$resource = proc_open($cmdline . ' &', array(), $foo, $directory);
|
|
|
|
|
|
|
|
if (is_resource($resource)) {
|
|
|
|
$executed = true;
|
|
|
|
proc_close($resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$executed) {
|
|
|
|
exec($cmdline.' spawn');
|
|
|
|
}
|
2016-11-27 15:57:18 -05:00
|
|
|
|
|
|
|
// Now sleep for 5 minutes
|
|
|
|
sleep(300);
|
|
|
|
}
|