Skip to content

Commit

Permalink
Merge pull request #75 from someboredkiddo/fix_activity_poll_exception
Browse files Browse the repository at this point in the history
Add error catching for Activity polling
  • Loading branch information
xethorn committed Aug 4, 2017
2 parents 5c40160 + dfbed38 commit 7402649
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 16 additions & 1 deletion garcon/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,22 @@ def run(self):
previous activity is consumed (context).
"""

activity_task = self.poll()
try:
activity_task = self.poll()
except Exception as error:
# Catch exceptions raised during poll() to avoid an Activity thread
# dying & worker daemon unable to process the affected Activity.
# AWS api limits on SWF calls are a common source of such
# exceptions (see https://github.com/xethorn/garcon/pull/75)

# on_exception() can be overriden by the flow to send an alert
# when an exception occurs.
if self.on_exception:
self.on_exception(self, error)

self.logger.error(error, exc_info=True)
return True

packed_context = activity_task.get('input')
context = dict()

Expand Down
24 changes: 23 additions & 1 deletion tests/test_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_run_activity(monkeypatch, poll):


def test_run_capture_exception(monkeypatch, poll):
"""Run an activity with an exception raised.
"""Run an activity with an exception raised during activity execution.
"""

current_activity = activity_run(monkeypatch, poll=poll)
Expand All @@ -93,6 +93,28 @@ def test_run_capture_exception(monkeypatch, poll):
assert not current_activity.complete.called


def test_run_capture_poll_exception(monkeypatch, poll):
"""Run an activity with an exception raised during poll.
"""

current_activity = activity_run(monkeypatch, poll=poll)
current_activity.on_exception = MagicMock()
current_activity.execute_activity = MagicMock()
exception = Exception('poll exception')
current_activity.poll.side_effect = exception
current_activity.run()

assert current_activity.poll.called
assert current_activity.on_exception.called
assert not current_activity.execute_activity.called
assert not current_activity.complete.called

current_activity.on_exception = None
current_activity.logger.error = MagicMock()
current_activity.run()
current_activity.logger.error.assert_called_with(exception, exc_info=True)


def test_run_activity_without_id(monkeypatch):
"""Run an activity without an activity id.
"""
Expand Down

0 comments on commit 7402649

Please sign in to comment.