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