update_daemon2.php 6.0 KB

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