Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

206 righe
4.7 KiB

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