From 246f2093bd67ccae0586312ab1948e9397b1b32c Mon Sep 17 00:00:00 2001 From: Marcin Kuzminski Date: Wed, 18 Jan 2017 23:13:27 +0100 Subject: [PATCH] celery: handle pyramid/pylons context better when running async tasks. --- rhodecode/api/__init__.py | 2 +- rhodecode/lib/celerylib/__init__.py | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/rhodecode/api/__init__.py b/rhodecode/api/__init__.py index 6587fca1..56fc5420 100644 --- a/rhodecode/api/__init__.py +++ b/rhodecode/api/__init__.py @@ -256,7 +256,7 @@ def request_view(request): except JSONRPCBaseError: raise except Exception: - log.exception('Unhandled exception occured on api call: %s', func) + log.exception('Unhandled exception occurred on api call: %s', func) return jsonrpc_error(request, retid=request.rpc_id, message='Internal server error') diff --git a/rhodecode/lib/celerylib/__init__.py b/rhodecode/lib/celerylib/__init__.py index de86c487..485732de 100644 --- a/rhodecode/lib/celerylib/__init__.py +++ b/rhodecode/lib/celerylib/__init__.py @@ -76,6 +76,18 @@ class RhodecodeCeleryTask(Task): request = get_current_request() + if hasattr(request, 'user'): + ip_addr = request.user.ip_addr + user_id = request.user.user_id + elif hasattr(request, 'rpc_params'): + # TODO(marcink) remove when migration is finished + # api specific call on Pyramid. + ip_addr = request.rpc_params['apiuser'].ip_addr + user_id = request.rpc_params['apiuser'].user_id + else: + raise Exception('Unable to fetch data from request: {}'.format( + request)) + if request: # we hook into kwargs since it is the only way to pass our data to # the celery worker in celery 2.2 @@ -91,8 +103,8 @@ class RhodecodeCeleryTask(Task): 'wsgi.url_scheme': request.environ['wsgi.url_scheme'], }, 'auth_user': { - 'ip_addr': request.user.ip_addr, - 'user_id': request.user.user_id + 'ip_addr': ip_addr, + 'user_id': user_id }, } })