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