update_daemon2.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/usr/bin/php
  2. <?php
  3. set_include_path(get_include_path() . PATH_SEPARATOR .
  4. dirname(__FILE__) . "/include");
  5. // This is an experimental multiprocess update daemon.
  6. // Some configurable variable may be found below.
  7. declare(ticks = 1);
  8. chdir(dirname(__FILE__));
  9. define('DISABLE_SESSIONS', true);
  10. require_once "version.php";
  11. if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
  12. define('DAEMON_EXTENDED_DEBUG', true);
  13. }
  14. define('PURGE_INTERVAL', 3600); // seconds
  15. define('MAX_CHILD_RUNTIME', 600); // seconds
  16. require_once "sanity_check.php";
  17. require_once "config.php";
  18. define('MAX_JOBS', 2);
  19. define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
  20. if (!function_exists('pcntl_fork')) {
  21. die("error: This script requires PHP compiled with PCNTL module.\n");
  22. }
  23. require_once "db.php";
  24. require_once "db-prefs.php";
  25. require_once "functions.php";
  26. require_once "rssfuncs.php";
  27. require_once "lib/magpierss/rss_fetch.inc";
  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. }
  42. } else {
  43. _debug("[reap_children] child $pid reaped.");
  44. unset($ctimes[$pid]);
  45. }
  46. }
  47. $children = $tmp;
  48. return count($tmp);
  49. }
  50. function check_ctimes() {
  51. global $ctimes;
  52. foreach (array_keys($ctimes) as $pid) {
  53. $started = $ctimes[$pid];
  54. if (time() - $started > MAX_CHILD_RUNTIME) {
  55. _debug("[MASTER] child process $pid seems to be stuck, aborting...");
  56. posix_kill($pid, SIGKILL);
  57. }
  58. }
  59. }
  60. function sigchld_handler($signal) {
  61. $running_jobs = reap_children();
  62. _debug("[SIGCHLD] jobs left: $running_jobs");
  63. pcntl_waitpid(-1, $status, WNOHANG);
  64. }
  65. function shutdown() {
  66. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
  67. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  68. }
  69. function task_shutdown() {
  70. $pid = posix_getpid();
  71. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
  72. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  73. }
  74. function sigint_handler() {
  75. shutdown();
  76. die("[SIGINT] removing lockfile and exiting.\n");
  77. }
  78. function task_sigint_handler() {
  79. task_shutdown();
  80. die("[SIGINT] removing lockfile and exiting.\n");
  81. }
  82. pcntl_signal(SIGCHLD, 'sigchld_handler');
  83. if (file_is_locked("update_daemon.lock")) {
  84. die("error: Can't create lockfile. ".
  85. "Maybe another daemon is already running.\n");
  86. }
  87. if (!pcntl_fork()) {
  88. pcntl_signal(SIGINT, 'sigint_handler');
  89. register_shutdown_function('shutdown');
  90. // Try to lock a file in order to avoid concurrent update.
  91. $lock_handle = make_lockfile("update_daemon.lock");
  92. if (!$lock_handle) {
  93. die("error: Can't create lockfile. ".
  94. "Maybe another daemon is already running.\n");
  95. }
  96. while (true) { sleep(100); }
  97. }
  98. // Testing database connection.
  99. // It is unnecessary to start the fork loop if database is not ok.
  100. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  101. if (!init_connection($link)) return;
  102. db_close($link);
  103. while (true) {
  104. // Since sleep is interupted by SIGCHLD, we need another way to
  105. // respect the SPAWN_INTERVAL
  106. $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
  107. if ($next_spawn % 10 == 0) {
  108. $running_jobs = count($children);
  109. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  110. }
  111. if ($last_checkpoint + SPAWN_INTERVAL < time()) {
  112. check_ctimes();
  113. reap_children();
  114. for ($j = count($children); $j < MAX_JOBS; $j++) {
  115. $pid = pcntl_fork();
  116. if ($pid == -1) {
  117. die("fork failed!\n");
  118. } else if ($pid) {
  119. _debug("[MASTER] spawned client $j [PID:$pid]...");
  120. array_push($children, $pid);
  121. $ctimes[$pid] = time();
  122. } else {
  123. pcntl_signal(SIGCHLD, SIG_IGN);
  124. pcntl_signal(SIGINT, 'task_sigint_handler');
  125. register_shutdown_function('task_shutdown');
  126. $my_pid = posix_getpid();
  127. $lock_filename = "update_daemon-$my_pid.lock";
  128. $lock_handle = make_lockfile($lock_filename);
  129. if (!$lock_handle) {
  130. die("error: Can't create lockfile ($lock_filename). ".
  131. "Maybe another daemon is already running.\n");
  132. }
  133. // ****** Updating RSS code *******
  134. // Only run in fork process.
  135. $start_timestamp = time();
  136. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  137. if (!init_connection($link)) return;
  138. // We disable stamp file, since it is of no use in a multiprocess update.
  139. // not really, tho for the time being -fox
  140. if (!make_stampfile('update_daemon.stamp')) {
  141. print "warning: unable to create stampfile";
  142. }
  143. // Call to the feed batch update function
  144. // or regenerate feedbrowser cache
  145. if (rand(0,100) > 30) {
  146. update_daemon_common($link);
  147. } else {
  148. $count = update_feedbrowser_cache($link);
  149. _debug("Feedbrowser updated, $count feeds processed.");
  150. purge_orphans($link, true);
  151. $rc = cleanup_tags($link, 14, 50000);
  152. _debug("Cleaned $rc cached tags.");
  153. _debug("Updating linked feeds...");
  154. get_linked_feeds($link);
  155. }
  156. _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
  157. db_close($link);
  158. // We are in a fork.
  159. // We wait a little before exiting to avoid to be faster than our parent process.
  160. sleep(1);
  161. unlink(LOCK_DIRECTORY . "/$lock_filename");
  162. // We exit in order to avoid fork bombing.
  163. exit(0);
  164. }
  165. // We wait a little time before the next fork, in order to let the first fork
  166. // mark the feeds it update :
  167. sleep(1);
  168. }
  169. $last_checkpoint = time();
  170. }
  171. sleep(1);
  172. }
  173. ?>