Skip to content

Commit 1b58bed

Browse files
humitosagjohnson
authored andcommitted
Send proper context to celery email notification task (#3653)
* Log exception on webhook notification failed * Send only needed context to email_notification celery task
1 parent c5753a5 commit 1b58bed

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

readthedocs/projects/tasks.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -904,16 +904,29 @@ def email_notification(version, build, email):
904904
"""
905905
log.debug(LOG_TEMPLATE.format(project=version.project.slug, version=version.slug,
906906
msg='sending email to: %s' % email))
907-
context = {'version': version,
908-
'project': version.project,
909-
'build': build,
910-
'build_url': 'https://{0}{1}'.format(
911-
getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'),
912-
build.get_absolute_url()),
913-
'unsub_url': 'https://{0}{1}'.format(
914-
getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'),
915-
reverse('projects_notifications', args=[version.project.slug])),
916-
}
907+
908+
# We send only what we need from the Django model objects here to avoid
909+
# serialization problems in the ``readthedocs.core.tasks.send_email_task``
910+
context = {
911+
'version': {
912+
'verbose_name': version.verbose_name,
913+
},
914+
'project': {
915+
'name': version.project.name,
916+
},
917+
'build': {
918+
'pk': build.pk,
919+
'error': build.error,
920+
},
921+
'build_url': 'https://{0}{1}'.format(
922+
getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'),
923+
build.get_absolute_url(),
924+
),
925+
'unsub_url': 'https://{0}{1}'.format(
926+
getattr(settings, 'PRODUCTION_DOMAIN', 'readthedocs.org'),
927+
reverse('projects_notifications', args=[version.project.slug]),
928+
),
929+
}
917930

918931
if build.commit:
919932
title = _('Failed: {project.name} ({commit})').format(commit=build.commit[:8], **context)
@@ -925,7 +938,7 @@ def email_notification(version, build, email):
925938
title,
926939
template='projects/email/build_failed.txt',
927940
template_html='projects/email/build_failed.html',
928-
context=context
941+
context=context,
929942
)
930943

931944

@@ -951,7 +964,10 @@ def webhook_notification(version, build, hook_url):
951964
log.debug(LOG_TEMPLATE
952965
.format(project=project.slug, version='',
953966
msg='sending notification to: %s' % hook_url))
954-
requests.post(hook_url, data=data)
967+
try:
968+
requests.post(hook_url, data=data)
969+
except Exception:
970+
log.exception('Failed to POST on webhook url: url=%s', hook_url)
955971

956972

957973
@app.task(queue='web')

0 commit comments

Comments
 (0)