]> git.wh0rd.org - tt-rss.git/blobdiff - include/rssfuncs.php
unify houskeeping stuff, increase spawn interval
[tt-rss.git] / include / rssfuncs.php
index 2be791cb7cb345d5a46fc4e7c51098f3cf4c3034..a64d60e1eebbbc84ba29e89dcda4b7713cb035c1 100644 (file)
@@ -1,7 +1,7 @@
 <?php
-       define('DAEMON_UPDATE_LOGIN_LIMIT', 30);
-       define('DAEMON_FEED_LIMIT', 100);
-       define('DAEMON_SLEEP_INTERVAL', 60);
+       define_default('DAEMON_UPDATE_LOGIN_LIMIT', 30);
+       define_default('DAEMON_FEED_LIMIT', 500);
+       define_default('DAEMON_SLEEP_INTERVAL', 120);
 
        function update_feedbrowser_cache() {
 
 
                // Test if feed is currently being updated by another process.
                if (DB_TYPE == "pgsql") {
-                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '5 minutes')";
+                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < NOW() - INTERVAL '10 minutes')";
                } else {
-                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 5 MINUTE))";
+                       $updstart_thresh_qpart = "AND (ttrss_feeds.last_update_started IS NULL OR ttrss_feeds.last_update_started < DATE_SUB(NOW(), INTERVAL 10 MINUTE))";
                }
 
                // Test if there is a limit to number of updated feeds
                $query_limit = "";
                if($limit) $query_limit = sprintf("LIMIT %d", $limit);
 
-               $random_qpart = sql_random_function();
-
-               // We search for feed needing update.
-               $result = db_query("SELECT DISTINCT ttrss_feeds.feed_url,$random_qpart
+               $query = "SELECT DISTINCT ttrss_feeds.feed_url, ttrss_feeds.last_updated
                        FROM
                                ttrss_feeds, ttrss_users, ttrss_user_prefs
                        WHERE
                                AND ttrss_user_prefs.pref_name = 'DEFAULT_UPDATE_INTERVAL'
                                $login_thresh_qpart $update_limit_qpart
                                $updstart_thresh_qpart
-                       ORDER BY $random_qpart $query_limit");
+                               ORDER BY last_updated $query_limit";
+
+               // We search for feed needing update.
+               $result = db_query($query);
 
                if($debug) _debug(sprintf("Scheduled %d feeds to update...", db_num_rows($result)));
 
                                WHERE feed_url IN (%s)", implode(',', $feeds_quoted)));
                }
 
-               expire_cached_files($debug);
-               expire_lock_files($debug);
-               expire_error_log($debug);
-
                $nf = 0;
 
                // For each feed, we call the feed update function.
                                        _debug("local cache valid and older than last_updated, nothing to do.", $debug_enabled);
                                        return;
                                }
+               } else {
+                       _debug("local cache will not be used for this feed", $debug_enabled);
+                       $cache_timestamp = 0;
                }
 
                if (!$rss) {
                                $favicon_interval_qpart = "favicon_last_checked < DATE_SUB(NOW(), INTERVAL 12 HOUR)";
                        }
 
-                       $result = db_query("SELECT title,site_url,owner_uid,
+                       $result = db_query("SELECT title,site_url,owner_uid,favicon_avg_color,
                                (favicon_last_checked IS NULL OR $favicon_interval_qpart) AS
                                                favicon_needs_check
                                FROM ttrss_feeds WHERE id = '$feed'");
                        $orig_site_url = db_fetch_result($result, 0, "site_url");
                        $favicon_needs_check = sql_bool_to_bool(db_fetch_result($result, 0,
                                "favicon_needs_check"));
+                       $favicon_avg_color = db_fetch_result($result, 0, "favicon_avg_color");
 
                        $owner_uid = db_fetch_result($result, 0, "owner_uid");
 
                        $site_url = db_escape_string(mb_substr(rewrite_relative_url($fetch_url, $rss->get_link()), 0, 245));
 
                        if ($favicon_needs_check || $force_refetch) {
+
+                               /* terrible hack: if we crash on floicon shit here, we won't check
+                                * the icon avgcolor again (unless the icon got updated) */
+
+                               db_query("UPDATE ttrss_feeds SET favicon_avg_color = 'fail' WHERE
+                                       id = '$feed'");
+
+                               $favicon_file = ICONS_DIR . "/$feed.ico";
+                               $favicon_modified = @filemtime($favicon_file);
+
                                _debug("checking favicon...", $debug_enabled);
 
                                check_feed_favicon($site_url, $feed, $link);
-                               $favicon_file = ICONS_DIR . "/$feed.ico";
+                               $favicon_modified_new = @filemtime($favicon_file);
 
-                               if (file_exists($favicon_file) && function_exists("imagecreatefromstring")) {
+                               if ($favicon_modified_new > $favicon_modified)
+                                       $favicon_avg_color = '';
+
+                               if (file_exists($favicon_file) && function_exists("imagecreatefromstring") && $favicon_avg_color == '') {
                                                require_once "colors.php";
 
                                                $favicon_color = db_escape_string(
                                                        calculate_avg_color($favicon_file));
 
                                                $favicon_colorstring = ",favicon_avg_color = '".$favicon_color."'";
+                               } else if ($favicon_avg_color == 'fail') {
+                                       _debug("floicon failed on this file, not trying to recalculate avg color");
                                }
 
                                db_query("UPDATE ttrss_feeds SET favicon_last_checked = NOW()
        }
 
        function expire_lock_files($debug) {
-               if ($debug) _debug("Removing old lock files...");
+               //if ($debug) _debug("Removing old lock files...");
 
                $num_deleted = 0;
 
                        }
                }
 
-               if ($debug) _debug("Removed $num_deleted files.");
+               if ($debug) _debug("Removed $num_deleted old lock files.");
        }
 
        function expire_cached_files($debug) {
                foreach (array("simplepie", "images", "export", "upload") as $dir) {
                        $cache_dir = CACHE_DIR . "/$dir";
 
-                       if ($debug) _debug("Expiring $cache_dir");
+//                     if ($debug) _debug("Expiring $cache_dir");
 
                        $num_deleted = 0;
 
                                }
                        }
 
-                       if ($debug) _debug("Removed $num_deleted files.");
+                       if ($debug) _debug("$cache_dir: removed $num_deleted files.");
                }
        }
 
                return $error;
        }
 
+       function housekeeping_common($debug) {
+               expire_cached_files($debug);
+               expire_lock_files($debug);
+               expire_error_log($debug);
+
+               $count = update_feedbrowser_cache();
+               _debug("Feedbrowser updated, $count feeds processed.");
+
+               purge_orphans( true);
+               $rc = cleanup_tags( 14, 50000);
+
+               _debug("Cleaned $rc cached tags.");
+       }
 ?>