Commits

Ronny Pfannschmidt committed 7a4fc09

rename handlers.slave to work

Comments (0)

Files changed (6)

juggler/handlers/slave.py

-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 = owner.name
-    try:
-        db.save_doc(task)
-        log.info(
-            'claiming task {task._id} for {owner.name}',
-            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
-        log.info(
-            'claiming task {task._id} for {owner.name} 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):
-    log.info(
-        "worker {owner.name} waited for {task._id} of {task.owner}",
-        owner=owner, task=task,
-    )
-    if task.owner == owner.name:
-        return task
-
-
-@watches_for(Task, s.claimed, owner=lambda kw: kw['owner'])
-def run_one_claimed_task(db, task, owner, run):
-    log.info('dispatching task {task._id}', task=task)
-    run(task)

juggler/handlers/work.py

+from couchdbkit.exceptions import ResourceConflict
+from ..model import Task, states as s
+from .utils import watches_for
+
+from logbook import Logger
+log = Logger('work')
+
+
+@watches_for(Task, s.pending)
+def claim_pending_task(db, task, owner):
+    task.status = s.claiming
+    task.owner = owner.name
+    try:
+        db.save_doc(task)
+        log.info(
+            'claiming task {task._id} for {owner.name}',
+            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
+        log.info(
+            'claiming task {task._id} for {owner.name} 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):
+    log.info(
+        "worker {owner.name} waited for {task._id} of {task.owner}",
+        owner=owner, task=task,
+    )
+    if task.owner == owner.name:
+        return task
+
+
+@watches_for(Task, s.claimed, owner=lambda kw: kw['owner'])
+def run_one_claimed_task(db, task, owner, run):
+    log.info('dispatching task {task._id}', task=task)
+    run(task)

juggler/simple_slave.py

-from juggler.handlers import slave
+import py
+from juggler.handlers import work
 
 
 def simple(service, args=None):
     #XXX hack
     #XXX: better basedir
-    import py
-    basedir = py.path.local(service.name).ensure(dir=1)
-    service.path = basedir
+    if service.path is None:
+        basedir = py.path.local(service.name).ensure(dir=1)
+        service.path = basedir
+
     while True:
         run_once(service)
 
 
 def run_once(service):
-    claiming = slave.claim_pending_task(service, owner=service)
+    claiming = work.claim_pending_task(service, owner=service)
     if claiming is None:
         return
-    task = slave.wait_for_one_claiming_task(service,
-                                            id=claiming._id,
-                                            owner=service)
+    task = work.wait_for_one_claiming_task(service,
+                                           id=claiming._id,
+                                           owner=service)
     if task is not None:
-        slave.run_one_claimed_task(service, task,
-                                   owner=service.name,
-                                   run=service.run_task)
+        work.run_one_claimed_task(service, task,
+                                  owner=service.name,
+                                  run=service.run_task)

testing/acceptance/test_scripted_end_to_end.py

 import pytest
 from juggler import async
 from juggler.service import Juggler
-from juggler.handlers import inbox, manage, slave
+from juggler.handlers import inbox, manage, work
 from juggler.model import Project, Order
 from juggler.process.subprocess import python_template
 from testing import with_quick_change_timeout
     inbox.ready_order_generate_tasks(juggler)
     manage.new_task_generate_steps(juggler)
 
-    slave.claim_pending_task(juggler, owner=juggler)
+    work.claim_pending_task(juggler, owner=juggler)
 
     manage.approve_claimed_task(juggler)
-    slave.run_one_claimed_task(juggler,
-                               owner=juggler.name,
-                               run=juggler.run_task)
+    work.run_one_claimed_task(juggler,
+                              owner=juggler.name,
+                              run=juggler.run_task)
 
 
 @pytest.mark.changes_extra(timeout=300)

testing/handlers/test_slave.py

-import pytest
-from couchdbkit.exceptions import ResourceConflict
-from juggler import model
-from juggler.handlers import slave
-from testing import with_quick_change_timeout
-
-
-@with_quick_change_timeout
-@pytest.mark.parametrize('conflict', [False, True],
-                         ids=['pass', 'conflict'])
-def test_claim_pending_task(db, conflict):
-    task = model.Task(status='pending', project='blabla')
-    db.save_doc(task)
-    if conflict:
-        db._.save_doc.side_effect = ResourceConflict()
-
-    class owner:
-        name = 'test'
-
-    result = slave.claim_pending_task(db, owner=owner)
-    db.refresh(task)
-    if not conflict:
-        assert task.owner == 'test'
-        assert task.status == 'claiming'
-        assert result._id == task._id
-
-
-@with_quick_change_timeout
-def test_run_one_claimed_task(db):
-    task = model.Task(_id='claim', owner='o', status='claimed')
-    db.save_doc(task)
-
-    def run(given):
-        assert given._id == task._id
-
-    slave.run_one_claimed_task(db, owner='o', run=run)

testing/handlers/test_work.py

+import pytest
+from couchdbkit.exceptions import ResourceConflict
+from juggler import model
+from juggler.handlers import work
+from testing import with_quick_change_timeout
+
+
+@with_quick_change_timeout
+@pytest.mark.parametrize('conflict', [False, True],
+                         ids=['pass', 'conflict'])
+def test_claim_pending_task(db, conflict):
+    task = model.Task(status='pending', project='blabla')
+    db.save_doc(task)
+    if conflict:
+        db._.save_doc.side_effect = ResourceConflict()
+
+    class owner:
+        name = 'test'
+
+    result = work.claim_pending_task(db, owner=owner)
+    db.refresh(task)
+    if not conflict:
+        assert task.owner == 'test'
+        assert task.status == 'claiming'
+        assert result._id == task._id
+
+
+@with_quick_change_timeout
+def test_run_one_claimed_task(db):
+    task = model.Task(_id='claim', owner='o', status='claimed')
+    db.save_doc(task)
+
+    def run(given):
+        assert given._id == task._id
+
+    work.run_one_claimed_task(db, owner='o', run=run)