Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
4.5KB

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * @file bin/daemon.php
  5. * @brief Run the worker from a daemon.
  6. *
  7. * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
  8. */
  9. use Friendica\App;
  10. use Friendica\Core\Config;
  11. use Friendica\Core\Logger;
  12. use Friendica\Core\Worker;
  13. use Friendica\Database\DBA;
  14. // Get options
  15. $shortopts = 'f';
  16. $longopts = ['foreground'];
  17. $options = getopt($shortopts, $longopts);
  18. // Ensure that daemon.php is executed from the base path of the installation
  19. if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
  20. $directory = dirname($_SERVER["argv"][0]);
  21. if (substr($directory, 0, 1) != "/") {
  22. $directory = $_SERVER["PWD"] . "/" . $directory;
  23. }
  24. $directory = realpath($directory . "/..");
  25. chdir($directory);
  26. }
  27. require_once "boot.php";
  28. $a = new App(dirname(__DIR__));
  29. if ($a->getMode()->isInstall()) {
  30. die("Friendica isn't properly installed yet.\n");
  31. }
  32. Config::load();
  33. if (empty(Config::get('system', 'pidfile'))) {
  34. die(<<<TXT
  35. Please set system.pidfile in config/local.config.php. For example:
  36. 'system' => [
  37. 'pidfile' => '/path/to/daemon.pid',
  38. ],
  39. TXT
  40. );
  41. }
  42. $pidfile = Config::get('system', 'pidfile');
  43. if (in_array("start", $_SERVER["argv"])) {
  44. $mode = "start";
  45. }
  46. if (in_array("stop", $_SERVER["argv"])) {
  47. $mode = "stop";
  48. }
  49. if (in_array("status", $_SERVER["argv"])) {
  50. $mode = "status";
  51. }
  52. $foreground = array_key_exists('f', $options) || array_key_exists('foreground', $options);
  53. if (!isset($mode)) {
  54. die("Please use either 'start', 'stop' or 'status'.\n");
  55. }
  56. if (empty($_SERVER["argv"][0])) {
  57. die("Unexpected script behaviour. This message should never occur.\n");
  58. }
  59. $pid = null;
  60. if (is_readable($pidfile)) {
  61. $pid = intval(file_get_contents($pidfile));
  62. }
  63. if (empty($pid) && in_array($mode, ["stop", "status"])) {
  64. Config::set('system', 'worker_daemon_mode', false);
  65. die("Pidfile wasn't found. Is the daemon running?\n");
  66. }
  67. if ($mode == "status") {
  68. if (posix_kill($pid, 0)) {
  69. die("Daemon process $pid is running.\n");
  70. }
  71. unlink($pidfile);
  72. Config::set('system', 'worker_daemon_mode', false);
  73. die("Daemon process $pid isn't running.\n");
  74. }
  75. if ($mode == "stop") {
  76. posix_kill($pid, SIGTERM);
  77. unlink($pidfile);
  78. Logger::log("Worker daemon process $pid was killed.", Logger::DEBUG);
  79. Config::set('system', 'worker_daemon_mode', false);
  80. die("Worker daemon process $pid was killed.\n");
  81. }
  82. if (!empty($pid) && posix_kill($pid, 0)) {
  83. die("Daemon process $pid is already running.\n");
  84. }
  85. Logger::log('Starting worker daemon.', Logger::DEBUG);
  86. if (!$foreground) {
  87. echo "Starting worker daemon.\n";
  88. // Switch over to daemon mode.
  89. if ($pid = pcntl_fork()) {
  90. return; // Parent
  91. }
  92. fclose(STDIN); // Close all of the standard
  93. // Enabling this seem to block a running php process with 100% CPU usage when there is an outpout
  94. // fclose(STDOUT); // file descriptors as we
  95. // fclose(STDERR); // are running as a daemon.
  96. DBA::disconnect();
  97. register_shutdown_function('shutdown');
  98. if (posix_setsid() < 0) {
  99. return;
  100. }
  101. if ($pid = pcntl_fork()) {
  102. return; // Parent
  103. }
  104. $pid = getmypid();
  105. file_put_contents($pidfile, $pid);
  106. // We lose the database connection upon forking
  107. $a->loadDatabase();
  108. }
  109. Config::set('system', 'worker_daemon_mode', true);
  110. // Just to be sure that this script really runs endlessly
  111. set_time_limit(0);
  112. $wait_interval = intval(Config::get('system', 'cron_interval', 5)) * 60;
  113. $do_cron = true;
  114. $last_cron = 0;
  115. // Now running as a daemon.
  116. while (true) {
  117. if (!$do_cron && ($last_cron + $wait_interval) < time()) {
  118. Logger::log('Forcing cron worker call.', Logger::DEBUG);
  119. $do_cron = true;
  120. }
  121. Worker::spawnWorker($do_cron);
  122. if ($do_cron) {
  123. // We force a reconnect of the database connection.
  124. // This is done to ensure that the connection don't get lost over time.
  125. DBA::reconnect();
  126. $last_cron = time();
  127. }
  128. Logger::log("Sleeping", Logger::DEBUG);
  129. $start = time();
  130. do {
  131. $seconds = (time() - $start);
  132. // logarithmic wait time calculation.
  133. // Background: After jobs had been started, they often fork many workers.
  134. // To not waste too much time, the sleep period increases.
  135. $arg = (($seconds + 1) / ($wait_interval / 9)) + 1;
  136. $sleep = round(log10($arg) * 1000000, 0);
  137. usleep($sleep);
  138. $timeout = ($seconds >= $wait_interval);
  139. } while (!$timeout && !Worker::IPCJobsExists());
  140. if ($timeout) {
  141. $do_cron = true;
  142. Logger::log("Woke up after $wait_interval seconds.", Logger::DEBUG);
  143. } else {
  144. $do_cron = false;
  145. Logger::log("Worker jobs are calling to be forked.", Logger::DEBUG);
  146. }
  147. }
  148. function shutdown() {
  149. posix_kill(posix_getpid(), SIGHUP);
  150. }