]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env php | |
2 | <?php | |
3 | set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR . | |
4 | get_include_path()); | |
5 | ||
6 | declare(ticks = 1); | |
7 | chdir(dirname(__FILE__)); | |
8 | ||
9 | define('DISABLE_SESSIONS', true); | |
10 | ||
11 | require_once "version.php"; | |
12 | require_once "config.php"; | |
13 | require_once "autoload.php"; | |
14 | require_once "functions.php"; | |
15 | require_once "rssfuncs.php"; | |
16 | ||
17 | // defaults | |
18 | define_default('PURGE_INTERVAL', 3600); // seconds | |
19 | define_default('MAX_CHILD_RUNTIME', 1800); // seconds | |
20 | define_default('MAX_JOBS', 2); | |
21 | define_default('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL); // seconds | |
22 | ||
23 | require_once "sanity_check.php"; | |
24 | require_once "db.php"; | |
25 | require_once "db-prefs.php"; | |
26 | ||
27 | ||
28 | if (!function_exists('pcntl_fork')) { | |
29 | die("error: This script requires PHP compiled with PCNTL module.\n"); | |
30 | } | |
31 | ||
32 | $children = array(); | |
33 | $ctimes = array(); | |
34 | ||
35 | $last_checkpoint = -1; | |
36 | ||
37 | function reap_children() { | |
38 | global $children; | |
39 | global $ctimes; | |
40 | ||
41 | $tmp = array(); | |
42 | ||
43 | foreach ($children as $pid) { | |
44 | if (pcntl_waitpid($pid, $status, WNOHANG) != $pid) { | |
45 | ||
46 | if (file_is_locked("update_daemon-$pid.lock")) { | |
47 | array_push($tmp, $pid); | |
48 | } else { | |
49 | _debug("[reap_children] child $pid seems active but lockfile is unlocked."); | |
50 | unset($ctimes[$pid]); | |
51 | ||
52 | } | |
53 | } else { | |
54 | _debug("[reap_children] child $pid reaped."); | |
55 | unset($ctimes[$pid]); | |
56 | } | |
57 | } | |
58 | ||
59 | $children = $tmp; | |
60 | ||
61 | return count($tmp); | |
62 | } | |
63 | ||
64 | function check_ctimes() { | |
65 | global $ctimes; | |
66 | ||
67 | foreach (array_keys($ctimes) as $pid) { | |
68 | $started = $ctimes[$pid]; | |
69 | ||
70 | if (time() - $started > MAX_CHILD_RUNTIME) { | |
71 | _debug("[MASTER] child process $pid seems to be stuck, aborting..."); | |
72 | posix_kill($pid, SIGKILL); | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | function sigchld_handler($signal) { | |
78 | $running_jobs = reap_children(); | |
79 | ||
80 | _debug("[SIGCHLD] jobs left: $running_jobs"); | |
81 | ||
82 | pcntl_waitpid(-1, $status, WNOHANG); | |
83 | } | |
84 | ||
85 | pcntl_signal(SIGCHLD, 'sigchld_handler'); | |
86 | ||
87 | $longopts = array("log:", | |
88 | "tasks:", | |
89 | "interval:", | |
90 | "quiet", | |
91 | "help"); | |
92 | ||
93 | $options = getopt("", $longopts); | |
94 | ||
95 | if (isset($options["help"]) ) { | |
96 | print "Tiny Tiny RSS update daemon.\n\n"; | |
97 | print "Options:\n"; | |
98 | print " --log FILE - log messages to FILE\n"; | |
99 | print " --tasks N - amount of update tasks to spawn\n"; | |
100 | print " default: " . MAX_JOBS . "\n"; | |
101 | print " --interval N - task spawn interval\n"; | |
102 | print " default: " . SPAWN_INTERVAL . " seconds.\n"; | |
103 | print " --quiet - don't output messages to stdout\n"; | |
104 | return; | |
105 | } | |
106 | ||
107 | define('QUIET', isset($options['quiet'])); | |
108 | ||
109 | if (isset($options["tasks"])) { | |
110 | _debug("Set to spawn " . $options["tasks"] . " children."); | |
111 | $max_jobs = $options["tasks"]; | |
112 | } else { | |
113 | $max_jobs = MAX_JOBS; | |
114 | } | |
115 | ||
116 | if (isset($options["interval"])) { | |
117 | _debug("Spawn interval: " . $options["interval"] . " seconds."); | |
118 | $spawn_interval = $options["interval"]; | |
119 | } else { | |
120 | $spawn_interval = SPAWN_INTERVAL; | |
121 | } | |
122 | ||
123 | if (isset($options["log"])) { | |
124 | _debug("Logging to " . $options["log"]); | |
125 | define('LOGFILE', $options["log"]); | |
126 | } | |
127 | ||
128 | if (file_is_locked("update_daemon.lock")) { | |
129 | die("error: Can't create lockfile. ". | |
130 | "Maybe another daemon is already running.\n"); | |
131 | } | |
132 | ||
133 | // Try to lock a file in order to avoid concurrent update. | |
134 | $lock_handle = make_lockfile("update_daemon.lock"); | |
135 | ||
136 | if (!$lock_handle) { | |
137 | die("error: Can't create lockfile. ". | |
138 | "Maybe another daemon is already running.\n"); | |
139 | } | |
140 | ||
141 | $schema_version = get_schema_version(); | |
142 | ||
143 | if ($schema_version != SCHEMA_VERSION) { | |
144 | die("Schema version is wrong, please upgrade the database.\n"); | |
145 | } | |
146 | ||
147 | // Protip: children close shared database handle when terminating, it's a bad idea to | |
148 | // do database stuff on main process from now on. | |
149 | ||
150 | while (true) { | |
151 | ||
152 | // Since sleep is interupted by SIGCHLD, we need another way to | |
153 | // respect the spawn interval | |
154 | $next_spawn = $last_checkpoint + $spawn_interval - time(); | |
155 | ||
156 | if ($next_spawn % 60 == 0) { | |
157 | $running_jobs = count($children); | |
158 | _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec."); | |
159 | } | |
160 | ||
161 | if ($last_checkpoint + $spawn_interval < time()) { | |
162 | check_ctimes(); | |
163 | reap_children(); | |
164 | ||
165 | for ($j = count($children); $j < $max_jobs; $j++) { | |
166 | $pid = pcntl_fork(); | |
167 | if ($pid == -1) { | |
168 | die("fork failed!\n"); | |
169 | } else if ($pid) { | |
170 | _debug("[MASTER] spawned client $j [PID:$pid]..."); | |
171 | array_push($children, $pid); | |
172 | $ctimes[$pid] = time(); | |
173 | } else { | |
174 | pcntl_signal(SIGCHLD, SIG_IGN); | |
175 | ||
176 | $quiet = (isset($options["quiet"])) ? "--quiet" : ""; | |
177 | ||
178 | $my_pid = posix_getpid(); | |
179 | ||
180 | passthru(PHP_EXECUTABLE . " update.php --daemon-loop $quiet --task $j --pidlock $my_pid"); | |
181 | ||
182 | sleep(1); | |
183 | ||
184 | // We exit in order to avoid fork bombing. | |
185 | exit(0); | |
186 | } | |
187 | } | |
188 | $last_checkpoint = time(); | |
189 | } | |
190 | sleep(1); | |
191 | } | |
192 | ||
193 | ?> |