mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 00:41:35 -05:00
Fix apply_async not working with positional kwargs
Celery's apply_async function accepts 'kwargs' as (get ready to be
confused) either a positional argument, or a keyword argument:
Positional: apply_async(['args'], {'kw': 'args'})
Keyword: apply_async(args=['args'], kwargs={'kw': 'args'})
We rely on the positional form in at least one place [1]. This fixes
the overload of apply_async to cope with both forms, and continue to
pass through any other (confusion time again) keyword args to super(),
such as queue="queue".
Note that we've also decided to stop accepting other positional args,
since this is unnecessarily confusing, and we don't currently rely on
it in our code. This stops it creeping in in future.
[1]: fde927e00e/app/job/rest.py (L186)
This commit is contained in:
@@ -73,15 +73,15 @@ def make_task(app):
|
||||
|
||||
return super().__call__(*args, **kwargs)
|
||||
|
||||
def apply_async(self, *args, **kwargs):
|
||||
kwargs['kwargs'] = kwargs.get('kwargs', {})
|
||||
def apply_async(self, args=None, kwargs=None, **other_kwargs):
|
||||
kwargs = kwargs or {}
|
||||
|
||||
if has_request_context() and hasattr(request, 'request_id'):
|
||||
kwargs['kwargs']['request_id'] = request.request_id
|
||||
kwargs['request_id'] = request.request_id
|
||||
elif has_app_context() and 'request_id' in g:
|
||||
kwargs['kwargs']['request_id'] = g.request_id
|
||||
kwargs['request_id'] = g.request_id
|
||||
|
||||
return super().apply_async(*args, **kwargs)
|
||||
return super().apply_async(args, kwargs, **other_kwargs)
|
||||
|
||||
return NotifyTask
|
||||
|
||||
|
||||
Reference in New Issue
Block a user