]>
Commit | Line | Data |
---|---|---|
de696427 AD |
1 | #!/usr/bin/php4 |
2 | <? | |
3 | // this daemon runs in the background and updates all feeds | |
4 | // continuously | |
5 | ||
6eafcac6 | 6 | declare(ticks = 1); |
de696427 AD |
7 | |
8 | define('MAGPIE_CACHE_DIR', '/var/tmp/magpie-ttrss-cache-daemon'); | |
de696427 AD |
9 | define('DISABLE_SESSIONS', true); |
10 | ||
11 | require_once "sanity_check.php"; | |
12 | require_once "config.php"; | |
c6784aea AD |
13 | |
14 | if (!ENABLE_UPDATE_DAEMON) { | |
15 | die("Please enable option ENABLE_UPDATE_DAEMON in config.php"); | |
16 | } | |
17 | ||
de696427 AD |
18 | require_once "db.php"; |
19 | require_once "db-prefs.php"; | |
20 | require_once "functions.php"; | |
21 | require_once "magpierss/rss_fetch.inc"; | |
22 | ||
6eafcac6 AD |
23 | function sigint_handler() { |
24 | unlink("update_daemon.lock"); | |
25 | die("Received SIGINT. Exiting.\n"); | |
26 | } | |
27 | ||
28 | pcntl_signal(SIGINT, sigint_handler); | |
29 | ||
30 | $lock_handle = make_lockfile("update_daemon.lock"); | |
31 | ||
32 | if (!$lock_handle) { | |
33 | die("error: Can't create lockfile ($lock_filename). ". | |
34 | "Maybe another daemon is already running."); | |
35 | } | |
36 | ||
de696427 AD |
37 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
38 | ||
39 | if (!$link) { | |
40 | if (DB_TYPE == "mysql") { | |
41 | print mysql_error(); | |
42 | } | |
43 | // PG seems to display its own errors just fine by default. | |
44 | return; | |
45 | } | |
46 | ||
47 | if (DB_TYPE == "pgsql") { | |
48 | pg_query("set client_encoding = 'utf-8'"); | |
49 | } | |
50 | ||
1f4ad53c AD |
51 | while (true) { |
52 | ||
085a5a74 | 53 | // FIXME: get all scheduled updates w/forced refetch |
6eafcac6 | 54 | // Stub, until I figure out if it is really needed. |
1f4ad53c | 55 | |
085a5a74 AD |
56 | # $result = db_query($link, "SELECT * FROM ttrss_scheduled_updates ORDER BY id"); |
57 | # while ($line = db_fetch_assoc($result)) { | |
58 | # print "Scheduled feed update: " . $line["feed_id"] . ", UID: " . | |
59 | # $line["owner_uid"] . "\n"; | |
60 | # } | |
1f4ad53c AD |
61 | |
62 | // Process all other feeds using last_updated and interval parameters | |
63 | ||
64 | $result = db_query($link, "SELECT feed_url,id,owner_uid, | |
65 | SUBSTRING(last_updated,1,19) AS last_updated, | |
66 | update_interval FROM ttrss_feeds ORDER BY last_updated DESC"); | |
67 | ||
68 | while ($line = db_fetch_assoc($result)) { | |
69 | ||
1f4ad53c | 70 | $upd_intl = $line["update_interval"]; |
1f4ad53c AD |
71 | $user_id = $line["owner_uid"]; |
72 | ||
73 | if (!$upd_intl || $upd_intl == 0) { | |
74 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); | |
75 | } | |
c1e202b7 AD |
76 | |
77 | if ($upd_intl < 0) { | |
1011b66a | 78 | # print "Updates disabled.\n"; |
c1e202b7 AD |
79 | continue; |
80 | } | |
1f4ad53c | 81 | |
1011b66a AD |
82 | print "Feed: " . $line["feed_url"] . ": "; |
83 | ||
78ea1de0 AD |
84 | printf("(%d/%d, %d) ", time() - strtotime($line["last_updated"]), |
85 | $upd_intl*60, $owner_uid); | |
1f4ad53c AD |
86 | |
87 | if (!$line["last_updated"] || | |
88 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60)) { | |
89 | ||
6eafcac6 AD |
90 | print "Updating...\n"; |
91 | update_rss_feed($link, $line["feed_url"], $line["id"], true); | |
13def219 | 92 | sleep(3); // prevent flood (FIXME make this an option?) |
6eafcac6 AD |
93 | } else { |
94 | print "Update not needed.\n"; | |
1f4ad53c | 95 | } |
de696427 AD |
96 | } |
97 | ||
78ea1de0 | 98 | print "Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...\n"; |
1f4ad53c | 99 | |
78ea1de0 | 100 | sleep(DAEMON_SLEEP_INTERVAL); |
de696427 AD |
101 | } |
102 | ||
de696427 AD |
103 | db_close($link); |
104 | ||
105 | ?> |