update_daemon2.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. // defaults
  13. define_default('PURGE_INTERVAL', 3600); // seconds
  14. define_default('MAX_CHILD_RUNTIME', 600); // seconds
  15. define_default('MAX_JOBS', 2);
  16. define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
  17. define_default('DAEMON_FEED_LIMIT', 250);
  18. require_once "rssfuncs.php";
  19. require_once "sanity_check.php";
  20. require_once "db.php";
  21. require_once "db-prefs.php";
  22. if (!function_exists('pcntl_fork')) {
  23. die("error: This script requires PHP compiled with PCNTL module.\n");
  24. }
  25. $master_handlers_installed = false;
  26. $children = array();
  27. $ctimes = array();
  28. $last_checkpoint = -1;
  29. function reap_children() {
  30. global $children;
  31. global $ctimes;
  32. $tmp = array();
  33. foreach ($children as $pid) {
  34. if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
  35. if (file_is_locked("update_daemon-$pid.lock")) {
  36. array_push($tmp, $pid);
  37. } else {
  38. _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
  39. unset($ctimes[$pid]);
  40. }
  41. } else {
  42. _debug("[reap_children] child $pid reaped.");
  43. unset($ctimes[$pid]);
  44. }
  45. }
  46. $children = $tmp;
  47. return count($tmp);
  48. }
  49. function check_ctimes() {
  50. global $ctimes;
  51. foreach (array_keys($ctimes) as $pid) {
  52. $started = $ctimes[$pid];
  53. if (time() - $started > MAX_CHILD_RUNTIME) {
  54. _debug("[MASTER] child process $pid seems to be stuck, aborting...");
  55. posix_kill($pid, SIGKILL);
  56. }
  57. }
  58. }
  59. function sigchld_handler($signal) {
  60. $running_jobs = reap_children();
  61. _debug("[SIGCHLD] jobs left: $running_jobs");
  62. pcntl_waitpid(-1, $status, WNOHANG);
  63. }
  64. function shutdown($caller_pid) {
  65. if ($caller_pid == posix_getpid()) {
  66. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  67. _debug("removing lockfile (master)...");
  68. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  69. }
  70. }
  71. }
  72. function task_shutdown() {
  73. $pid = posix_getpid();
  74. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
  75. _debug("removing lockfile ($pid)...");
  76. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  77. }
  78. }
  79. function sigint_handler() {
  80. _debug("[MASTER] SIG_INT received.\n");
  81. shutdown(posix_getpid());
  82. die;
  83. }
  84. function task_sigint_handler() {
  85. _debug("[TASK] SIG_INT received.\n");
  86. task_shutdown();
  87. die;
  88. }
  89. pcntl_signal(SIGCHLD, 'sigchld_handler');
  90. $longopts = array("log:",
  91. "tasks:",
  92. "interval:",
  93. "quiet",
  94. "help");
  95. $options = getopt("", $longopts);
  96. if (isset($options["help"]) ) {
  97. print "Tiny Tiny RSS update daemon.\n\n";
  98. print "Options:\n";
  99. print " --log FILE - log messages to FILE\n";
  100. print " --tasks N - amount of update tasks to spawn\n";
  101. print " default: " . MAX_JOBS . "\n";
  102. print " --interval N - task spawn interval\n";
  103. print " default: " . SPAWN_INTERVAL . " seconds.\n";
  104. print " --quiet - don't output messages to stdout\n";
  105. return;
  106. }
  107. define('QUIET', isset($options['quiet']));
  108. if (isset($options["tasks"])) {
  109. _debug("Set to spawn " . $options["tasks"] . " children.");
  110. $max_jobs = $options["tasks"];
  111. } else {
  112. $max_jobs = MAX_JOBS;
  113. }
  114. if (isset($options["interval"])) {
  115. _debug("Spawn interval: " . $options["interval"] . " seconds.");
  116. $spawn_interval = $options["interval"];
  117. } else {
  118. $spawn_interval = SPAWN_INTERVAL;
  119. }
  120. if (isset($options["log"])) {
  121. _debug("Logging to " . $options["log"]);
  122. define('LOGFILE', $options["log"]);
  123. }
  124. if (file_is_locked("update_daemon.lock")) {
  125. die("error: Can't create lockfile. ".
  126. "Maybe another daemon is already running.\n");
  127. }
  128. // Try to lock a file in order to avoid concurrent update.
  129. $lock_handle = make_lockfile("update_daemon.lock");
  130. if (!$lock_handle) {
  131. die("error: Can't create lockfile. ".
  132. "Maybe another daemon is already running.\n");
  133. }
  134. init_plugins();
  135. $schema_version = get_schema_version();
  136. if ($schema_version != SCHEMA_VERSION) {
  137. die("Schema version is wrong, please upgrade the database.\n");
  138. }
  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 if schema version changed */
  149. $test_schema_version = get_schema_version();
  150. if ($test_schema_version != $schema_version) {
  151. echo "Expected schema version: $schema_version, got: $test_schema_version\n";
  152. echo "Schema version changed while we were running, bailing out\n";
  153. exit(100);
  154. }
  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. register_shutdown_function('shutdown', posix_getpid());
  166. $master_handlers_installed = true;
  167. }
  168. _debug("[MASTER] spawned client $j [PID:$pid]...");
  169. array_push($children, $pid);
  170. $ctimes[$pid] = time();
  171. } else {
  172. pcntl_signal(SIGCHLD, SIG_IGN);
  173. pcntl_signal(SIGINT, 'task_sigint_handler');
  174. register_shutdown_function('task_shutdown');
  175. $quiet = (isset($options["quiet"])) ? "--quiet" : "";
  176. $my_pid = posix_getpid();
  177. passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid");
  178. sleep(1);
  179. // We exit in order to avoid fork bombing.
  180. exit(0);
  181. }
  182. }
  183. $last_checkpoint = time();
  184. }
  185. sleep(1);
  186. }
  187. ?>