update_daemon2.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. define('PURGE_INTERVAL', 3600); // seconds
  13. define('MAX_CHILD_RUNTIME', 600); // seconds
  14. require_once "functions.php";
  15. require_once "rssfuncs.php";
  16. require_once "sanity_check.php";
  17. require_once "config.php";
  18. require_once "db.php";
  19. require_once "db-prefs.php";
  20. define('MAX_JOBS', 2);
  21. define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
  22. if (!function_exists('pcntl_fork')) {
  23. die("error: This script requires PHP compiled with PCNTL module.\n");
  24. }
  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() {
  64. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
  65. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  66. }
  67. function task_shutdown() {
  68. $pid = posix_getpid();
  69. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
  70. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  71. }
  72. function sigint_handler() {
  73. shutdown();
  74. die("[SIGINT] removing lockfile and exiting.\n");
  75. }
  76. function task_sigint_handler() {
  77. task_shutdown();
  78. die("[SIGINT] removing lockfile and exiting.\n");
  79. }
  80. pcntl_signal(SIGCHLD, 'sigchld_handler');
  81. if (file_is_locked("update_daemon.lock")) {
  82. die("error: Can't create lockfile. ".
  83. "Maybe another daemon is already running.\n");
  84. }
  85. /* if (!pcntl_fork()) {
  86. pcntl_signal(SIGINT, 'sigint_handler');
  87. register_shutdown_function('shutdown');
  88. // Try to lock a file in order to avoid concurrent update.
  89. $lock_handle = make_lockfile("update_daemon.lock");
  90. if (!$lock_handle) {
  91. die("error: Can't create lockfile. ".
  92. "Maybe another daemon is already running.\n");
  93. }
  94. while (true) { sleep(100); }
  95. } */
  96. // Testing database connection.
  97. // It is unnecessary to start the fork loop if database is not ok.
  98. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  99. if (!init_connection($link)) return;
  100. db_close($link);
  101. while (true) {
  102. // Since sleep is interupted by SIGCHLD, we need another way to
  103. // respect the SPAWN_INTERVAL
  104. $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
  105. if ($next_spawn % 10 == 0) {
  106. $running_jobs = count($children);
  107. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  108. }
  109. if ($last_checkpoint + SPAWN_INTERVAL < time()) {
  110. check_ctimes();
  111. reap_children();
  112. for ($j = count($children); $j < MAX_JOBS; $j++) {
  113. $pid = pcntl_fork();
  114. if ($pid == -1) {
  115. die("fork failed!\n");
  116. } else if ($pid) {
  117. _debug("[MASTER] spawned client $j [PID:$pid]...");
  118. array_push($children, $pid);
  119. $ctimes[$pid] = time();
  120. } else {
  121. pcntl_signal(SIGCHLD, SIG_IGN);
  122. pcntl_signal(SIGINT, 'task_sigint_handler');
  123. register_shutdown_function('task_shutdown');
  124. $my_pid = posix_getpid();
  125. $lock_filename = "update_daemon-$my_pid.lock";
  126. $lock_handle = make_lockfile($lock_filename);
  127. if (!$lock_handle) {
  128. die("error: Can't create lockfile ($lock_filename). ".
  129. "Maybe another daemon is already running.\n");
  130. }
  131. // ****** Updating RSS code *******
  132. // Only run in fork process.
  133. $start_timestamp = time();
  134. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  135. if (!init_connection($link)) return;
  136. // We disable stamp file, since it is of no use in a multiprocess update.
  137. // not really, tho for the time being -fox
  138. if (!make_stampfile('update_daemon.stamp')) {
  139. print "warning: unable to create stampfile";
  140. }
  141. // Call to the feed batch update function
  142. // or regenerate feedbrowser cache
  143. if (rand(0,100) > 30) {
  144. update_daemon_common($link);
  145. } else {
  146. $count = update_feedbrowser_cache($link);
  147. _debug("Feedbrowser updated, $count feeds processed.");
  148. purge_orphans($link, true);
  149. $rc = cleanup_tags($link, 14, 50000);
  150. _debug("Cleaned $rc cached tags.");
  151. global $pluginhost;
  152. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  153. }
  154. _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
  155. db_close($link);
  156. // We are in a fork.
  157. // We wait a little before exiting to avoid to be faster than our parent process.
  158. sleep(1);
  159. unlink(LOCK_DIRECTORY . "/$lock_filename");
  160. // We exit in order to avoid fork bombing.
  161. exit(0);
  162. }
  163. // We wait a little time before the next fork, in order to let the first fork
  164. // mark the feeds it update :
  165. sleep(1);
  166. }
  167. $last_checkpoint = time();
  168. }
  169. sleep(1);
  170. }
  171. ?>