]> git.wh0rd.org - tt-rss.git/blob - update_daemon.php
code cleanup, test for db_escape() crazyness in DB sanity check
[tt-rss.git] / update_daemon.php
1 #!/usr/bin/php
2 <?php
3 // this daemon runs in the background and updates all feeds
4 // continuously
5
6 // define('DEFAULT_ERROR_LEVEL', E_ALL);
7 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
8
9 declare(ticks = 1);
10
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);
14
15 require_once "version.php";
16
17 if (strpos(VERSION, ".99") !== false || getenv('DAEMON_XDEBUG')) {
18 define('DAEMON_EXTENDED_DEBUG', true);
19 }
20
21 define('PURGE_INTERVAL', 3600); // seconds
22
23 require_once "sanity_check.php";
24 require_once "config.php";
25
26 if (!defined('PHP_EXECUTABLE')) {
27 define('PHP_EXECUTABLE', '/usr/bin/php');
28 }
29
30 if (!ENABLE_UPDATE_DAEMON) {
31 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
32 }
33
34 require_once "db.php";
35 require_once "db-prefs.php";
36 require_once "functions.php";
37 require_once "magpierss/rss_fetch.inc";
38
39 error_reporting(DEFAULT_ERROR_LEVEL);
40
41 function sigint_handler() {
42 unlink(LOCK_DIRECTORY . "/update_daemon.lock");
43 die("Received SIGINT. Exiting.\n");
44 }
45
46 function sigalrm_handler() {
47 die("received SIGALRM, hang in feed update?\n");
48 }
49
50 pcntl_signal(SIGINT, sigint_handler);
51 pcntl_signal(SIGALRM, sigalrm_handler);
52
53 $lock_handle = make_lockfile("update_daemon.lock");
54
55 if (!$lock_handle) {
56 die("error: Can't create lockfile ($lock_filename). ".
57 "Maybe another daemon is already running.\n");
58 }
59
60 // Testing database connection.
61 // It is unnecessary to start the fork loop if database is not ok.
62 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
63
64 if (!$link) {
65 if (DB_TYPE == "mysql") {
66 print mysql_error();
67 }
68 // PG seems to display its own errors just fine by default.
69 return;
70 }
71
72 db_close($link);
73
74 $last_purge = 0;
75
76 while (true) {
77
78 passthru(PHP_EXECUTABLE . " update_daemon_loop.php SRV_RUN_OK");
79
80 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
81
82 sleep(DAEMON_SLEEP_INTERVAL);
83 }
84
85
86 ?>