PageRenderTime 90ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/update_daemon2.php

https://github.com/haharazer/Tiny-Tiny-RSS
PHP | 247 lines | 190 code | 50 blank | 7 comment | 27 complexity | a54eb388e83d0eadae7589ea7fa18958 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, BSD-3-Clause, LGPL-2.0
  1. #!/usr/bin/env php
  2. <?php
  3. set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
  4. get_include_path());
  5. declare(ticks = 1);
  6. chdir(dirname(__FILE__));
  7. define('DISABLE_SESSIONS', true);
  8. require_once "version.php";
  9. require_once "autoload.php";
  10. require_once "functions.php";
  11. require_once "config.php";
  12. require_once "rssfuncs.php";
  13. // defaults
  14. define_default('PURGE_INTERVAL', 3600); // seconds
  15. define_default('MAX_CHILD_RUNTIME', 1800); // seconds
  16. define_default('MAX_JOBS', 2);
  17. define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
  18. require_once "sanity_check.php";
  19. require_once "db.php";
  20. require_once "db-prefs.php";
  21. if (!function_exists('pcntl_fork')) {
  22. die("error: This script requires PHP compiled with PCNTL module.\n");
  23. }
  24. $options = getopt("");
  25. if (!is_array($options)) {
  26. die("error: getopt() failed. ".
  27. "Most probably you are using PHP CGI to run this script ".
  28. "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
  29. "additional information.\n");
  30. }
  31. $master_handlers_installed = false;
  32. $children = array();
  33. $ctimes = array();
  34. $last_checkpoint = -1;
  35. function reap_children() {
  36. global $children;
  37. global $ctimes;
  38. $tmp = array();
  39. foreach ($children as $pid) {
  40. if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
  41. if (file_is_locked("update_daemon-$pid.lock")) {
  42. array_push($tmp, $pid);
  43. } else {
  44. _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
  45. unset($ctimes[$pid]);
  46. }
  47. } else {
  48. _debug("[reap_children] child $pid reaped.");
  49. unset($ctimes[$pid]);
  50. }
  51. }
  52. $children = $tmp;
  53. return count($tmp);
  54. }
  55. function check_ctimes() {
  56. global $ctimes;
  57. foreach (array_keys($ctimes) as $pid) {
  58. $started = $ctimes[$pid];
  59. if (time() - $started > MAX_CHILD_RUNTIME) {
  60. _debug("[MASTER] child process $pid seems to be stuck, aborting...");
  61. posix_kill($pid, SIGKILL);
  62. }
  63. }
  64. }
  65. function sigchld_handler($signal) {
  66. $running_jobs = reap_children();
  67. _debug("[SIGCHLD] jobs left: $running_jobs");
  68. pcntl_waitpid(-1, $status, WNOHANG);
  69. }
  70. function shutdown($caller_pid) {
  71. if ($caller_pid == posix_getpid()) {
  72. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  73. _debug("removing lockfile (master)...");
  74. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  75. }
  76. }
  77. }
  78. function task_shutdown() {
  79. $pid = posix_getpid();
  80. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
  81. _debug("removing lockfile ($pid)...");
  82. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  83. }
  84. }
  85. function sigint_handler() {
  86. _debug("[MASTER] SIG_INT received.\n");
  87. shutdown(posix_getpid());
  88. die;
  89. }
  90. function task_sigint_handler() {
  91. _debug("[TASK] SIG_INT received.\n");
  92. task_shutdown();
  93. die;
  94. }
  95. pcntl_signal(SIGCHLD, 'sigchld_handler');
  96. $longopts = array("log:",
  97. "tasks:",
  98. "interval:",
  99. "quiet",
  100. "help");
  101. $options = getopt("", $longopts);
  102. if (isset($options["help"]) ) {
  103. print "Tiny Tiny RSS update daemon.\n\n";
  104. print "Options:\n";
  105. print " --log FILE - log messages to FILE\n";
  106. print " --tasks N - amount of update tasks to spawn\n";
  107. print " default: " . MAX_JOBS . "\n";
  108. print " --interval N - task spawn interval\n";
  109. print " default: " . SPAWN_INTERVAL . " seconds.\n";
  110. print " --quiet - don't output messages to stdout\n";
  111. return;
  112. }
  113. define('QUIET', isset($options['quiet']));
  114. if (isset($options["tasks"])) {
  115. _debug("Set to spawn " . $options["tasks"] . " children.");
  116. $max_jobs = $options["tasks"];
  117. } else {
  118. $max_jobs = MAX_JOBS;
  119. }
  120. if (isset($options["interval"])) {
  121. _debug("Spawn interval: " . $options["interval"] . " seconds.");
  122. $spawn_interval = $options["interval"];
  123. } else {
  124. $spawn_interval = SPAWN_INTERVAL;
  125. }
  126. if (isset($options["log"])) {
  127. _debug("Logging to " . $options["log"]);
  128. define('LOGFILE', $options["log"]);
  129. }
  130. if (file_is_locked("update_daemon.lock")) {
  131. die("error: Can't create lockfile. ".
  132. "Maybe another daemon is already running.\n");
  133. }
  134. // Try to lock a file in order to avoid concurrent update.
  135. $lock_handle = make_lockfile("update_daemon.lock");
  136. if (!$lock_handle) {
  137. die("error: Can't create lockfile. ".
  138. "Maybe another daemon is already running.\n");
  139. }
  140. $schema_version = get_schema_version();
  141. if ($schema_version != SCHEMA_VERSION) {
  142. die("Schema version is wrong, please upgrade the database.\n");
  143. }
  144. // Protip: children close shared database handle when terminating, it's a bad idea to
  145. // do database stuff on main process from now on.
  146. while (true) {
  147. // Since sleep is interupted by SIGCHLD, we need another way to
  148. // respect the spawn interval
  149. $next_spawn = $last_checkpoint + $spawn_interval - time();
  150. if ($next_spawn % 60 == 0) {
  151. $running_jobs = count($children);
  152. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  153. }
  154. if ($last_checkpoint + $spawn_interval < time()) {
  155. check_ctimes();
  156. reap_children();
  157. for ($j = count($children); $j < $max_jobs; $j++) {
  158. $pid = pcntl_fork();
  159. if ($pid == -1) {
  160. die("fork failed!\n");
  161. } else if ($pid) {
  162. if (!$master_handlers_installed) {
  163. _debug("[MASTER] installing shutdown handlers");
  164. pcntl_signal(SIGINT, 'sigint_handler');
  165. pcntl_signal(SIGTERM, 'sigint_handler');
  166. register_shutdown_function('shutdown', posix_getpid());
  167. $master_handlers_installed = true;
  168. }
  169. _debug("[MASTER] spawned client $j [PID:$pid]...");
  170. array_push($children, $pid);
  171. $ctimes[$pid] = time();
  172. } else {
  173. pcntl_signal(SIGCHLD, SIG_IGN);
  174. pcntl_signal(SIGINT, 'task_sigint_handler');
  175. register_shutdown_function('task_shutdown');
  176. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  177. $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
  178. $my_pid = posix_getpid();
  179. passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
  180. sleep(1);
  181. // We exit in order to avoid fork bombing.
  182. exit(0);
  183. }
  184. }
  185. $last_checkpoint = time();
  186. }
  187. sleep(1);
  188. }
  189. ?>