juggler / juggler / handlers /

Full commit
from couchdbkit.exceptions import ResourceConflict
from ..model import Task, states as s
from .utils import watches_for

from logbook import Logger
log = Logger('slave')

@watches_for(Task, s.pending)
def claim_pending_task(db, task, owner):
    task.status = s.claiming
    task.owner =
            'claiming task {task._id} for {}',
            task=task, owner=owner,
        return task
    except ResourceConflict:
        # its important to ignore conflicts here
        # it just means something already claimed
        # no need to generate more conflict
            'claiming task {task._id} for {} failed',
            task=task, owner=owner,

@watches_for(Task, s.claimed, _id=lambda kw: kw['id'])
def wait_for_one_claiming_task(db, task, id, owner):
        "worker {} waited for {task._id} of {task.owner}",
        owner=owner, task=task,
    if task.owner ==
        return task

@watches_for(Task, s.claimed, owner=lambda kw: kw['owner'])
def run_one_claimed_task(db, task, owner, run):'dispatching task {task._id}', task=task)