]>
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 AD |
11 | define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon'); |
12 | define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon'); | |
13 | define('DISABLE_SESSIONS', true); | |
02008cb1 | 14 | |
ffa7cbae AD |
15 | require_once "version.php"; |
16 | ||
010c16f1 | 17 | if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) { |
ffa7cbae AD |
18 | define('DAEMON_EXTENDED_DEBUG', true); |
19 | } | |
20 | ||
21 | define('PURGE_INTERVAL', 3600); // seconds | |
22 | ||
23 | require_once "sanity_check.php"; | |
24 | require_once "config.php"; | |
25 | ||
98f70418 AD |
26 | define('MAX_JOBS', 2); |
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 AD |
45 | $children = array(); |
46 | ||
02008cb1 AD |
47 | $last_checkpoint = -1; |
48 | ||
5a613536 | 49 | function reap_children() { |
e9338405 AD |
50 | global $children; |
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 | } | |
60 | } | |
61 | ||
62 | $children = $tmp; | |
63 | ||
5a613536 AD |
64 | return count($tmp); |
65 | } | |
66 | ||
67 | function sigalrm_handler() { | |
a65a05a7 | 68 | die("[SIGALRM] hang in feed update?\n"); |
5a613536 AD |
69 | } |
70 | ||
71 | function sigchld_handler($signal) { | |
72 | $running_jobs = reap_children(); | |
e9338405 AD |
73 | |
74 | _debug("[SIGCHLD] jobs left: $running_jobs"); | |
5a613536 | 75 | |
02008cb1 AD |
76 | pcntl_waitpid(-1, $status, WNOHANG); |
77 | } | |
78 | ||
6a69e61f AD |
79 | function sigint_handler() { |
80 | unlink(LOCK_DIRECTORY . "/update_daemon.lock"); | |
a65a05a7 | 81 | die("[SIGINT] removing lockfile and exiting.\n"); |
6a69e61f AD |
82 | } |
83 | ||
ffa7cbae | 84 | pcntl_signal(SIGALRM, 'sigalrm_handler'); |
02008cb1 | 85 | pcntl_signal(SIGCHLD, 'sigchld_handler'); |
6a69e61f | 86 | |
884c0a36 AD |
87 | if (file_is_locked("update_daemon.lock")) { |
88 | die("error: Can't create lockfile. ". | |
6a69e61f AD |
89 | "Maybe another daemon is already running.\n"); |
90 | } | |
02008cb1 | 91 | |
884c0a36 | 92 | if (!pcntl_fork()) { |
0d6a7147 AD |
93 | pcntl_signal(SIGINT, 'sigint_handler'); |
94 | ||
45004d43 | 95 | // Try to lock a file in order to avoid concurrent update. |
884c0a36 AD |
96 | $lock_handle = make_lockfile("update_daemon.lock"); |
97 | ||
98 | if (!$lock_handle) { | |
99 | die("error: Can't create lockfile. ". | |
100 | "Maybe another daemon is already running.\n"); | |
101 | } | |
102 | ||
103 | while (true) { sleep(100); } | |
104 | } | |
105 | ||
ffa7cbae AD |
106 | // Testing database connection. |
107 | // It is unnecessary to start the fork loop if database is not ok. | |
108 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
109 | ||
110 | if (!$link) { | |
111 | if (DB_TYPE == "mysql") { | |
112 | print mysql_error(); | |
113 | } | |
114 | // PG seems to display its own errors just fine by default. | |
115 | return; | |
116 | } | |
117 | ||
118 | db_close($link); | |
119 | ||
02008cb1 AD |
120 | while (true) { |
121 | ||
45004d43 AD |
122 | // Since sleep is interupted by SIGCHLD, we need another way to |
123 | // respect the SPAWN_INTERVAL | |
02008cb1 AD |
124 | $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); |
125 | ||
ce1aa9b7 | 126 | if ($next_spawn % 10 == 0) { |
e9338405 AD |
127 | $running_jobs = count($children); |
128 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
ce1aa9b7 | 129 | } |
02008cb1 AD |
130 | |
131 | if ($last_checkpoint + SPAWN_INTERVAL < time()) { | |
132 | ||
5a613536 AD |
133 | reap_children(); |
134 | ||
e9338405 | 135 | for ($j = count($children); $j < MAX_JOBS; $j++) { |
02008cb1 AD |
136 | $pid = pcntl_fork(); |
137 | if ($pid == -1) { | |
138 | die("fork failed!\n"); | |
139 | } else if ($pid) { | |
e9338405 AD |
140 | _debug("[MASTER] spawned client $j [PID:$pid]..."); |
141 | array_push($children, $pid); | |
02008cb1 AD |
142 | } else { |
143 | pcntl_signal(SIGCHLD, SIG_IGN); | |
6a69e61f | 144 | pcntl_signal(SIGINT, SIG_DFL); |
ffa7cbae AD |
145 | |
146 | // ****** Updating RSS code ******* | |
147 | // Only run in fork process. | |
148 | ||
149 | $start_timestamp = time(); | |
150 | ||
151 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); | |
152 | ||
153 | if (!$link) { | |
154 | if (DB_TYPE == "mysql") { | |
155 | print mysql_error(); | |
156 | } | |
157 | // PG seems to display its own errors just fine by default. | |
158 | return; | |
159 | } | |
160 | ||
f29ba148 | 161 | init_connection($link); |
ffa7cbae AD |
162 | |
163 | // We disable stamp file, since it is of no use in a multiprocess update. | |
164 | // not really, tho for the time being -fox | |
165 | if (!make_stampfile('update_daemon.stamp')) { | |
166 | print "warning: unable to create stampfile"; | |
167 | } | |
168 | ||
ffa7cbae AD |
169 | // FIXME : $last_purge is of no use in a multiprocess update. |
170 | // FIXME : We ALWAYS purge old posts. | |
3907ef71 AD |
171 | //_debug("Purging old posts (random 30 feeds)..."); |
172 | //global_purge_old_posts($link, true, 30); | |
ffa7cbae | 173 | |
e3b54693 AD |
174 | // Call to the feed batch update function |
175 | // or regenerate feedbrowser cache | |
176 | ||
177 | if (rand(0,100) > 50) { | |
178 | update_daemon_common($link); | |
179 | } else { | |
180 | $count = update_feedbrowser_cache($link); | |
181 | _debug("Finished, $count feeds processed."); | |
182 | } | |
ffa7cbae | 183 | |
0d6a7147 | 184 | _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)"); |
abfa57fd | 185 | |
ffa7cbae AD |
186 | db_close($link); |
187 | ||
188 | // We are in a fork. | |
189 | // We wait a little before exiting to avoid to be faster than our parent process. | |
190 | sleep(1); | |
191 | // We exit in order to avoid fork bombing. | |
02008cb1 AD |
192 | exit(0); |
193 | } | |
0d6a7147 AD |
194 | |
195 | // We wait a little time before the next fork, in order to let the first fork | |
196 | // mark the feeds it update : | |
197 | sleep(1); | |
02008cb1 AD |
198 | } |
199 | $last_checkpoint = time(); | |
200 | } | |
201 | sleep(1); | |
202 | } | |
203 | ||
204 | ?> |