update_daemon2.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. require_once "errorhandler.php";
  20. // defaults
  21. define('PURGE_INTERVAL', 3600); // seconds
  22. define('MAX_CHILD_RUNTIME', 600); // seconds
  23. define('MAX_JOBS', 2);
  24. define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
  25. if (!function_exists('pcntl_fork')) {
  26. die("error: This script requires PHP compiled with PCNTL module.\n");
  27. }
  28. $master_handlers_installed = false;
  29. $children = array();
  30. $ctimes = array();
  31. $last_checkpoint = -1;
  32. function reap_children() {
  33. global $children;
  34. global $ctimes;
  35. $tmp = array();
  36. foreach ($children as $pid) {
  37. if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
  38. if (file_is_locked("update_daemon-$pid.lock")) {
  39. array_push($tmp, $pid);
  40. } else {
  41. _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
  42. unset($ctimes[$pid]);
  43. }
  44. } else {
  45. _debug("[reap_children] child $pid reaped.");
  46. unset($ctimes[$pid]);
  47. }
  48. }
  49. $children = $tmp;
  50. return count($tmp);
  51. }
  52. function check_ctimes() {
  53. global $ctimes;
  54. foreach (array_keys($ctimes) as $pid) {
  55. $started = $ctimes[$pid];
  56. if (time() - $started > MAX_CHILD_RUNTIME) {
  57. _debug("[MASTER] child process $pid seems to be stuck, aborting...");
  58. posix_kill($pid, SIGKILL);
  59. }
  60. }
  61. }
  62. function sigchld_handler($signal) {
  63. $running_jobs = reap_children();
  64. _debug("[SIGCHLD] jobs left: $running_jobs");
  65. pcntl_waitpid(-1, $status, WNOHANG);
  66. }
  67. function shutdown($caller_pid) {
  68. if ($caller_pid == posix_getpid()) {
  69. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  70. _debug("removing lockfile (master)...");
  71. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  72. }
  73. }
  74. }
  75. function task_shutdown() {
  76. $pid = posix_getpid();
  77. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
  78. _debug("removing lockfile ($pid)...");
  79. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  80. }
  81. }
  82. function sigint_handler() {
  83. _debug("[MASTER] SIG_INT received.\n");
  84. shutdown(posix_getpid());
  85. die;
  86. }
  87. function task_sigint_handler() {
  88. _debug("[TASK] SIG_INT received.\n");
  89. task_shutdown();
  90. die;
  91. }
  92. pcntl_signal(SIGCHLD, 'sigchld_handler');
  93. $longopts = array("log:",
  94. "tasks:",
  95. "interval:",
  96. "quiet",
  97. "help");
  98. $options = getopt("", $longopts);
  99. if (isset($options["help"]) ) {
  100. print "Tiny Tiny RSS update daemon.\n\n";
  101. print "Options:\n";
  102. print " --log FILE - log messages to FILE\n";
  103. print " --tasks N - amount of update tasks to spawn\n";
  104. print " default: " . MAX_JOBS . "\n";
  105. print " --interval N - task spawn interval\n";
  106. print " default: " . SPAWN_INTERVAL . " seconds.\n";
  107. print " --quiet - don't output messages to stdout\n";
  108. return;
  109. }
  110. define('QUIET', isset($options['quiet']));
  111. if (isset($options["tasks"])) {
  112. _debug("Set to spawn " . $options["tasks"] . " children.");
  113. $max_jobs = $options["tasks"];
  114. } else {
  115. $max_jobs = MAX_JOBS;
  116. }
  117. if (isset($options["interval"])) {
  118. _debug("Spawn interval: " . $options["interval"] . " seconds.");
  119. $spawn_interval = $options["interval"];
  120. } else {
  121. $spawn_interval = SPAWN_INTERVAL;
  122. }
  123. if (isset($options["log"])) {
  124. _debug("Logging to " . $options["log"]);
  125. define('LOGFILE', $options["log"]);
  126. }
  127. if (file_is_locked("update_daemon.lock")) {
  128. die("error: Can't create lockfile. ".
  129. "Maybe another daemon is already running.\n");
  130. }
  131. // Try to lock a file in order to avoid concurrent update.
  132. $lock_handle = make_lockfile("update_daemon.lock");
  133. if (!$lock_handle) {
  134. die("error: Can't create lockfile. ".
  135. "Maybe another daemon is already running.\n");
  136. }
  137. init_plugins();
  138. $schema_version = get_schema_version();
  139. db_close();
  140. if ($schema_version != SCHEMA_VERSION) {
  141. die("Schema version is wrong, please upgrade the database.\n");
  142. }
  143. while (true) {
  144. // Since sleep is interupted by SIGCHLD, we need another way to
  145. // respect the spawn interval
  146. $next_spawn = $last_checkpoint + $spawn_interval - time();
  147. if ($next_spawn % 60 == 0) {
  148. $running_jobs = count($children);
  149. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  150. }
  151. if ($last_checkpoint + $spawn_interval < time()) {
  152. /* Check if schema version changed */
  153. init_plugins();
  154. $test_schema_version = get_schema_version();
  155. if ($test_schema_version != $schema_version) {
  156. echo "Expected schema version: $schema_version, got: $test_schema_version\n";
  157. echo "Schema version changed while we were running, bailing out\n";
  158. exit(100);
  159. }
  160. check_ctimes();
  161. reap_children();
  162. for ($j = count($children); $j < $max_jobs; $j++) {
  163. $pid = pcntl_fork();
  164. if ($pid == -1) {
  165. die("fork failed!\n");
  166. } else if ($pid) {
  167. if (!$master_handlers_installed) {
  168. _debug("[MASTER] installing shutdown handlers");
  169. pcntl_signal(SIGINT, 'sigint_handler');
  170. register_shutdown_function('shutdown', posix_getpid());
  171. $master_handlers_installed = true;
  172. }
  173. _debug("[MASTER] spawned client $j [PID:$pid]...");
  174. array_push($children, $pid);
  175. $ctimes[$pid] = time();
  176. } else {
  177. pcntl_signal(SIGCHLD, SIG_IGN);
  178. pcntl_signal(SIGINT, 'task_sigint_handler');
  179. register_shutdown_function('task_shutdown');
  180. $my_pid = posix_getpid();
  181. $lock_filename = "update_daemon-$my_pid.lock";
  182. $lock_handle = make_lockfile($lock_filename);
  183. if (!$lock_handle) {
  184. die("error: Can't create lockfile ($lock_filename). ".
  185. "Maybe another daemon is already running.\n");
  186. }
  187. // ****** Updating RSS code *******
  188. // Only run in fork process.
  189. $start_timestamp = time();
  190. if (!init_plugins()) return;
  191. // We disable stamp file, since it is of no use in a multiprocess update.
  192. // not really, tho for the time being -fox
  193. if (!make_stampfile('update_daemon.stamp')) {
  194. _debug("warning: unable to create stampfile\n");
  195. }
  196. // Call to the feed batch update function
  197. // and maybe regenerate feedbrowser cache
  198. $nf = 0;
  199. _debug("Waiting before update [$j]..");
  200. sleep($j*5);
  201. $nf = update_daemon_common();
  202. if (rand(0,100) > 50) {
  203. $count = update_feedbrowser_cache();
  204. _debug("Feedbrowser updated, $count feeds processed.");
  205. purge_orphans( true);
  206. $rc = cleanup_tags( 14, 50000);
  207. _debug("Cleaned $rc cached tags.");
  208. global $pluginhost;
  209. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  210. }
  211. _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
  212. if ($nf > 0) {
  213. _debug("Feeds processed: $nf");
  214. if (time() - $start_timestamp > 0) {
  215. _debug("Feeds/minute: " . sprintf("%.2d", $nf/((time()-$start_timestamp)/60)));
  216. }
  217. }
  218. db_close();
  219. // We are in a fork.
  220. // We wait a little before exiting to avoid to be faster than our parent process.
  221. sleep(1);
  222. unlink(LOCK_DIRECTORY . "/$lock_filename");
  223. // We exit in order to avoid fork bombing.
  224. exit(0);
  225. }
  226. }
  227. $last_checkpoint = time();
  228. }
  229. sleep(1);
  230. }
  231. ?>