Skip to content

Commit

Permalink
Merge branch 'master' into better-tz-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sibson committed Jun 2, 2016
2 parents d92d021 + 5082607 commit 84475ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion redbeat/schedulers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def add_defaults(app=None):
app = app_or_default(app)

app.add_defaults({
'REDBEAT_REDIS_URL': app.conf['BROKER_URL'],
'REDBEAT_REDIS_URL': app.conf.get('REDBEAT_REDIS_URL', app.conf['BROKER_URL']),
'REDBEAT_KEY_PREFIX': app.conf.get('REDBEAT_KEY_PREFIX', 'redbeat:'),
'REDBEAT_SCHEDULE_KEY': app.conf.get('REDBEAT_KEY_PREFIX', 'redbeat:') + ':schedule',
'REDBEAT_STATICS_KEY': app.conf.get('REDBEAT_KEY_PREFIX', 'redbeat:') + ':statics',
Expand Down Expand Up @@ -89,10 +89,16 @@ def from_key(key, app=None):

@property
def due_at(self):
# never run => due now
if self.last_run_at == datetime.min:
return self._default_now()

delta = self.schedule.remaining_estimate(self.last_run_at)

# overdue => due now
if delta.total_seconds() < 0:
return self._default_now()

return self.last_run_at + delta

@property
Expand All @@ -103,6 +109,10 @@ def key(self):
def score(self):
return to_timestamp(self.due_at)

@property
def rank(self):
return redis(self.app).zrank(self.app.conf.REDBEAT_SCHEDULE_KEY, self.key)

def save(self):
definition = {
'name': self.name,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ def test_due_at(self):
self.assertLess(now, due_at)
self.assertLess(due_at, now + entry.schedule.run_every)

def test_due_at_overdue(self):
last_run_at = self.app.now() - timedelta(hours=10)
entry = self.create_entry(last_run_at=last_run_at)

before = entry._default_now()
due_at = entry.due_at

self.assertLess(last_run_at, due_at)
self.assertGreater(due_at, before)

def test_score(self):
run_every = 61*60
entry = self.create_entry(run_every=run_every)
Expand Down

0 comments on commit 84475ba

Please sign in to comment.