1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#!/usr/bin/python
import asyncio
import logging
import joblib.joblib
class JobFailed(Exception):
pass
class Worker(object):
"""Abstract service worker class."""
def __init__(self, jobtype):
self._queue = None
self._jobtype = jobtype
async def connect(self, **queuedb):
self._queue = joblib.joblib.JobQueue()
await self._queue.connect(**queuedb)
await self._queue.setup()
async def setup(self):
# override
pass
async def _windup(self):
# override
pass
async def _execute_job(self, jobid, payload, priority):
# override
pass
async def _teardown(self, failed):
# override
pass
async def run(self, batchlimit=1):
"""Start jobs forever."""
while True:
jobs = await self._queue.acquire(jobtype=self._jobtype,
length=batchlimit)
if len(jobs) == 0:
await asyncio.sleep(1)
# nothing to do
continue
await self._windup()
critical_error = False
failed = []
for jobid, payload, priority in jobs:
try:
await self._execute_job(jobid, payload, priority)
except JobFailed as err:
failed.append((jobid, err.args[0]))
if len(err.args) > 1:
# rollback
critical_error = err.args[1]
if critical_error:
break
else:
critical_error = True # default
if critical_error:
await self._queue.reset([j[0] for j in jobs])
logging.warning("batch failed, reset")
else:
await self._queue.finish([j[0] for j in jobs])
for jobid, err in failed:
await self._queue.fail(jobid, err)
await self._teardown(failed=critical_error)
async def start(self, batchlimit=1):
"""Start in background."""
asyncio.ensure_future(self.run(batchlimit))
|