Skip to content

Handle raising exceptions from PublicTask #4078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions readthedocs/core/utils/tasks/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,28 @@ def set_public_data(self, data):
self.update_progress_data()

def run(self, *args, **kwargs):
error = False
exception_raised = None
self.set_permission_context(kwargs)
result = self.run_public(*args, **kwargs)
if result is not None:
self.set_public_data(result)
_, info = self.get_task_data()
return info
try:
result = self.run_public(*args, **kwargs)
except Exception as e:
# With Celery 4 we lost the ability to keep our data dictionary into
# ``AsyncResult.info`` when an exception was raised inside the
# Task. In this case, ``info`` will contain the exception raised
# instead of our data. So, I'm keeping the task as ``SUCCESS`` but
# the adding the exception message into an ``error`` key to be used
# from outside
exception_raised = e
error = True

def after_return(self, status, retval, task_id, args, kwargs, einfo):
"""Add the error to the task data"""
_, info = self.get_task_data()
if status == states.FAILURE:
info['error'] = retval
if STATUS_UPDATES_ENABLED:
self.update_state(state=status, meta=info)
if error and exception_raised:
info['error'] = str(exception_raised)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we are passing the string here instead of the actual Exception object? It seems like this limits what we can do with it later on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I had different problems when trying to serialize an Exception object. In fact, this serialization problem was one of the things that originated this issue.

elif result is not None:
self.set_public_data(result)

return info


def permission_check(check):
Expand Down
8 changes: 5 additions & 3 deletions readthedocs/restapi/views/task_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ def get_status_data(task_name, state, data, error=None):
'data': data,
'started': state in STARTED_STATES,
'finished': state in FINISHED_STATES,
'success': state in SUCCESS_STATES,
# When an exception is raised inside the task, we keep this as SUCCESS
# and add the exception messsage into the 'error' key
'success': state in SUCCESS_STATES and error is None,
}
if error is not None and isinstance(error, Exception):
data['error'] = error.message
if error is not None:
data['error'] = error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, we should probably we defensive here about what error is. Are we sure it will always be a string type, or should we be actively turning it into a string here, also?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be always a string since that data object here is the info dictionary that we are setting in the run method of the task where we cast the Exception object to a string.

return data


Expand Down
20 changes: 20 additions & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from readthedocs.integrations.models import Integration
from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
from readthedocs.projects.models import Feature, Project
from readthedocs.restapi.views.task_views import get_status_data

super_auth = base64.b64encode(b'super:test').decode('utf-8')
eric_auth = base64.b64encode(b'eric:test').decode('utf-8')
Expand Down Expand Up @@ -759,3 +760,22 @@ def test_get_version_by_id(self):
resp.data,
version_data,
)


class TaskViewsTests(TestCase):

def test_get_status_data(self):
data = get_status_data(
'public_task_exception',
'SUCCESS',
{'data': 'public'},
'Something bad happened',
)
self.assertEqual(data, {
'name': 'public_task_exception',
'data': {'data': 'public'},
'started': True,
'finished': True,
'success': False,
'error': 'Something bad happened',
})
30 changes: 30 additions & 0 deletions readthedocs/rtd_tests/tests/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,33 @@ def test_sync_repository(self):
args=(version.pk,),
)
self.assertTrue(result.successful())

def test_public_task_exception(self):
"""
Test when a PublicTask rises an Exception.

The exception should be catched and added to the ``info`` attribute of
the result. Besides, the task should be SUCCESS.
"""
from readthedocs.core.utils.tasks import PublicTask
from readthedocs.worker import app

class PublicTaskException(PublicTask):
name = 'public_task_exception'

def run_public(self):
raise Exception('Something bad happened')

app.tasks.register(PublicTaskException)
exception_task = PublicTaskException()
result = exception_task.delay()

# although the task risen an exception, it's success since we add the
# exception into the ``info`` attributes
self.assertEqual(result.status, 'SUCCESS')
self.assertEqual(result.info, {
'task_name': 'public_task_exception',
'context': {},
'public_data': {},
'error': 'Something bad happened',
})