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