Changeset - 81c13cdbe91f
[Not reviewed]
default
0 4 0
Mads Kiilerich - 9 years ago 2016-09-06 00:51:18
madski@unity3d.com
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.
4 files changed with 21 insertions and 23 deletions:
0 comments (0 inline, 0 general)
kallithea/controllers/admin/repos.py
Show inline comments
 
@@ -116,7 +116,6 @@ class ReposController(BaseRepoController
 
    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 @@ class ReposController(BaseRepoController
 
            # 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(
kallithea/controllers/api/api.py
Show inline comments
 
@@ -1504,10 +1504,7 @@ class ApiController(JSONRPCController):
 
            )
 

	
 
            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 @@ class ApiController(JSONRPCController):
 
            )
 
            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),
kallithea/controllers/forks.py
Show inline comments
 
@@ -170,9 +170,7 @@ class ForksController(BaseRepoController
 
            # 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'),
kallithea/lib/celerylib/__init__.py
Show inline comments
 
@@ -36,7 +36,6 @@ from pylons import config
 
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 @@ from sqlalchemy import engine_from_confi
 
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 @@ def run_task(task, *args, **kwargs):
 
            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):
0 comments (0 inline, 0 general)