Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
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.

daemon.php 4.5KB

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