# HG changeset patch # User Mads Kiilerich # Date 1473115878 -7200 # Node ID 81c13cdbe91f14b183f4f1208ae1c322bb34540e # Parent f770722cdb6cd2669d0e1f46f74b897e95477fc9 celerylib: improve handling of sync results and get rid of BaseAsyncResult handling A better wrapper of sync results simplifies the code. Note: Results are currently not really used. diff -r f770722cdb6c -r 81c13cdbe91f kallithea/controllers/admin/repos.py --- a/kallithea/controllers/admin/repos.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/controllers/admin/repos.py Tue Sep 06 00:51:18 2016 +0200 @@ -116,7 +116,6 @@ def create(self): self.__load_defaults() form_result = {} - task_id = None try: # CanWriteGroup validators checks permissions of this POST form_result = RepoForm(repo_groups=c.repo_groups, @@ -126,9 +125,7 @@ # create is done sometimes async on celery, db transaction # management is handled there. task = RepoModel().create(form_result, self.authuser.user_id) - from celery.result import BaseAsyncResult - if isinstance(task, BaseAsyncResult): - task_id = task.task_id + task_id = task.task_id except formencode.Invalid as errors: log.info(errors) return htmlfill.render( diff -r f770722cdb6c -r 81c13cdbe91f kallithea/controllers/api/api.py --- a/kallithea/controllers/api/api.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/controllers/api/api.py Tue Sep 06 00:51:18 2016 +0200 @@ -1504,10 +1504,7 @@ ) task = RepoModel().create(form_data=data, cur_user=owner) - from celery.result import BaseAsyncResult - task_id = None - if isinstance(task, BaseAsyncResult): - task_id = task.task_id + task_id = task.task_id # no commit, it's done in RepoModel, or async via celery return dict( msg="Created new repository `%s`" % (repo_name,), @@ -1690,10 +1687,7 @@ ) task = RepoModel().create_fork(form_data, cur_user=owner) # no commit, it's done in RepoModel, or async via celery - from celery.result import BaseAsyncResult - task_id = None - if isinstance(task, BaseAsyncResult): - task_id = task.task_id + task_id = task.task_id return dict( msg='Created fork of `%s` as `%s`' % (repo.repo_name, fork_name), diff -r f770722cdb6c -r 81c13cdbe91f kallithea/controllers/forks.py --- a/kallithea/controllers/forks.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/controllers/forks.py Tue Sep 06 00:51:18 2016 +0200 @@ -170,9 +170,7 @@ # create fork is done sometimes async on celery, db transaction # management is handled there. task = RepoModel().create_fork(form_result, self.authuser.user_id) - from celery.result import BaseAsyncResult - if isinstance(task, BaseAsyncResult): - task_id = task.task_id + task_id = task.task_id except formencode.Invalid as errors: return htmlfill.render( render('forks/fork.html'), diff -r f770722cdb6c -r 81c13cdbe91f kallithea/lib/celerylib/__init__.py --- a/kallithea/lib/celerylib/__init__.py Tue Sep 06 00:51:18 2016 +0200 +++ b/kallithea/lib/celerylib/__init__.py Tue Sep 06 00:51:18 2016 +0200 @@ -36,7 +36,6 @@ from hashlib import md5 from decorator import decorator -from kallithea.lib.vcs.utils.lazy import LazyProperty from kallithea import CELERY_ON, CELERY_EAGER from kallithea.lib.utils2 import str2bool, safe_str from kallithea.lib.pidlock import DaemonLock, LockHeld @@ -49,13 +48,18 @@ log = logging.getLogger(__name__) -class ResultWrapper(object): - def __init__(self, task): - self.task = task +class FakeTask(object): + """Fake a sync result to make it look like a finished task""" + + def __init__(self, result): + self.result = result - @LazyProperty - def result(self): - return self.task + def failed(self): + return False + + traceback = None # if failed + + task_id = None def run_task(task, *args, **kwargs): @@ -78,7 +82,12 @@ log.error(traceback.format_exc()) log.debug('executing task %s in sync mode', task) - return ResultWrapper(task(*args, **kwargs)) + try: + result = task(*args, **kwargs) + except Exception as e: + log.error('exception running sync task %s: %s', task, e) + raise # TODO: return this in FakeTask as with async tasks? + return FakeTask(result) def __get_lockkey(func, *fargs, **fkwargs):