update_daemon2.php 5.9 KB

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