]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/php | |
2 | <?php | |
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); | |
8 | ||
9 | declare(ticks = 1); | |
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 | if (!ENABLE_UPDATE_DAEMON) { | |
33 | die("error: Please enable option ENABLE_UPDATE_DAEMON in config.php\n"); | |
34 | } | |
35 | ||
36 | require_once "db.php"; | |
37 | require_once "db-prefs.php"; | |
38 | require_once "functions.php"; | |
39 | require_once "lib/magpierss/rss_fetch.inc"; | |
40 | ||
41 | error_reporting(DEFAULT_ERROR_LEVEL); | |
42 | ||
43 | $children = array(); | |
44 | $ctimes = array(); | |
45 | ||
46 | $last_checkpoint = -1; | |
47 | ||
48 | function reap_children() { | |
49 | global $children; | |
50 | global $ctimes; | |
51 | ||
52 | $tmp = array(); | |
53 | ||
54 | foreach ($children as $pid) { | |
55 | if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { | |
56 | array_push($tmp, $pid); | |
57 | } else { | |
58 | _debug("[SIGCHLD] child $pid reaped."); | |
59 | unset($ctimes[$pid]); | |
60 | } | |
61 | } | |
62 | ||
63 | $children = $tmp; | |
64 | ||
65 | return count($tmp); | |
66 | } | |
67 | ||
68 | function check_ctimes() { | |
69 | global $ctimes; | |
70 | ||
71 | foreach (array_keys($ctimes) as $pid) { | |
72 | $started = $ctimes[$pid]; | |
73 | ||
74 | if (time() - $started > MAX_CHILD_RUNTIME) { | |
75 | _debug("[MASTER] child process $pid seems to be stuck, aborting..."); | |
76 | posix_kill($pid, SIGKILL); | |
77 | } | |
78 | } | |
79 | } | |
80 | ||
81 | function sigchld_handler($signal) { | |
82 | $running_jobs = reap_children(); | |
83 | ||
84 | _debug("[SIGCHLD] jobs left: $running_jobs"); | |
85 | ||
86 | pcntl_waitpid(-1, $status, WNOHANG); | |
87 | } | |
88 | ||
89 | function sigint_handler() { | |
90 | unlink(LOCK_DIRECTORY . "/update_daemon.lock"); | |
91 | die("[SIGINT] removing lockfile and exiting.\n"); | |
92 | } | |
93 | ||
94 | pcntl_signal(SIGCHLD, 'sigchld_handler'); | |
95 | ||
96 | if (file_is_locked("update_daemon.lock")) { | |
97 | die("error: Can't create lockfile. ". | |
98 | "Maybe another daemon is already running.\n"); | |
99 | } | |
100 | ||
101 | if (!pcntl_fork()) { | |
102 | pcntl_signal(SIGINT, 'sigint_handler'); | |
103 | ||
104 | // Try to lock a file in order to avoid concurrent update. | |
105 | $lock_handle = make_lockfile("update_daemon.lock"); | |
106 | ||
107 | if (!$lock_handle) { | |
108 | die("error: Can't create lockfile. ". | |
109 | "Maybe another daemon is already running.\n"); | |
110 | } | |
111 | ||
112 | while (true) { sleep(100); } | |
113 | } | |
114 | ||
115 | // Testing database connection. | |
116 | // It is unnecessary to start the fork loop if database is not ok. | |
117 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
118 | ||
119 | if (!$link) { | |
120 | if (DB_TYPE == "mysql") { | |
121 | print mysql_error(); | |
122 | } | |
123 | // PG seems to display its own errors just fine by default. | |
124 | return; | |
125 | } | |
126 | ||
127 | db_close($link); | |
128 | ||
129 | while (true) { | |
130 | ||
131 | // Since sleep is interupted by SIGCHLD, we need another way to | |
132 | // respect the SPAWN_INTERVAL | |
133 | $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); | |
134 | ||
135 | if ($next_spawn % 10 == 0) { | |
136 | $running_jobs = count($children); | |
137 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
138 | } | |
139 | ||
140 | if ($last_checkpoint + SPAWN_INTERVAL < time()) { | |
141 | ||
142 | check_ctimes(); | |
143 | reap_children(); | |
144 | ||
145 | for ($j = count($children); $j < MAX_JOBS; $j++) { | |
146 | $pid = pcntl_fork(); | |
147 | if ($pid == -1) { | |
148 | die("fork failed!\n"); | |
149 | } else if ($pid) { | |
150 | _debug("[MASTER] spawned client $j [PID:$pid]..."); | |
151 | array_push($children, $pid); | |
152 | $ctimes[$pid] = time(); | |
153 | } else { | |
154 | pcntl_signal(SIGCHLD, SIG_IGN); | |
155 | pcntl_signal(SIGINT, SIG_DFL); | |
156 | ||
157 | // ****** Updating RSS code ******* | |
158 | // Only run in fork process. | |
159 | ||
160 | $start_timestamp = time(); | |
161 | ||
162 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
163 | ||
164 | if (!$link) { | |
165 | if (DB_TYPE == "mysql") { | |
166 | print mysql_error(); | |
167 | } | |
168 | // PG seems to display its own errors just fine by default. | |
169 | return; | |
170 | } | |
171 | ||
172 | init_connection($link); | |
173 | ||
174 | // We disable stamp file, since it is of no use in a multiprocess update. | |
175 | // not really, tho for the time being -fox | |
176 | if (!make_stampfile('update_daemon.stamp')) { | |
177 | print "warning: unable to create stampfile"; | |
178 | } | |
179 | ||
180 | // Call to the feed batch update function | |
181 | // or regenerate feedbrowser cache | |
182 | ||
183 | if (rand(0,100) > 50) { | |
184 | update_daemon_common($link); | |
185 | } else { | |
186 | $count = update_feedbrowser_cache($link); | |
187 | _debug("Finished, $count feeds processed."); | |
188 | } | |
189 | ||
190 | _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)"); | |
191 | ||
192 | db_close($link); | |
193 | ||
194 | // We are in a fork. | |
195 | // We wait a little before exiting to avoid to be faster than our parent process. | |
196 | sleep(1); | |
197 | // We exit in order to avoid fork bombing. | |
198 | exit(0); | |
199 | } | |
200 | ||
201 | // We wait a little time before the next fork, in order to let the first fork | |
202 | // mark the feeds it update : | |
203 | sleep(1); | |
204 | } | |
205 | $last_checkpoint = time(); | |
206 | } | |
207 | sleep(1); | |
208 | } | |
209 | ||
210 | ?> |