Move request_id injection into send_task override

This applies the same change we made in other apps [1][2]. Adding
the override here is special, though, because it means the others
will now get triggered, since this app is the start of the chain
of tasks for a request. We will also retain existing request_id
tracing for tasks within this app, since "apply_async" calls the
"send_task" method internally, which is the one we're overriding.

[1]: 6f3c118a1e
[2]: 2e08b7aa95
This commit is contained in:
Ben Thorner
2021-04-27 10:35:21 +01:00
parent 5f26d16915
commit 99bc29418e
2 changed files with 26 additions and 26 deletions

View File

@@ -73,16 +73,6 @@ def make_task(app):
return super().__call__(*args, **kwargs)
def apply_async(self, args=None, kwargs=None, **other_kwargs):
kwargs = kwargs or {}
if has_request_context() and hasattr(request, 'request_id'):
kwargs['request_id'] = request.request_id
elif has_app_context() and 'request_id' in g:
kwargs['request_id'] = g.request_id
return super().apply_async(args, kwargs, **other_kwargs)
return NotifyTask
@@ -97,3 +87,13 @@ class NotifyCelery(Celery):
self.conf.update(app.config)
self._app = app
def send_task(self, name, args=None, kwargs=None, **other_kwargs):
kwargs = kwargs or {}
if has_request_context() and hasattr(request, 'request_id'):
kwargs['request_id'] = request.request_id
elif has_app_context() and 'request_id' in g:
kwargs['request_id'] = g.request_id
return super().send_task(name, args, kwargs, **other_kwargs)