# HG changeset patch # User Marcin Kuzminski # Date 2017-01-18 22:13:27 # Node ID b1a9c9bbaf0cf577119572321da64f25acaf1fbf # Parent 77ebdd4cf25e85ae78fb4a1b5df6f93c69e4baa1 celery: handle pyramid/pylons context better when running async tasks. diff --git a/rhodecode/api/__init__.py b/rhodecode/api/__init__.py --- 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 --- 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 }, } })