]> git.wh0rd.org - tt-rss.git/blob - update.php
unify update_daemon, update_feeds and update_feedbrowser into update.php; move update...
[tt-rss.git] / update.php
1 #!/usr/bin/php
2 <?php
3 define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE);
4 define('DISABLE_SESSIONS', true);
5
6 if (!defined('PHP_EXECUTABLE'))
7 define('PHP_EXECUTABLE', '/usr/bin/php');
8
9 error_reporting(DEFAULT_ERROR_LEVEL);
10
11 require_once "sanity_check.php";
12 require_once "config.php";
13 require_once "db.php";
14 require_once "db-prefs.php";
15 require_once "functions.php";
16
17 $op = $argv[1];
18
19 if (!$op || $op == "-help") {
20 print "Tiny Tiny RSS data update script.\n\n";
21 print "Options:\n";
22 print " -feeds - update feeds\n";
23 print " -feedbrowser - update feedbrowser\n";
24 print " -daemon - start single-process update daemon\n";
25 print " -help - show this help\n";
26 return;
27 }
28
29 if ($op != "-daemon") {
30 $lock_filename = "update.lock";
31 } else {
32 $lock_filename = "update_daemon.lock";
33 }
34
35 $lock_handle = make_lockfile($lock_filename);
36 $must_exit = false;
37
38 // Try to lock a file in order to avoid concurrent update.
39 if (!$lock_handle) {
40 die("error: Can't create lockfile ($lock_filename). ".
41 "Maybe another update process is already running.\n");
42 }
43
44 // Create a database connection.
45 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
46
47 if (!$link) {
48 if (DB_TYPE == "mysql") {
49 print mysql_error();
50 }
51 // PG seems to display its own errors just fine by default.
52 return;
53 }
54
55 init_connection($link);
56
57 if ($op == "-feeds") {
58 // Update all feeds needing a update.
59 update_daemon_common($link);
60 }
61
62 if ($op == "-feedbrowser") {
63 $count = update_feedbrowser_cache($link);
64 print "Finished, $count feeds processed.\n";
65 }
66
67 if ($op == "-daemon") {
68 if (!ENABLE_UPDATE_DAEMON)
69 die("Please enable option ENABLE_UPDATE_DAEMON in config.php\n");
70
71 while (true) {
72 passthru(PHP_EXECUTABLE . " " . $argv[0] . " -daemon-loop");
73 _debug("Sleeping for " . DAEMON_SLEEP_INTERVAL . " seconds...");
74 sleep(DAEMON_SLEEP_INTERVAL);
75 }
76 }
77
78 if ($op == "-daemon-loop") {
79 if (!make_stampfile('update_daemon.stamp')) {
80 die("error: unable to create stampfile\n");
81 }
82
83 // Call to the feed batch update function
84 // or regenerate feedbrowser cache
85
86 if (rand(0,100) > 30) {
87 update_daemon_common($link);
88 } else {
89 $count = update_feedbrowser_cache($link);
90 _debug("Finished, $count feeds processed.");
91 }
92
93 }
94
95 db_close($link);
96
97 unlink(LOCK_DIRECTORY . "/$lock_filename");
98 ?>