3 // This is an experimental multiprocess update daemon.
4 // Some configurable variable may be found below.
6 // define('DEFAULT_ERROR_LEVEL', E_ALL);
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE
);
11 define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon');
12 define('SIMPLEPIE_CACHE_DIR', '/var/tmp/simplepie-ttrss-cache-daemon');
13 define('DISABLE_SESSIONS', true);
15 require_once "version.php";
17 if (strpos(VERSION
, ".99") !== false ||
getenv('DAEMON_XDEBUG')) {
18 define('DAEMON_EXTENDED_DEBUG', true);
21 define('PURGE_INTERVAL', 3600); // seconds
23 require_once "sanity_check.php";
24 require_once "config.php";
26 define('MAX_JOBS', 2);
28 define('SPAWN_INTERVAL', DAEMON_SLEEP_INTERVAL
);
30 if (!ENABLE_UPDATE_DAEMON
) {
31 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
34 require_once "db.php";
35 require_once "db-prefs.php";
36 require_once "functions.php";
37 require_once "magpierss/rss_fetch.inc";
39 error_reporting(DEFAULT_ERROR_LEVEL
);
43 $last_checkpoint = -1;
45 function reap_children() {
50 foreach ($children as $pid) {
51 if (pcntl_waitpid($pid, $status, WNOHANG
) != $pid) {
52 array_push($tmp, $pid);
54 _debug("[SIGCHLD] child $pid reaped.");
63 function sigalrm_handler() {
64 die("[SIGALRM] hang in feed update?\n");
67 function sigchld_handler($signal) {
68 $running_jobs = reap_children();
70 _debug("[SIGCHLD] jobs left: $running_jobs");
72 pcntl_waitpid(-1, $status, WNOHANG
);
75 function sigint_handler() {
76 unlink(LOCK_DIRECTORY
. "/update_daemon.lock");
77 die("[SIGINT] removing lockfile and exiting.\n");
80 pcntl_signal(SIGALRM
, 'sigalrm_handler');
81 pcntl_signal(SIGCHLD
, 'sigchld_handler');
83 if (file_is_locked("update_daemon.lock")) {
84 die("error: Can't create lockfile. ".
85 "Maybe another daemon is already running.\n");
89 pcntl_signal(SIGINT
, 'sigint_handler');
91 // Try to lock a file in order to avoid concurrent update.
92 $lock_handle = make_lockfile("update_daemon.lock");
95 die("error: Can't create lockfile. ".
96 "Maybe another daemon is already running.\n");
99 while (true) { sleep(100); }
102 // Testing database connection.
103 // It is unnecessary to start the fork loop if database is not ok.
104 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
107 if (DB_TYPE
== "mysql") {
110 // PG seems to display its own errors just fine by default.
119 // Since sleep is interupted by SIGCHLD, we need another way to
120 // respect the SPAWN_INTERVAL
121 $next_spawn = $last_checkpoint + SPAWN_INTERVAL
- time();
123 if ($next_spawn %
10 == 0) {
124 $running_jobs = count($children);
125 _debug("[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec.");
128 if ($last_checkpoint + SPAWN_INTERVAL
< time()) {
132 for ($j = count($children); $j < MAX_JOBS
; $j++
) {
135 die("fork failed!\n");
137 _debug("[MASTER] spawned client $j [PID:$pid]...");
138 array_push($children, $pid);
140 pcntl_signal(SIGCHLD
, SIG_IGN
);
141 pcntl_signal(SIGINT
, SIG_DFL
);
143 // ****** Updating RSS code *******
144 // Only run in fork process.
146 $start_timestamp = time();
148 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
151 if (DB_TYPE
== "mysql") {
154 // PG seems to display its own errors just fine by default.
158 if (DB_TYPE
== "pgsql") {
159 pg_query("set client_encoding = 'utf-8'");
160 pg_set_client_encoding("UNICODE");
162 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET
) {
163 db_query($link, "SET NAMES " . MYSQL_CHARSET
);
164 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
168 // We disable stamp file, since it is of no use in a multiprocess update.
169 // not really, tho for the time being -fox
170 if (!make_stampfile('update_daemon.stamp')) {
171 print "warning: unable to create stampfile";
176 // if (time() - $last_purge > PURGE_INTERVAL) {
178 // FIXME : $last_purge is of no use in a multiprocess update.
179 // FIXME : We ALWAYS purge old posts.
180 _debug("Purging old posts (random 30 feeds)...");
181 global_purge_old_posts($link, true, 30);
183 // $last_purge = time();
186 // Call to the feed batch update function
187 update_daemon_common($link);
189 _debug("Elapsed time: " . (time() - $start_timestamp) . " second(s)");
194 // We wait a little before exiting to avoid to be faster than our parent process.
196 // We exit in order to avoid fork bombing.
200 // We wait a little time before the next fork, in order to let the first fork
201 // mark the feeds it update :
204 $last_checkpoint = time();