diff --git a/redbeat/schedulers.py b/redbeat/schedulers.py index a6f9da1..72c9f4f 100644 --- a/redbeat/schedulers.py +++ b/redbeat/schedulers.py @@ -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', @@ -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 @@ -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, diff --git a/tests/test_entry.py b/tests/test_entry.py index 94967f1..289760b 100644 --- a/tests/test_entry.py +++ b/tests/test_entry.py @@ -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)