From 0c7a83f93890015a9b3b5e77082ecf2f0f1d8bb3 Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Sat, 31 Mar 2012 15:17:44 +0100 Subject: [PATCH] Don't keep trying to check failed feeds that we never had Normally, if fetching a feed failed within the last hour then we don't try again. But if we never managed to fetch the feed then we always retried. --- tests/testifacecache.py | 7 +++++-- zeroinstall/cmd/add_feed.py | 9 +++------ zeroinstall/cmd/select.py | 3 +-- zeroinstall/injector/iface_cache.py | 31 +++++++++++++++++++------------ 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/tests/testifacecache.py b/tests/testifacecache.py index a0d3eca..9b2b62e 100755 --- a/tests/testifacecache.py +++ b/tests/testifacecache.py @@ -158,9 +158,12 @@ class TestIfaceCache(BaseTest): def testIsStale(self): iface_cache = self.config.iface_cache feed = self.import_feed('http://localhost:8000/Hello', 'Hello') - assert iface_cache.is_stale(feed, 1) == True - assert iface_cache.is_stale(feed, time.time() + 1) == False + assert iface_cache.is_stale(feed.url, 1) == True + assert iface_cache.is_stale(feed.url, time.time() + 1) == False iface_cache.mark_as_checking(feed.url) + assert iface_cache.is_stale(feed.url, 1) == False + + # Old API assert iface_cache.is_stale(feed, 1) == False if __name__ == '__main__': diff --git a/zeroinstall/cmd/add_feed.py b/zeroinstall/cmd/add_feed.py index fd26ce3..fb9f44d 100644 --- a/zeroinstall/cmd/add_feed.py +++ b/zeroinstall/cmd/add_feed.py @@ -11,7 +11,6 @@ from zeroinstall import SafeException, _ from zeroinstall.support import tasks from zeroinstall.cmd import UsageError from zeroinstall.injector import model, writer -from zeroinstall.injector.policy import Policy syntax = "NEW-FEED" @@ -31,18 +30,16 @@ def handle(config, options, args, add_ok = True, remove_ok = False): print(_("Feed '%s':") % x + '\n') x = model.canonical_iface_uri(x) - policy = Policy(x, config = config) if options.offline: config.network_use = model.network_offline - feed = config.iface_cache.get_feed(x) - if policy.network_use != model.network_offline and policy.is_stale(feed): - blocker = policy.fetcher.download_and_import_feed(x, config.iface_cache) + if config.network_use != model.network_offline and config.iface_cache.is_stale(x, config.freshness): + blocker = config.fetcher.download_and_import_feed(x, config.iface_cache) print(_("Downloading feed; please wait...")) tasks.wait_for_blocker(blocker) print(_("Done")) - candidate_interfaces = policy.get_feed_targets(x) + candidate_interfaces = config.iface_cache.get_feed_targets(x) assert candidate_interfaces interfaces = [] for i in range(len(candidate_interfaces)): diff --git a/zeroinstall/cmd/select.py b/zeroinstall/cmd/select.py index 291b53d..e854d28 100644 --- a/zeroinstall/cmd/select.py +++ b/zeroinstall/cmd/select.py @@ -79,9 +79,8 @@ def get_selections(config, options, iface_uri, select_only, download_only, test_ can_run_immediately = not driver.need_download() stale_feeds = [feed for feed in driver.solver.feeds_used if - not os.path.isabs(feed) and # Ignore local feeds (note: file might be missing too) not feed.startswith('distribution:') and # Ignore (memory-only) PackageKit feeds - iface_cache.is_stale(iface_cache.get_feed(feed), config.freshness)] + iface_cache.is_stale(feed, config.freshness)] if download_only and stale_feeds: can_run_immediately = False diff --git a/zeroinstall/injector/iface_cache.py b/zeroinstall/injector/iface_cache.py index d9c0911..abf8c91 100644 --- a/zeroinstall/injector/iface_cache.py +++ b/zeroinstall/injector/iface_cache.py @@ -522,26 +522,33 @@ class IfaceCache(object): debug(_("Feed targets: %s"), feed_targets) return [self.get_interface(uri) for uri in feed_targets] - def is_stale(self, feed, freshness_threshold): + def is_stale(self, feed_url, freshness_threshold): """Check whether feed needs updating, based on the configured L{config.Config.freshness}. None is considered to be stale. If we already tried to update the feed within FAILED_CHECK_DELAY, returns false. @return: True if feed should be updated @since: 0.53""" - if feed is None: - return True - if os.path.isabs(feed.url): - return False # Local feeds are never stale - if feed.last_modified is None: - return True # Don't even have it yet + if isinstance(feed_url, model.ZeroInstallFeed): + feed_url = feed_url.url # old API + elif feed_url is None: + return True # old API + now = time.time() - staleness = now - (feed.last_checked or 0) - debug(_("Staleness for %(feed)s is %(staleness).2f hours"), {'feed': feed, 'staleness': staleness / 3600.0}) - if freshness_threshold <= 0 or staleness < freshness_threshold: - return False # Fresh enough for us + feed = self.get_feed(feed_url) + if feed is not None: + if feed.local_path is not None: + return False # Local feeds are never stale + + if feed.last_modified is not None: + staleness = now - (feed.last_checked or 0) + debug(_("Staleness for %(feed)s is %(staleness).2f hours"), {'feed': feed, 'staleness': staleness / 3600.0}) + + if freshness_threshold <= 0 or staleness < freshness_threshold: + return False # Fresh enough for us + # else we've never had it - last_check_attempt = self.get_last_check_attempt(feed.url) + last_check_attempt = self.get_last_check_attempt(feed_url) if last_check_attempt and last_check_attempt > now - FAILED_CHECK_DELAY: debug(_("Stale, but tried to check recently (%s) so not rechecking now."), time.ctime(last_check_attempt)) return False -- 2.11.4.GIT