mirror of
https://github.com/getredash/redash.git
synced 2025-12-19 17:37:19 -05:00
Fix issue with scheduled queries (#7111)
Co-authored-by: Ezra Odio <eodio@starfishstorage.com> Co-authored-by: Arik Fraimovich <arik@arikfr.com>
This commit is contained in:
@@ -387,6 +387,10 @@ class QueryResult(db.Model, BelongsToOrgMixin):
|
|||||||
|
|
||||||
|
|
||||||
def should_schedule_next(previous_iteration, now, interval, time=None, day_of_week=None, failures=0):
|
def should_schedule_next(previous_iteration, now, interval, time=None, day_of_week=None, failures=0):
|
||||||
|
# if previous_iteration is None, it means the query has never been run before
|
||||||
|
# so we should schedule it immediately
|
||||||
|
if previous_iteration is None:
|
||||||
|
return True
|
||||||
# if time exists then interval > 23 hours (82800s)
|
# if time exists then interval > 23 hours (82800s)
|
||||||
# if day_of_week exists then interval > 6 days (518400s)
|
# if day_of_week exists then interval > 6 days (518400s)
|
||||||
if time is None:
|
if time is None:
|
||||||
@@ -602,6 +606,11 @@ class Query(ChangeTrackingMixin, TimestampMixin, BelongsToOrgMixin, db.Model):
|
|||||||
if query.schedule.get("disabled"):
|
if query.schedule.get("disabled"):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Skip queries that have None for all schedule values. It's unclear whether this
|
||||||
|
# something that can happen in practice, but we have a test case for it.
|
||||||
|
if all(value is None for value in query.schedule.values()):
|
||||||
|
continue
|
||||||
|
|
||||||
if query.schedule["until"]:
|
if query.schedule["until"]:
|
||||||
schedule_until = pytz.utc.localize(datetime.datetime.strptime(query.schedule["until"], "%Y-%m-%d"))
|
schedule_until = pytz.utc.localize(datetime.datetime.strptime(query.schedule["until"], "%Y-%m-%d"))
|
||||||
|
|
||||||
@@ -613,7 +622,7 @@ class Query(ChangeTrackingMixin, TimestampMixin, BelongsToOrgMixin, db.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if should_schedule_next(
|
if should_schedule_next(
|
||||||
retrieved_at or now,
|
retrieved_at,
|
||||||
now,
|
now,
|
||||||
query.schedule["interval"],
|
query.schedule["interval"],
|
||||||
query.schedule["time"],
|
query.schedule["time"],
|
||||||
|
|||||||
@@ -216,6 +216,20 @@ class QueryOutdatedQueriesTest(BaseTestCase):
|
|||||||
|
|
||||||
self.assertEqual(list(models.Query.outdated_queries()), [query2])
|
self.assertEqual(list(models.Query.outdated_queries()), [query2])
|
||||||
|
|
||||||
|
def test_enqueues_scheduled_query_without_latest_query_data(self):
|
||||||
|
"""
|
||||||
|
Queries with a schedule but no latest_query_data will still be reported by Query.outdated_queries()
|
||||||
|
"""
|
||||||
|
query = self.factory.create_query(
|
||||||
|
schedule=self.schedule(interval="60"),
|
||||||
|
data_source=self.factory.create_data_source(),
|
||||||
|
)
|
||||||
|
|
||||||
|
outdated_queries = models.Query.outdated_queries()
|
||||||
|
self.assertEqual(query.latest_query_data, None)
|
||||||
|
self.assertEqual(len(outdated_queries), 1)
|
||||||
|
self.assertIn(query, outdated_queries)
|
||||||
|
|
||||||
def test_enqueues_query_with_correct_data_source(self):
|
def test_enqueues_query_with_correct_data_source(self):
|
||||||
"""
|
"""
|
||||||
Queries from different data sources will be reported by
|
Queries from different data sources will be reported by
|
||||||
|
|||||||
Reference in New Issue
Block a user