update_daemon2.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "functions.php";
  13. require_once "rssfuncs.php";
  14. require_once "sanity_check.php";
  15. require_once "config.php";
  16. require_once "db.php";
  17. require_once "db-prefs.php";
  18. // defaults
  19. define('PURGE_INTERVAL', 3600); // seconds
  20. define('MAX_CHILD_RUNTIME', 600); // seconds
  21. define('MAX_JOBS', 2);
  22. define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
  23. if (!function_exists('pcntl_fork')) {
  24. die("error: This script requires PHP compiled with PCNTL module.\n");
  25. }
  26. $master_handlers_installed = false;
  27. $children = array();
  28. $ctimes = array();
  29. $last_checkpoint = -1;
  30. function reap_children() {
  31. global $children;
  32. global $ctimes;
  33. $tmp = array();
  34. foreach ($children as $pid) {
  35. if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
  36. if (file_is_locked("update_daemon-$pid.lock")) {
  37. array_push($tmp, $pid);
  38. } else {
  39. _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
  40. unset($ctimes[$pid]);
  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($caller_pid) {
  66. if ($caller_pid == posix_getpid()) {
  67. if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
  68. _debug("removing lockfile (master)...");
  69. unlink(LOCK_DIRECTORY . "/update_daemon.lock");
  70. }
  71. }
  72. }
  73. function task_shutdown() {
  74. $pid = posix_getpid();
  75. if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
  76. _debug("removing lockfile ($pid)...");
  77. unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
  78. }
  79. }
  80. function sigint_handler() {
  81. _debug("[MASTER] SIG_INT received.\n");
  82. shutdown(posix_getpid());
  83. die;
  84. }
  85. function task_sigint_handler() {
  86. _debug("[TASK] SIG_INT received.\n");
  87. task_shutdown();
  88. die;
  89. }
  90. pcntl_signal(SIGCHLD, 'sigchld_handler');
  91. $longopts = array("log:",
  92. "tasks:",
  93. "interval",
  94. "quiet",
  95. "help");
  96. $options = getopt("", $longopts);
  97. if (isset($options["help"]) ) {
  98. print "Tiny Tiny RSS update daemon.\n\n";
  99. print "Options:\n";
  100. print " --log FILE - log messages to FILE\n";
  101. print " --tasks N - amount of update tasks to spawn\n";
  102. print " default: " . MAX_JOBS . "\n";
  103. print " --interval N - task spawn interval\n";
  104. print " default: " . SPAWN_INTERVAL . " seconds.\n";
  105. print " --quiet - don't output messages to stdout\n";
  106. return;
  107. }
  108. define('QUIET', isset($options['quiet']));
  109. if (isset($options["tasks"])) {
  110. _debug("Set to spawn " . $options["tasks"] . " children.");
  111. $max_jobs = $option["tasks"];
  112. } else {
  113. $max_jobs = MAX_JOBS;
  114. }
  115. if (isset($options["interval"])) {
  116. _debug("Spawn interval: " . $options["interval"] . " seconds.");
  117. $spawn_interval = $option["interval"];
  118. } else {
  119. $spawn_interval = SPAWN_INTERVAL;
  120. }
  121. if (isset($options["log"])) {
  122. _debug("Logging to " . $options["log"]);
  123. define('LOGFILE', $options["log"]);
  124. }
  125. if (file_is_locked("update_daemon.lock")) {
  126. die("error: Can't create lockfile. ".
  127. "Maybe another daemon is already running.\n");
  128. }
  129. // Try to lock a file in order to avoid concurrent update.
  130. $lock_handle = make_lockfile("update_daemon.lock");
  131. if (!$lock_handle) {
  132. die("error: Can't create lockfile. ".
  133. "Maybe another daemon is already running.\n");
  134. }
  135. // Testing database connection.
  136. // It is unnecessary to start the fork loop if database is not ok.
  137. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  138. if (!init_connection($link)) die("Can't initialize db connection.\n");
  139. $schema_version = get_schema_version($link);
  140. db_close($link);
  141. while (true) {
  142. // Since sleep is interupted by SIGCHLD, we need another way to
  143. // respect the spawn interval
  144. $next_spawn = $last_checkpoint + $spawn_interval - time();
  145. if ($next_spawn % 60 == 0) {
  146. $running_jobs = count($children);
  147. _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
  148. }
  149. if ($last_checkpoint + $spawn_interval < time()) {
  150. /* Check if schema version changed */
  151. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  152. if (!init_connection($link)) die("Can't initialize db connection.\n");
  153. $test_schema_version = get_schema_version($link);
  154. db_close($link);
  155. if ($test_schema_version != $schema_version) {
  156. _debug("Expected schema version: $schema_version, got: $test_schema_version");
  157. _debug("Schema version changed while we were running, bailing out");
  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. $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  191. if (!init_connection($link)) return;
  192. // We disable stamp file, since it is of no use in a multiprocess update.
  193. // not really, tho for the time being -fox
  194. if (!make_stampfile('update_daemon.stamp')) {
  195. _debug("warning: unable to create stampfile\n");
  196. }
  197. // Call to the feed batch update function
  198. // and maybe regenerate feedbrowser cache
  199. $nf = 0;
  200. _debug("Waiting before update [$j]..");
  201. sleep($j*5);
  202. $nf = update_daemon_common($link);
  203. if (rand(0,100) > 50) {
  204. $count = update_feedbrowser_cache($link);
  205. _debug("Feedbrowser updated, $count feeds processed.");
  206. purge_orphans($link, true);
  207. $rc = cleanup_tags($link, 14, 50000);
  208. _debug("Cleaned $rc cached tags.");
  209. global $pluginhost;
  210. $pluginhost->run_hooks($pluginhost::HOOK_UPDATE_TASK, "hook_update_task", $op);
  211. }
  212. _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
  213. if ($nf > 0) {
  214. _debug("Feeds processed: $nf; feeds/minute: " . sprintf("%.2d", $nf/((time()-$start_timestamp)/60)));
  215. }
  216. db_close($link);
  217. // We are in a fork.
  218. // We wait a little before exiting to avoid to be faster than our parent process.
  219. sleep(1);
  220. unlink(LOCK_DIRECTORY . "/$lock_filename");
  221. // We exit in order to avoid fork bombing.
  222. exit(0);
  223. }
  224. }
  225. $last_checkpoint = time();
  226. }
  227. sleep(1);
  228. }
  229. ?>