X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=bin%2Fdaemon.php;h=2069c13ea823a8fbce27af06e8365664a0d0abee;hb=83243800b55c1e1dae886f67f569c839dcb68306;hp=d0f57d44d0b74955d48f34d8315aee18fa0d9395;hpb=346697d771e2656b4bcd12a5e84045710e9b2b55;p=friendica.git diff --git a/bin/daemon.php b/bin/daemon.php index d0f57d44d0..2069c13ea8 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -6,8 +6,38 @@ * * This script was taken from http://php.net/manual/en/function.pcntl-fork.php */ -function shutdown() { - posix_kill(posix_getpid(), SIGHUP); + +use Friendica\App; +use Friendica\BaseObject; +use Friendica\Core\Config; +use Friendica\Core\Worker; + +// Ensure that daemon.php is executed from the base path of the installation +if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { + $directory = dirname($_SERVER["argv"][0]); + + if (substr($directory, 0, 1) != "/") { + $directory = $_SERVER["PWD"]."/".$directory; + } + $directory = realpath($directory."/.."); + + chdir($directory); +} + +require_once "boot.php"; +require_once "include/dba.php"; + +$a = new App(dirname(__DIR__)); +BaseObject::setApp($a); + +require_once ".htconfig.php"; +dba::connect($db_host, $db_user, $db_pass, $db_data); + +Config::load(); + +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("start", $_SERVER["argv"])) { @@ -30,27 +60,11 @@ 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."/.."); - -@include($directory."/config/.htconfig.php"); - -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); +$pid = @file_get_contents($pidfile); - if (!$pid) { - die("Pidfile wasn't found. Is the daemon running?\n"); - } +if (empty($pid) && in_array($mode, ["stop", "status"])) { + Config::set('system', 'worker_daemon_mode', false); + die("Pidfile wasn't found. Is the daemon running?\n"); } if ($mode == "status") { @@ -60,6 +74,7 @@ if ($mode == "status") { unlink($pidfile); + Config::set('system', 'worker_daemon_mode', false); die("Daemon process $pid isn't running.\n"); } @@ -68,17 +83,19 @@ if ($mode == "stop") { unlink($pidfile); + logger("Worker daemon process $pid was killed.", LOGGER_DEBUG); + + Config::set('system', 'worker_daemon_mode', false); 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"; +if (!empty($pid) && posix_kill($pid, 0)) { + die("Daemon process $pid is already running.\n"); } +logger('Starting worker daemon.', LOGGER_DEBUG); +echo "Starting worker daemon.\n"; + // Switch over to daemon mode. if ($pid = pcntl_fork()) return; // Parent @@ -95,32 +112,51 @@ if (posix_setsid() < 0) if ($pid = pcntl_fork()) return; // Parent +// We lose the database connection upon forking +dba::connect($db_host, $db_user, $db_pass, $db_data); +unset($db_host, $db_user, $db_pass, $db_data); + +Config::set('system', 'worker_daemon_mode', true); + +// Just to be sure that this script really runs endlessly +set_time_limit(0); + $pid = getmypid(); file_put_contents($pidfile, $pid); -// Now running as a daemon. -while (true) { - // Just to be sure that this script really runs endlessly - set_time_limit(0); +$wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60; - // Call the worker - $cmdline = $php.' bin/worker.php'; +$do_cron = true; +$last_cron = 0; - $executed = false; +// Now running as a daemon. +while (true) { + if (!$do_cron && ($last_cron + $wait_interval) < time()) { + logger('Forcing cron worker call.'); + $do_cron = true; + } - if (function_exists('proc_open')) { - $resource = proc_open($cmdline . ' &', array(), $foo, $directory); + Worker::spawnWorker($do_cron); - if (is_resource($resource)) { - $executed = true; - proc_close($resource); - } + if ($do_cron) { + $last_cron = time(); } - if (!$executed) { - exec($cmdline.' spawn'); + logger("Sleeping", LOGGER_DEBUG); + $i = 0; + do { + sleep(1); + } while (($i++ < $wait_interval) && !Worker::IPCJobsExists()); + + if ($i >= $wait_interval) { + $do_cron = true; + logger("Woke up after $wait_interval seconds.", LOGGER_DEBUG); + } else { + $do_cron = false; + logger("Worker jobs are calling to be forked.", LOGGER_DEBUG); } +} - // Now sleep for 5 minutes - sleep(300); +function shutdown() { + posix_kill(posix_getpid(), SIGHUP); }