update_daemon2.php 6.2 KB

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