update_daemon2.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "config.php";
  10. require_once "autoload.php";
  11. require_once "functions.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. $master_handlers_installed = false;
  25. $children = array();
  26. $ctimes = array();
  27. $last_checkpoint = -1;
  28. function reap_children() {
  29. global $children;
  30. global $ctimes;
  31. $tmp = array();
  32. foreach ($children as $pid) {
  33. if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
  34. if (file_is_locked("update_daemon-$pid.lock")) {
  35. array_push($tmp, $pid);
  36. } else {
  37. _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
  38. unset($ctimes[$pid]);
  39. }
  40. } else {
  41. _debug("[reap_children] child $pid reaped.");
  42. unset($ctimes[$pid]);
  43. }
  44. }
  45. $children = $tmp;
  46. return count($tmp);
  47. }
  48. function check_ctimes() {
  49. global $ctimes;
  50. foreach (array_keys($ctimes) as $pid) {
  51. $started = $ctimes[$pid];
  52. if (time() - $started > MAX_CHILD_RUNTIME) {
  53. _debug("[MASTER] child process $pid seems to be stuck, aborting...");
  54. posix_kill($pid, SIGKILL);
  55. }
  56. }
  57. }
  58. function sigchld_handler($signal) {
  59. $running_jobs = reap_children();
  60. _debug("[SIGCHLD] jobs left: $running_jobs");
  61. pcntl_waitpid(-1, $status, WNOHANG);
  62. }
  63. function shutdown($caller_pid) {
  64. if ($caller_pid == posix_getpid()) {
  65. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  66. _debug("removing lockfile (master)...");
  67. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  68. }
  69. }
  70. }
  71. function task_shutdown() {
  72. $pid = posix_getpid();
  73. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
  74. _debug("removing lockfile ($pid)...");
  75. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  76. }
  77. }
  78. function sigint_handler() {
  79. _debug("[MASTER] SIG_INT received.\n");
  80. shutdown(posix_getpid());
  81. die;
  82. }
  83. function task_sigint_handler() {
  84. _debug("[TASK] SIG_INT received.\n");
  85. task_shutdown();
  86. die;
  87. }
  88. pcntl_signal(SIGCHLD, 'sigchld_handler');
  89. $longopts = array("log:",
  90. "tasks:",
  91. "interval:",
  92. "quiet",
  93. "help");
  94. $options = getopt("", $longopts);
  95. if (isset($options["help"]) ) {
  96. print "Tiny Tiny RSS update daemon.\n\n";
  97. print "Options:\n";
  98. print " --log FILE - log messages to FILE\n";
  99. print " --tasks N - amount of update tasks to spawn\n";
  100. print " default: " . MAX_JOBS . "\n";
  101. print " --interval N - task spawn interval\n";
  102. print " default: " . SPAWN_INTERVAL . " seconds.\n";
  103. print " --quiet - don't output messages to stdout\n";
  104. return;
  105. }
  106. define('QUIET', isset($options['quiet']));
  107. if (isset($options["tasks"])) {
  108. _debug("Set to spawn " . $options["tasks"] . " children.");
  109. $max_jobs = $options["tasks"];
  110. } else {
  111. $max_jobs = MAX_JOBS;
  112. }
  113. if (isset($options["interval"])) {
  114. _debug("Spawn interval: " . $options["interval"] . " seconds.");
  115. $spawn_interval = $options["interval"];
  116. } else {
  117. $spawn_interval = SPAWN_INTERVAL;
  118. }
  119. if (isset($options["log"])) {
  120. _debug("Logging to " . $options["log"]);
  121. define('LOGFILE', $options["log"]);
  122. }
  123. if (file_is_locked("update_daemon.lock")) {
  124. die("error: Can't create lockfile. ".
  125. "Maybe another daemon is already running.\n");
  126. }
  127. // Try to lock a file in order to avoid concurrent update.
  128. $lock_handle = make_lockfile("update_daemon.lock");
  129. if (!$lock_handle) {
  130. die("error: Can't create lockfile. ".
  131. "Maybe another daemon is already running.\n");
  132. }
  133. $schema_version = get_schema_version();
  134. if ($schema_version != SCHEMA_VERSION) {
  135. die("Schema version is wrong, please upgrade the database.\n");
  136. }
  137. // Protip: children close shared database handle when terminating, it's a bad idea to
  138. // do database stuff on main process from now on.
  139. while (true) {
  140. // Since sleep is interupted by SIGCHLD, we need another way to
  141. // respect the spawn interval
  142. $next_spawn = $last_checkpoint + $spawn_interval - time();
  143. if ($next_spawn % 60 == 0) {
  144. $running_jobs = count($children);
  145. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  146. }
  147. if ($last_checkpoint + $spawn_interval < time()) {
  148. check_ctimes();
  149. reap_children();
  150. for ($j = count($children); $j < $max_jobs; $j++) {
  151. $pid = pcntl_fork();
  152. if ($pid == -1) {
  153. die("fork failed!\n");
  154. } else if ($pid) {
  155. if (!$master_handlers_installed) {
  156. _debug("[MASTER] installing shutdown handlers");
  157. pcntl_signal(SIGINT, 'sigint_handler');
  158. pcntl_signal(SIGTERM, 'sigint_handler');
  159. register_shutdown_function('shutdown', posix_getpid());
  160. $master_handlers_installed = true;
  161. }
  162. _debug("[MASTER] spawned client $j [PID:$pid]...");
  163. array_push($children, $pid);
  164. $ctimes[$pid] = time();
  165. } else {
  166. pcntl_signal(SIGCHLD, SIG_IGN);
  167. pcntl_signal(SIGINT, 'task_sigint_handler');
  168. register_shutdown_function('task_shutdown');
  169. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  170. $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
  171. $my_pid = posix_getpid();
  172. passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
  173. sleep(1);
  174. // We exit in order to avoid fork bombing.
  175. exit(0);
  176. }
  177. }
  178. $last_checkpoint = time();
  179. }
  180. sleep(1);
  181. }
  182. ?>