Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Remove duplication in the staleness check
Browse files Browse the repository at this point in the history
  • Loading branch information
jabley committed Aug 13, 2014
1 parent d4530ef commit d3141b1
Showing 1 changed file with 19 additions and 26 deletions.
45 changes: 19 additions & 26 deletions backdrop/core/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,9 @@ def name(self):
return self.config['name']

def is_recent_enough(self):
max_age_expected = self.get_max_age_expected()

now = timeutils.now()
last_updated = self.get_last_updated()

"""
- If `max_age_expected` is None we're saying
'hey, we're not going to check this'.
- If `last_updated` is a null value,
the data-set has never been updated,
so it doesn't really give us any value to say
'you should update this thing you've never updated before'
because we already know that, right?
"""
if max_age_expected is None or not last_updated:
return True

max_age_expected_delta = datetime.timedelta(seconds=max_age_expected)

return (now - last_updated) < max_age_expected_delta
seconds_out_of_date = self.get_seconds_out_of_date()

return seconds_out_of_date is None or seconds_out_of_date < 0

def get_max_age_expected(self):
return self.config.get('max_age_expected',
Expand All @@ -55,20 +38,30 @@ def get_last_updated(self):

def get_seconds_out_of_date(self):
now = timeutils.now()
max_age_expected = self.get_max_age_expected()
last_updated = self.get_last_updated()

if not self.get_last_updated() or self.get_max_age_expected() is None:
if not self._is_staleness_appropriate(last_updated, max_age_expected):
return None
else:
last_updated = self.get_last_updated()

max_age_delta = datetime.timedelta(
seconds=self.get_max_age_expected()
)
max_age_delta = datetime.timedelta(seconds=max_age_expected)

return int((
now - last_updated - max_age_delta
).total_seconds())

def _is_staleness_appropriate(self, last_updated, max_age_expected):
"""
- If `max_age_expected` is None we're saying
'hey, we're not going to check this'.
- If `last_updated` is a null value,
the data-set has never been updated,
so it doesn't really give us any value to say
'you should update this thing you've never updated before'
because we already know that, right?
"""
return last_updated and max_age_expected

def create_if_not_exists(self):
if not self.storage.data_set_exists(self.name):
self.storage.create_data_set(self.name, self.config['capped_size'])
Expand Down

0 comments on commit d3141b1

Please sign in to comment.