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