]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
fix help not loading on ? press
[tt-rss.git] / update_daemon2.php
CommitLineData
02008cb1
AD
1#!/usr/bin/php
2<?php
f03a795d
AD
3 set_include_path(get_include_path() . PATH_SEPARATOR .
4 dirname(__FILE__) . "/include");
107d0cf3 5
ffa7cbae
AD
6 // This is an experimental multiprocess update daemon.
7 // Some configurable variable may be found below.
8
02008cb1 9 declare(ticks = 1);
9b27cec8 10 chdir(dirname(__FILE__));
02008cb1 11
ffa7cbae 12 define('DISABLE_SESSIONS', true);
02008cb1 13
ffa7cbae
AD
14 require_once "version.php";
15
010c16f1 16 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
ffa7cbae
AD
17 define('DAEMON_EXTENDED_DEBUG', true);
18 }
19
20 define('PURGE_INTERVAL', 3600); // seconds
c90a028c 21 define('MAX_CHILD_RUNTIME', 600); // seconds
ffa7cbae
AD
22
23 require_once "sanity_check.php";
24 require_once "config.php";
25
98f70418 26 define('MAX_JOBS', 2);
02008cb1 27 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
ffa7cbae 28
16956646
AD
29 if (!function_exists('pcntl_fork')) {
30 die("error: This script requires PHP compiled with PCNTL module.\n");
31 }
32
ffa7cbae
AD
33 require_once "db.php";
34 require_once "db-prefs.php";
35 require_once "functions.php";
2c08214a 36 require_once "rssfuncs.php";
816cdfb7 37 require_once "lib/magpierss/rss_fetch.inc";
ffa7cbae 38
e9338405 39 $children = array();
c90a028c 40 $ctimes = array();
e9338405 41
02008cb1
AD
42 $last_checkpoint = -1;
43
5a613536 44 function reap_children() {
e9338405 45 global $children;
51ddf0f8 46 global $ctimes;
e9338405
AD
47
48 $tmp = array();
49
50 foreach ($children as $pid) {
51 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
7aabaa09
AD
52
53 if (file_is_locked("update_daemon-$pid.lock")) {
8ccaff02
AD
54 array_push($tmp, $pid);
55 } else {
56 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
57 }
e9338405 58 } else {
8ccaff02 59 _debug("[reap_children] child $pid reaped.");
c90a028c 60 unset($ctimes[$pid]);
e9338405
AD
61 }
62 }
63
64 $children = $tmp;
65
5a613536
AD
66 return count($tmp);
67 }
68
c90a028c
AD
69 function check_ctimes() {
70 global $ctimes;
dbaa4e4a 71
c90a028c
AD
72 foreach (array_keys($ctimes) as $pid) {
73 $started = $ctimes[$pid];
74
75 if (time() - $started > MAX_CHILD_RUNTIME) {
76 _debug("[MASTER] child process $pid seems to be stuck, aborting...");
0618b81c 77 posix_kill($pid, SIGKILL);
c90a028c
AD
78 }
79 }
5a613536
AD
80 }
81
82 function sigchld_handler($signal) {
83 $running_jobs = reap_children();
e9338405
AD
84
85 _debug("[SIGCHLD] jobs left: $running_jobs");
5a613536 86
02008cb1
AD
87 pcntl_waitpid(-1, $status, WNOHANG);
88 }
89
8ccaff02
AD
90 function shutdown() {
91 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock"))
92 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
93 }
94
95 function task_shutdown() {
96 $pid = posix_getpid();
97
98 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock"))
99 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
100 }
101
6a69e61f 102 function sigint_handler() {
8ccaff02 103 shutdown();
a65a05a7 104 die("[SIGINT] removing lockfile and exiting.\n");
dbaa4e4a 105 }
8ccaff02
AD
106
107 function task_sigint_handler() {
108 task_shutdown();
109 die("[SIGINT] removing lockfile and exiting.\n");
dbaa4e4a 110 }
6a69e61f 111
02008cb1 112 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 113
884c0a36
AD
114 if (file_is_locked("update_daemon.lock")) {
115 die("error: Can't create lockfile. ".
6a69e61f
AD
116 "Maybe another daemon is already running.\n");
117 }
02008cb1 118
884c0a36 119 if (!pcntl_fork()) {
0d6a7147 120 pcntl_signal(SIGINT, 'sigint_handler');
8ccaff02 121 register_shutdown_function('shutdown');
0d6a7147 122
45004d43 123 // Try to lock a file in order to avoid concurrent update.
884c0a36
AD
124 $lock_handle = make_lockfile("update_daemon.lock");
125
126 if (!$lock_handle) {
127 die("error: Can't create lockfile. ".
128 "Maybe another daemon is already running.\n");
129 }
130
131 while (true) { sleep(100); }
132 }
133
ffa7cbae
AD
134 // Testing database connection.
135 // It is unnecessary to start the fork loop if database is not ok.
dbaa4e4a 136 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
ffa7cbae 137
5f0a3741 138 if (!init_connection($link)) return;
ffa7cbae
AD
139
140 db_close($link);
141
02008cb1
AD
142 while (true) {
143
45004d43
AD
144 // Since sleep is interupted by SIGCHLD, we need another way to
145 // respect the SPAWN_INTERVAL
02008cb1
AD
146 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
147
ce1aa9b7 148 if ($next_spawn % 10 == 0) {
e9338405
AD
149 $running_jobs = count($children);
150 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 151 }
02008cb1
AD
152
153 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
154
c90a028c 155 check_ctimes();
5a613536
AD
156 reap_children();
157
e9338405 158 for ($j = count($children); $j < MAX_JOBS; $j++) {
02008cb1
AD
159 $pid = pcntl_fork();
160 if ($pid == -1) {
161 die("fork failed!\n");
162 } else if ($pid) {
e9338405
AD
163 _debug("[MASTER] spawned client $j [PID:$pid]...");
164 array_push($children, $pid);
c90a028c 165 $ctimes[$pid] = time();
02008cb1
AD
166 } else {
167 pcntl_signal(SIGCHLD, SIG_IGN);
8ccaff02
AD
168 pcntl_signal(SIGINT, 'task_sigint_handler');
169
170 register_shutdown_function('task_shutdown');
171
172 $my_pid = posix_getpid();
173 $lock_filename = "update_daemon-$my_pid.lock";
174
175 $lock_handle = make_lockfile($lock_filename);
176
177 if (!$lock_handle) {
178 die("error: Can't create lockfile ($lock_filename). ".
179 "Maybe another daemon is already running.\n");
180 }
ffa7cbae
AD
181
182 // ****** Updating RSS code *******
183 // Only run in fork process.
184
185 $start_timestamp = time();
186
dbaa4e4a 187 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
ffa7cbae 188
5f0a3741 189 if (!init_connection($link)) return;
ffa7cbae
AD
190
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 print "warning: unable to create stampfile";
dbaa4e4a 195 }
ffa7cbae 196
dbaa4e4a 197 // Call to the feed batch update function
e3b54693
AD
198 // or regenerate feedbrowser cache
199
8ccaff02 200 if (rand(0,100) > 30) {
e3b54693
AD
201 update_daemon_common($link);
202 } else {
203 $count = update_feedbrowser_cache($link);
e3b42c5a
AD
204 _debug("Feedbrowser updated, $count feeds processed.");
205
206 purge_orphans($link, true);
207
208 $rc = cleanup_tags($link, 14, 50000);
209
210 _debug("Cleaned $rc cached tags.");
211
ae5f7bb1
AD
212 _debug("Updating linked feeds...");
213 get_linked_feeds($link);
214
e3b54693 215 }
ffa7cbae 216
0d6a7147 217 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
dbaa4e4a 218
ffa7cbae
AD
219 db_close($link);
220
221 // We are in a fork.
222 // We wait a little before exiting to avoid to be faster than our parent process.
223 sleep(1);
8ccaff02
AD
224
225 unlink(LOCK_DIRECTORY . "/$lock_filename");
226
ffa7cbae 227 // We exit in order to avoid fork bombing.
02008cb1
AD
228 exit(0);
229 }
0d6a7147
AD
230
231 // We wait a little time before the next fork, in order to let the first fork
232 // mark the feeds it update :
233 sleep(1);
02008cb1
AD
234 }
235 $last_checkpoint = time();
236 }
237 sleep(1);
238 }
239
240?>