Skip to content

Commit

Permalink
Propagate query execution errors from Celery tasks properly (re #290)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Short authored and jezdez committed Mar 5, 2018
1 parent cff65fe commit 80d9ab6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions redash/tasks/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ def run(self):
if self.scheduled_query:
self.scheduled_query.schedule_failures += 1
models.db.session.add(self.scheduled_query)
models.db.session.commit()
raise result
else:
if (self.scheduled_query and
self.scheduled_query.schedule_failures > 0):
Expand All @@ -478,8 +480,8 @@ def run(self):
self._log_progress('finished')

result = query_result.id
models.db.session.commit()
return result
models.db.session.commit()
return result

def _annotate_query(self, query_runner):
if query_runner.annotate_query():
Expand Down
22 changes: 14 additions & 8 deletions tests/tasks/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from tests import BaseTestCase
from redash import redis_connection, models
from redash.query_runner.pg import PostgreSQL
from redash.tasks.queries import QueryTaskTracker, enqueue_query, execute_query
from redash.tasks.queries import (QueryExecutionError, QueryTaskTracker,
enqueue_query, execute_query)


class TestPrune(TestCase):
Expand Down Expand Up @@ -113,10 +114,14 @@ def test_failure_scheduled(self):
{'routing_key': 'test'})
q = self.factory.create_query(query_text="SELECT 1, 2", schedule=300)
with cm, mock.patch.object(PostgreSQL, "run_query") as qr:
qr.exception = ValueError("broken")
execute_query("SELECT 1, 2", self.factory.data_source.id, {}, scheduled_query_id=q.id)
qr.side_effect = ValueError("broken")
with self.assertRaises(QueryExecutionError):
execute_query("SELECT 1, 2", self.factory.data_source.id, {},
scheduled_query_id=q.id)
self.assertEqual(q.schedule_failures, 1)
execute_query("SELECT 1, 2", self.factory.data_source.id, {}, scheduled_query_id=q.id)
with self.assertRaises(QueryExecutionError):
execute_query("SELECT 1, 2", self.factory.data_source.id, {},
scheduled_query_id=q.id)
q = models.Query.get_by_id(q.id)
self.assertEqual(q.schedule_failures, 2)

Expand All @@ -128,10 +133,11 @@ def test_success_after_failure(self):
{'routing_key': 'test'})
q = self.factory.create_query(query_text="SELECT 1, 2", schedule=300)
with cm, mock.patch.object(PostgreSQL, "run_query") as qr:
qr.exception = ValueError("broken")
execute_query("SELECT 1, 2",
self.factory.data_source.id, {},
scheduled_query_id=q.id)
qr.side_effect = ValueError("broken")
with self.assertRaises(QueryExecutionError):
execute_query("SELECT 1, 2",
self.factory.data_source.id, {},
scheduled_query_id=q.id)
q = models.Query.get_by_id(q.id)
self.assertEqual(q.schedule_failures, 1)

Expand Down

0 comments on commit 80d9ab6

Please sign in to comment.