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