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