Skip to content

Commit 673ba76

Browse files
ezraodio1Ezra Odioarikfr
authored
Fix issue with scheduled queries (#7111)
Co-authored-by: Ezra Odio <[email protected]> Co-authored-by: Arik Fraimovich <[email protected]>
1 parent b922730 commit 673ba76

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

redash/models/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ def groups(self):
387387

388388

389389
def should_schedule_next(previous_iteration, now, interval, time=None, day_of_week=None, failures=0):
390+
# if previous_iteration is None, it means the query has never been run before
391+
# so we should schedule it immediately
392+
if previous_iteration is None:
393+
return True
390394
# if time exists then interval > 23 hours (82800s)
391395
# if day_of_week exists then interval > 6 days (518400s)
392396
if time is None:
@@ -602,6 +606,11 @@ def outdated_queries(cls):
602606
if query.schedule.get("disabled"):
603607
continue
604608

609+
# Skip queries that have None for all schedule values. It's unclear whether this
610+
# something that can happen in practice, but we have a test case for it.
611+
if all(value is None for value in query.schedule.values()):
612+
continue
613+
605614
if query.schedule["until"]:
606615
schedule_until = pytz.utc.localize(datetime.datetime.strptime(query.schedule["until"], "%Y-%m-%d"))
607616

@@ -613,7 +622,7 @@ def outdated_queries(cls):
613622
)
614623

615624
if should_schedule_next(
616-
retrieved_at or now,
625+
retrieved_at,
617626
now,
618627
query.schedule["interval"],
619628
query.schedule["time"],

tests/test_models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ def test_enqueues_query_only_once(self):
216216

217217
self.assertEqual(list(models.Query.outdated_queries()), [query2])
218218

219+
def test_enqueues_scheduled_query_without_latest_query_data(self):
220+
"""
221+
Queries with a schedule but no latest_query_data will still be reported by Query.outdated_queries()
222+
"""
223+
query = self.factory.create_query(
224+
schedule=self.schedule(interval="60"),
225+
data_source=self.factory.create_data_source(),
226+
)
227+
228+
outdated_queries = models.Query.outdated_queries()
229+
self.assertEqual(query.latest_query_data, None)
230+
self.assertEqual(len(outdated_queries), 1)
231+
self.assertIn(query, outdated_queries)
232+
219233
def test_enqueues_query_with_correct_data_source(self):
220234
"""
221235
Queries from different data sources will be reported by

0 commit comments

Comments
 (0)