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.

186 lines
4.2KB

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