]>
Commit | Line | Data |
---|---|---|
02008cb1 AD |
1 | #!/usr/bin/php |
2 | <?php | |
ffa7cbae AD |
3 | // This is an experimental multiprocess update daemon. |
4 | // Some configurable variable may be found below. | |
5 | ||
6 | // define('DEFAULT_ERROR_LEVEL', E_ALL); | |
7 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
02008cb1 AD |
8 | |
9 | declare(ticks = 1); | |
10 | ||
ffa7cbae | 11 | define('DISABLE_SESSIONS', true); |
02008cb1 | 12 | |
ffa7cbae AD |
13 | require_once "version.php"; |
14 | ||
010c16f1 | 15 | if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) { |
ffa7cbae AD |
16 | define('DAEMON_EXTENDED_DEBUG', true); |
17 | } | |
18 | ||
19 | define('PURGE_INTERVAL', 3600); // seconds | |
c90a028c | 20 | define('MAX_CHILD_RUNTIME', 600); // seconds |
ffa7cbae AD |
21 | |
22 | require_once "sanity_check.php"; | |
23 | require_once "config.php"; | |
24 | ||
98f70418 | 25 | define('MAX_JOBS', 2); |
c90a028c | 26 | define('SPAWN_INTERVAL', 1); |
98f70418 | 27 | |
02008cb1 | 28 | define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); |
ffa7cbae | 29 | |
16956646 AD |
30 | if (!function_exists('pcntl_fork')) { |
31 | die("error: This script requires PHP compiled with PCNTL module.\n"); | |
32 | } | |
33 | ||
ffa7cbae | 34 | if (!ENABLE_UPDATE_DAEMON) { |
16956646 | 35 | die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n"); |
ffa7cbae AD |
36 | } |
37 | ||
38 | require_once "db.php"; | |
39 | require_once "db-prefs.php"; | |
40 | require_once "functions.php"; | |
816cdfb7 | 41 | require_once "lib/magpierss/rss_fetch.inc"; |
ffa7cbae AD |
42 | |
43 | error_reporting(DEFAULT_ERROR_LEVEL); | |
02008cb1 | 44 | |
e9338405 | 45 | $children = array(); |
c90a028c | 46 | $ctimes = array(); |
e9338405 | 47 | |
02008cb1 AD |
48 | $last_checkpoint = -1; |
49 | ||
5a613536 | 50 | function reap_children() { |
e9338405 AD |
51 | global $children; |
52 | ||
53 | $tmp = array(); | |
54 | ||
55 | foreach ($children as $pid) { | |
56 | if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { | |
57 | array_push($tmp, $pid); | |
58 | } else { | |
59 | _debug("[SIGCHLD] 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; | |
71 | ||
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..."); | |
77 | posix_kill($pid, SIGINT); | |
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 | ||
6a69e61f AD |
90 | function sigint_handler() { |
91 | unlink(LOCK_DIRECTORY . "/update_daemon.lock"); | |
a65a05a7 | 92 | die("[SIGINT] removing lockfile and exiting.\n"); |
6a69e61f AD |
93 | } |
94 | ||
02008cb1 | 95 | pcntl_signal(SIGCHLD, 'sigchld_handler'); |
6a69e61f | 96 | |
884c0a36 AD |
97 | if (file_is_locked("update_daemon.lock")) { |
98 | die("error: Can't create lockfile. ". | |
6a69e61f AD |
99 | "Maybe another daemon is already running.\n"); |
100 | } | |
02008cb1 | 101 | |
884c0a36 | 102 | if (!pcntl_fork()) { |
0d6a7147 AD |
103 | pcntl_signal(SIGINT, 'sigint_handler'); |
104 | ||
45004d43 | 105 | // Try to lock a file in order to avoid concurrent update. |
884c0a36 AD |
106 | $lock_handle = make_lockfile("update_daemon.lock"); |
107 | ||
108 | if (!$lock_handle) { | |
109 | die("error: Can't create lockfile. ". | |
110 | "Maybe another daemon is already running.\n"); | |
111 | } | |
112 | ||
113 | while (true) { sleep(100); } | |
114 | } | |
115 | ||
ffa7cbae AD |
116 | // Testing database connection. |
117 | // It is unnecessary to start the fork loop if database is not ok. | |
118 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
119 | ||
120 | if (!$link) { | |
121 | if (DB_TYPE == "mysql") { | |
122 | print mysql_error(); | |
123 | } | |
124 | // PG seems to display its own errors just fine by default. | |
125 | return; | |
126 | } | |
127 | ||
128 | db_close($link); | |
129 | ||
02008cb1 AD |
130 | while (true) { |
131 | ||
45004d43 AD |
132 | // Since sleep is interupted by SIGCHLD, we need another way to |
133 | // respect the SPAWN_INTERVAL | |
02008cb1 AD |
134 | $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); |
135 | ||
ce1aa9b7 | 136 | if ($next_spawn % 10 == 0) { |
e9338405 AD |
137 | $running_jobs = count($children); |
138 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
ce1aa9b7 | 139 | } |
02008cb1 AD |
140 | |
141 | if ($last_checkpoint + SPAWN_INTERVAL < time()) { | |
142 | ||
c90a028c | 143 | check_ctimes(); |
5a613536 AD |
144 | reap_children(); |
145 | ||
e9338405 | 146 | for ($j = count($children); $j < MAX_JOBS; $j++) { |
02008cb1 AD |
147 | $pid = pcntl_fork(); |
148 | if ($pid == -1) { | |
149 | die("fork failed!\n"); | |
150 | } else if ($pid) { | |
e9338405 AD |
151 | _debug("[MASTER] spawned client $j [PID:$pid]..."); |
152 | array_push($children, $pid); | |
c90a028c | 153 | $ctimes[$pid] = time(); |
02008cb1 AD |
154 | } else { |
155 | pcntl_signal(SIGCHLD, SIG_IGN); | |
6a69e61f | 156 | pcntl_signal(SIGINT, SIG_DFL); |
ffa7cbae AD |
157 | |
158 | // ****** Updating RSS code ******* | |
159 | // Only run in fork process. | |
160 | ||
161 | $start_timestamp = time(); | |
162 | ||
163 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
164 | ||
165 | if (!$link) { | |
166 | if (DB_TYPE == "mysql") { | |
167 | print mysql_error(); | |
168 | } | |
169 | // PG seems to display its own errors just fine by default. | |
170 | return; | |
171 | } | |
172 | ||
f29ba148 | 173 | init_connection($link); |
ffa7cbae AD |
174 | |
175 | // We disable stamp file, since it is of no use in a multiprocess update. | |
176 | // not really, tho for the time being -fox | |
177 | if (!make_stampfile('update_daemon.stamp')) { | |
178 | print "warning: unable to create stampfile"; | |
179 | } | |
180 | ||
ffa7cbae AD |
181 | // FIXME : $last_purge is of no use in a multiprocess update. |
182 | // FIXME : We ALWAYS purge old posts. | |
3907ef71 AD |
183 | //_debug("Purging old posts (random 30 feeds)..."); |
184 | //global_purge_old_posts($link, true, 30); | |
ffa7cbae | 185 | |
e3b54693 AD |
186 | // Call to the feed batch update function |
187 | // or regenerate feedbrowser cache | |
188 | ||
189 | if (rand(0,100) > 50) { | |
190 | update_daemon_common($link); | |
191 | } else { | |
192 | $count = update_feedbrowser_cache($link); | |
193 | _debug("Finished, $count feeds processed."); | |
194 | } | |
ffa7cbae | 195 | |
0d6a7147 | 196 | _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)"); |
abfa57fd | 197 | |
ffa7cbae AD |
198 | db_close($link); |
199 | ||
200 | // We are in a fork. | |
201 | // We wait a little before exiting to avoid to be faster than our parent process. | |
202 | sleep(1); | |
203 | // We exit in order to avoid fork bombing. | |
02008cb1 AD |
204 | exit(0); |
205 | } | |
0d6a7147 AD |
206 | |
207 | // We wait a little time before the next fork, in order to let the first fork | |
208 | // mark the feeds it update : | |
209 | sleep(1); | |
02008cb1 AD |
210 | } |
211 | $last_checkpoint = time(); | |
212 | } | |
213 | sleep(1); | |
214 | } | |
215 | ||
216 | ?> |