]> git.wh0rd.org - tt-rss.git/blame - update_daemon2.php
minor css fixes (mostly for zoom mode)
[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 11 require_once "version.php";
404e2e36 12 require_once "autoload.php";
9765e8b9 13 require_once "functions.php";
bd8ae98b 14 require_once "config.php";
0e6bdaef
AD
15
16 // defaults
17 define_default('PURGE_INTERVAL', 3600); // seconds
f90728cd 18 define_default('MAX_CHILD_RUNTIME', 1800); // seconds
0e6bdaef
AD
19 define_default('MAX_JOBS', 2);
20 define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds
0e6bdaef 21
ffa7cbae 22 require_once "sanity_check.php";
5893edd5
AD
23 require_once "db.php";
24 require_once "db-prefs.php";
ffa7cbae 25
16956646
AD
26 if (!function_exists('pcntl_fork')) {
27 die("error: This script requires PHP compiled with PCNTL module.\n");
28 }
29
45ce1610
AD
30 $options = getopt("");
31
6f61ba46
AD
32 if (!is_array($options)) {
33 die("error: getopt() failed. ".
34 "Most probably you are using PHP CGI to run this script ".
35 "instead of required PHP CLI. Check tt-rss wiki page on updating feeds for ".
36 "additional information.\n");
37 }
38
45ce1610 39
0f906745
AD
40 $master_handlers_installed = false;
41
e9338405 42 $children = array();
c90a028c 43 $ctimes = array();
e9338405 44
02008cb1
AD
45 $last_checkpoint = -1;
46
e552bb0b
AD
47 /**
48 * @SuppressWarnings(unused)
49 */
5a613536 50 function reap_children() {
e9338405 51 global $children;
51ddf0f8 52 global $ctimes;
e9338405
AD
53
54 $tmp = array();
55
56 foreach ($children as $pid) {
57 if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) {
7aabaa09
AD
58
59 if (file_is_locked("update_daemon-$pid.lock")) {
8ccaff02
AD
60 array_push($tmp, $pid);
61 } else {
c10a4306 62 Debug::log("[reap_children] child $pid seems active but lockfile is unlocked.");
125ab8db
AD
63 unset($ctimes[$pid]);
64
8ccaff02 65 }
e9338405 66 } else {
c10a4306 67 Debug::log("[reap_children] child $pid reaped.");
c90a028c 68 unset($ctimes[$pid]);
e9338405
AD
69 }
70 }
71
72 $children = $tmp;
73
5a613536
AD
74 return count($tmp);
75 }
76
c90a028c
AD
77 function check_ctimes() {
78 global $ctimes;
dbaa4e4a 79
c90a028c
AD
80 foreach (array_keys($ctimes) as $pid) {
81 $started = $ctimes[$pid];
82
83 if (time() - $started > MAX_CHILD_RUNTIME) {
c10a4306 84 Debug::log("[MASTER] child process $pid seems to be stuck, aborting...");
0618b81c 85 posix_kill($pid, SIGKILL);
c90a028c
AD
86 }
87 }
5a613536
AD
88 }
89
e552bb0b
AD
90 /**
91 * @SuppressWarnings(unused)
92 */
5a613536
AD
93 function sigchld_handler($signal) {
94 $running_jobs = reap_children();
e9338405 95
c10a4306 96 Debug::log("[SIGCHLD] jobs left: $running_jobs");
5a613536 97
02008cb1
AD
98 pcntl_waitpid(-1, $status, WNOHANG);
99 }
100
0f906745
AD
101 function shutdown($caller_pid) {
102 if ($caller_pid == posix_getpid()) {
103 if (file_exists(LOCK_DIRECTORY . "/update_daemon.lock")) {
c10a4306 104 Debug::log("removing lockfile (master)...");
0f906745
AD
105 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
106 }
107 }
108 }
109
110 function task_shutdown() {
111 $pid = posix_getpid();
112
113 if (file_exists(LOCK_DIRECTORY . "/update_daemon-$pid.lock")) {
c10a4306 114 Debug::log("removing lockfile ($pid)...");
0f906745
AD
115 unlink(LOCK_DIRECTORY . "/update_daemon-$pid.lock");
116 }
117 }
118
119 function sigint_handler() {
c10a4306 120 Debug::log("[MASTER] SIG_INT received.\n");
0f906745
AD
121 shutdown(posix_getpid());
122 die;
123 }
124
125 function task_sigint_handler() {
c10a4306 126 Debug::log("[TASK] SIG_INT received.\n");
0f906745
AD
127 task_shutdown();
128 die;
129 }
130
02008cb1 131 pcntl_signal(SIGCHLD, 'sigchld_handler');
6a69e61f 132
dc24b520 133 $longopts = array("log:",
c10a4306 134 "log-level:",
dc24b520 135 "tasks:",
ec25336d 136 "interval:",
db985423 137 "quiet",
dc24b520
AD
138 "help");
139
140 $options = getopt("", $longopts);
141
142 if (isset($options["help"]) ) {
143 print "Tiny Tiny RSS update daemon.\n\n";
144 print "Options:\n";
145 print " --log FILE - log messages to FILE\n";
c10a4306 146 print " --log-level N - log verbosity level\n";
dc24b520
AD
147 print " --tasks N - amount of update tasks to spawn\n";
148 print " default: " . MAX_JOBS . "\n";
149 print " --interval N - task spawn interval\n";
150 print " default: " . SPAWN_INTERVAL . " seconds.\n";
151 print " --quiet - don't output messages to stdout\n";
152 return;
153 }
154
c10a4306
AD
155 Debug::set_enabled(true);
156 Debug::set_quiet(isset($options['quiet']));
157
158 if (isset($options["log-level"])) {
159 Debug::set_loglevel((int)$options["log-level"]);
160 }
161
162 if (isset($options["log"])) {
163 Debug::set_logfile($options["log"]);
164 Debug::log("Logging to " . $options["log"]);
165 }
dc24b520
AD
166
167 if (isset($options["tasks"])) {
c10a4306 168 Debug::log("Set to spawn " . $options["tasks"] . " children.");
ec25336d 169 $max_jobs = $options["tasks"];
dc24b520
AD
170 } else {
171 $max_jobs = MAX_JOBS;
172 }
173
174 if (isset($options["interval"])) {
c10a4306 175 Debug::log("Spawn interval: " . $options["interval"] . " seconds.");
ec25336d 176 $spawn_interval = $options["interval"];
dc24b520
AD
177 } else {
178 $spawn_interval = SPAWN_INTERVAL;
179 }
180
4fd07908
AD
181 // let's enforce a minimum spawn interval as to not forkbomb the host
182 $spawn_interval = max(60, $spawn_interval);
c10a4306 183 Debug::log("Spawn interval: $spawn_interval sec");
dc24b520 184
884c0a36
AD
185 if (file_is_locked("update_daemon.lock")) {
186 die("error: Can't create lockfile. ".
6a69e61f
AD
187 "Maybe another daemon is already running.\n");
188 }
02008cb1 189
1a43a68c
AD
190 // Try to lock a file in order to avoid concurrent update.
191 $lock_handle = make_lockfile("update_daemon.lock");
192
193 if (!$lock_handle) {
194 die("error: Can't create lockfile. ".
195 "Maybe another daemon is already running.\n");
196 }
197
6322ac79 198 $schema_version = get_schema_version();
4c2da349 199
857efe49
AD
200 if ($schema_version != SCHEMA_VERSION) {
201 die("Schema version is wrong, please upgrade the database.\n");
202 }
203
e2cf81e2
AD
204 // Protip: children close shared database handle when terminating, it's a bad idea to
205 // do database stuff on main process from now on.
206
02008cb1
AD
207 while (true) {
208
45004d43 209 // Since sleep is interupted by SIGCHLD, we need another way to
dc24b520
AD
210 // respect the spawn interval
211 $next_spawn = $last_checkpoint + $spawn_interval - time();
02008cb1 212
61aa7499 213 if ($next_spawn % 60 == 0) {
e9338405 214 $running_jobs = count($children);
c10a4306 215 Debug::log("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
ce1aa9b7 216 }
02008cb1 217
dc24b520 218 if ($last_checkpoint + $spawn_interval < time()) {
c90a028c 219 check_ctimes();
5a613536
AD
220 reap_children();
221
dc24b520 222 for ($j = count($children); $j < $max_jobs; $j++) {
02008cb1
AD
223 $pid = pcntl_fork();
224 if ($pid == -1) {
225 die("fork failed!\n");
226 } else if ($pid) {
0f906745
AD
227
228 if (!$master_handlers_installed) {
c10a4306 229 Debug::log("[MASTER] installing shutdown handlers");
0f906745
AD
230 pcntl_signal(SIGINT, 'sigint_handler');
231 pcntl_signal(SIGTERM, 'sigint_handler');
232 register_shutdown_function('shutdown', posix_getpid());
233 $master_handlers_installed = true;
234 }
235
c10a4306 236 Debug::log("[MASTER] spawned client $j [PID:$pid]...");
e9338405 237 array_push($children, $pid);
c90a028c 238 $ctimes[$pid] = time();
02008cb1
AD
239 } else {
240 pcntl_signal(SIGCHLD, SIG_IGN);
0f906745
AD
241 pcntl_signal(SIGINT, 'task_sigint_handler');
242
243 register_shutdown_function('task_shutdown');
8ccaff02 244
7440a7fe 245 $quiet = (isset($options["quiet"])) ? "--quiet" : "";
a33558a6 246 $log = function_exists("flock") && isset($options['log']) ? '--log '.$options['log'] : '';
8292d05b 247
7440a7fe 248 $my_pid = posix_getpid();
dbaa4e4a 249
a33558a6 250 passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet $log --task $j --pidlock $my_pid");
a26f0c17 251
ffa7cbae 252 sleep(1);
8ccaff02 253
ffa7cbae 254 // We exit in order to avoid fork bombing.
02008cb1
AD
255 exit(0);
256 }
257 }
258 $last_checkpoint = time();
259 }
260 sleep(1);
261 }
262
263?>