# HG changeset patch # User RhodeCode Admin # Date 2023-01-31 20:46:38 # Node ID 92d53da00a5cfa50f51667a7bca1641939f8799c # Parent d7efefeb242c5433d52c69b97c22fc01d1d1dc84 celery: don't allow subtasks within tasks 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 @@ -43,14 +43,26 @@ class ResultWrapper(object): def run_task(task, *args, **kwargs): + import celery log.debug('Got task `%s` for execution, celery mode enabled:%s', task, rhodecode.CELERY_ENABLED) if task is None: raise ValueError('Got non-existing task for execution') exec_mode = 'sync' + allow_async = True + + # if we're already in a celery task, don't allow async execution again + # e.g task within task + in_task = celery.current_task + if in_task: + log.debug('This task in in context of another task: %s, not allowing another async execution', in_task) + allow_async = False + if kwargs.pop('allow_subtask', False): + log.debug('Forced async by allow_async=True flag') + allow_async = True t = None - if rhodecode.CELERY_ENABLED: + if rhodecode.CELERY_ENABLED and allow_async: try: t = task.apply_async(args=args, kwargs=kwargs) log.debug('executing task %s:%s in async mode', t.task_id, task) diff --git a/rhodecode/lib/celerylib/loader.py b/rhodecode/lib/celerylib/loader.py --- a/rhodecode/lib/celerylib/loader.py +++ b/rhodecode/lib/celerylib/loader.py @@ -182,6 +182,8 @@ def task_retry_signal( @signals.task_failure.connect def task_failure_signal( task_id, exception, args, kwargs, traceback, einfo, **kargs): + + log.error('Task: %s failed !! exc_info: %s', task_id, einfo) from rhodecode.lib.exc_tracking import store_exception from rhodecode.lib.statsd_client import StatsdClient @@ -276,14 +278,8 @@ class RequestContextTask(Task): log.debug('Running Task with class: %s. Request Class: %s', self.__class__, req.__class__) - proxy_data = getattr(self.request, 'rhodecode_proxy_data', None) - log.debug('celery proxy data:%r', proxy_data) - user_id = None ip_addr = None - if proxy_data: - user_id = proxy_data['auth_user']['user_id'] - ip_addr = proxy_data['auth_user']['ip_addr'] # web case if hasattr(req, 'user'):