]> git.wh0rd.org - tt-rss.git/blob - update_daemon2.php
fix attempt to insert user with null pwd_hash when auto creating (fixes #412)
[tt-rss.git] / update_daemon2.php
1 #!/usr/bin/php
2 <?php
3 set_include_path(get_include_path() . PATH_SEPARATOR .
4 dirname(__FILE__) . "/include");
5
6 // This is an experimental multiprocess update daemon.
7 // Some configurable variable may be found below.
8
9 declare(ticks = 1);
10 chdir(dirname(__FILE__));
11
12 define('DISABLE_SESSIONS', true);
13
14 require_once "version.php";
15
16 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
17 define('DAEMON_EXTENDED_DEBUG', true);
18 }
19
20 define('PURGE_INTERVAL', 3600); // seconds
21 define('MAX_CHILD_RUNTIME', 600); // seconds
22
23 require_once "sanity_check.php";
24 require_once "config.php";
25 require_once "db.php";
26 require_once "db-prefs.php";
27 require_once "functions.php";
28 require_once "rssfuncs.php";
29 require_once "lib/magpierss/rss_fetch.inc";
30
31 define('MAX_JOBS', 2);
32 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL);
33
34 if (!function_exists('pcntl_fork')) {
35 die("error: This script requires PHP compiled with PCNTL module.\n");
36 }
37
38 $children = array();
39 $ctimes = array();
40
41 $last_checkpoint = -1;
42
43 function reap_children() {
44 global $children;
45 global $ctimes;
46
47 $tmp = array();
48
49 foreach ($children as $pid) {
50 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
51
52 if (file_is_locked("update_daemon-$pid.lock")) {
53 array_push($tmp, $pid);
54 } else {
55 _debug("[reap_children] child $pid seems active but lockfile is unlocked.");
56 }
57 } else {
58 _debug("[reap_children] 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 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
101 function sigint_handler() {
102 shutdown();
103 die("[SIGINT] removing lockfile and exiting.\n");
104 }
105
106 function task_sigint_handler() {
107 task_shutdown();
108 die("[SIGINT] removing lockfile and exiting.\n");
109 }
110
111 pcntl_signal(SIGCHLD, 'sigchld_handler');
112
113 if (file_is_locked("update_daemon.lock")) {
114 die("error: Can't create lockfile. ".
115 "Maybe another daemon is already running.\n");
116 }
117
118 if (!pcntl_fork()) {
119 pcntl_signal(SIGINT, 'sigint_handler');
120 register_shutdown_function('shutdown');
121
122 // Try to lock a file in order to avoid concurrent update.
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
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 (!init_connection($link)) return;
138
139 db_close($link);
140
141 while (true) {
142
143 // Since sleep is interupted by SIGCHLD, we need another way to
144 // respect the SPAWN_INTERVAL
145 $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time();
146
147 if ($next_spawn % 10 == 0) {
148 $running_jobs = count($children);
149 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
150 }
151
152 if ($last_checkpoint + SPAWN_INTERVAL < time()) {
153
154 check_ctimes();
155 reap_children();
156
157 for ($j = count($children); $j < MAX_JOBS; $j++) {
158 $pid = pcntl_fork();
159 if ($pid == -1) {
160 die("fork failed!\n");
161 } else if ($pid) {
162 _debug("[MASTER] spawned client $j [PID:$pid]...");
163 array_push($children, $pid);
164 $ctimes[$pid] = time();
165 } else {
166 pcntl_signal(SIGCHLD, SIG_IGN);
167 pcntl_signal(SIGINT, 'task_sigint_handler');
168
169 register_shutdown_function('task_shutdown');
170
171 $my_pid = posix_getpid();
172 $lock_filename = "update_daemon-$my_pid.lock";
173
174 $lock_handle = make_lockfile($lock_filename);
175
176 if (!$lock_handle) {
177 die("error: Can't create lockfile ($lock_filename). ".
178 "Maybe another daemon is already running.\n");
179 }
180
181 // ****** Updating RSS code *******
182 // Only run in fork process.
183
184 $start_timestamp = time();
185
186 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
187
188 if (!init_connection($link)) return;
189
190 // We disable stamp file, since it is of no use in a multiprocess update.
191 // not really, tho for the time being -fox
192 if (!make_stampfile('update_daemon.stamp')) {
193 print "warning: unable to create stampfile";
194 }
195
196 // Call to the feed batch update function
197 // or regenerate feedbrowser cache
198
199 if (rand(0,100) > 30) {
200 update_daemon_common($link);
201 } else {
202 $count = update_feedbrowser_cache($link);
203 _debug("Feedbrowser updated, $count feeds processed.");
204
205 purge_orphans($link, true);
206
207 $rc = cleanup_tags($link, 14, 50000);
208
209 _debug("Cleaned $rc cached tags.");
210
211 _debug("Updating linked feeds...");
212 get_linked_feeds($link);
213
214 }
215
216 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
217
218 db_close($link);
219
220 // We are in a fork.
221 // We wait a little before exiting to avoid to be faster than our parent process.
222 sleep(1);
223
224 unlink(LOCK_DIRECTORY . "/$lock_filename");
225
226 // We exit in order to avoid fork bombing.
227 exit(0);
228 }
229
230 // We wait a little time before the next fork, in order to let the first fork
231 // mark the feeds it update :
232 sleep(1);
233 }
234 $last_checkpoint = time();
235 }
236 sleep(1);
237 }
238
239 ?>