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