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