-
Notifications
You must be signed in to change notification settings - Fork 51
/
worker.py
323 lines (271 loc) · 11 KB
/
worker.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import asyncio
import contextlib
import logging
import time
from enum import Enum
from typing import Dict, Iterable, Optional, Set, Union
from procrastinate import app, exceptions, job_context, jobs, signals, tasks, utils
logger = logging.getLogger(__name__)
WORKER_NAME = "worker"
WORKER_TIMEOUT = 5.0 # seconds
WORKER_CONCURRENCY = 1 # parallel task(s)
class DeleteJobCondition(Enum):
"""
An enumeration with all the possible conditions to delete a job
"""
NEVER = "never" #: Keep jobs in database after completion
SUCCESSFUL = "successful" #: Delete only successful jobs
ALWAYS = "always" #: Always delete jobs at completion
class Worker:
def __init__(
self,
app: app.App,
queues: Optional[Iterable[str]] = None,
name: Optional[str] = None,
concurrency: int = WORKER_CONCURRENCY,
wait: bool = True,
timeout: float = WORKER_TIMEOUT,
listen_notify: bool = True,
delete_jobs: str = DeleteJobCondition.NEVER.value,
):
self.app = app
self.queues = queues
self.worker_name: str = name or WORKER_NAME
self.concurrency = concurrency
self.timeout = timeout
self.wait = wait
self.listen_notify = listen_notify
self.delete_jobs = DeleteJobCondition(delete_jobs)
# Handling the info about the currently running task.
self.known_missing_tasks: Set[str] = set()
self.job_manager = self.app.job_manager
if name:
self.logger = logger.getChild(name)
else:
self.logger = logger
self.base_context: job_context.JobContext = job_context.JobContext(
app=app, worker_name=self.worker_name, worker_queues=self.queues
)
self.current_contexts: Dict[int, job_context.JobContext] = {}
self.stop_requested = False
self.notify_event: Optional[asyncio.Event] = None
def context_for_worker(
self, worker_id: int, reset=False, **kwargs
) -> job_context.JobContext:
"""
Retrieves the context for sub-sworker ``worker_id``. If not found, or ``reset``
is True, context is recreated from ``self.base_context``. Additionnal parameters
are used to update the context. The resulting context is kept and will be
returned for later calls.
"""
if reset or worker_id not in self.current_contexts:
context = self.base_context
kwargs["worker_id"] = worker_id
else:
context = self.current_contexts[worker_id]
if kwargs:
context = context.evolve(**kwargs)
self.current_contexts[worker_id] = context
return context
def listener(self):
assert self.notify_event
return utils.task_context(
awaitable=self.job_manager.listen_for_jobs(
event=self.notify_event, queues=self.queues
),
name="listener",
)
def periodic_deferrer(self):
return utils.task_context(
awaitable=self.app.periodic_deferrer.worker(), name="periodic_deferrer"
)
async def run(self) -> None:
self.notify_event = asyncio.Event()
self.stop_requested = False
self.logger.info(
f"Starting worker on {self.base_context.queues_display}",
extra=self.base_context.log_extra(
action="start_worker", queues=self.queues
),
)
with contextlib.ExitStack() as stack:
if self.wait and self.listen_notify:
stack.enter_context(self.listener())
stack.enter_context(self.periodic_deferrer())
stack.enter_context(signals.on_stop(self.stop))
await asyncio.gather(
*(
self.single_worker(worker_id=worker_id)
for worker_id in range(self.concurrency)
)
)
self.logger.info(
f"Stopped worker on {self.base_context.queues_display}",
extra=self.base_context.log_extra(action="stop_worker", queues=self.queues),
)
self.notify_event = None
async def single_worker(self, worker_id: int):
current_timeout = self.timeout * (worker_id + 1)
while not self.stop_requested:
job = await self.job_manager.fetch_job(self.queues)
if job:
await self.process_job(job=job, worker_id=worker_id)
else:
if not self.wait or self.stop_requested:
break
await self.wait_for_job(timeout=current_timeout)
current_timeout = self.timeout * self.concurrency
async def wait_for_job(self, timeout: float):
assert self.notify_event
self.logger.debug(
f"Waiting for new jobs on queues " f"{self.base_context.queues_display}",
extra=self.base_context.log_extra(
action="waiting_for_jobs", queues=self.queues
),
)
self.notify_event.clear()
try:
await asyncio.wait_for(self.notify_event.wait(), timeout=timeout)
except asyncio.TimeoutError:
pass
else:
self.notify_event.clear()
async def process_job(self, job: jobs.Job, worker_id: int = 0) -> None:
context = self.context_for_worker(worker_id=worker_id, job=job)
self.logger.debug(
f"Loaded job info, about to start job {job.call_string}",
extra=context.log_extra(action="loaded_job_info"),
)
status, retry_at = None, None
try:
await self.run_job(job=job, worker_id=worker_id)
status = jobs.Status.SUCCEEDED
except exceptions.JobRetry as e:
retry_at = e.scheduled_at
except exceptions.JobError:
status = jobs.Status.FAILED
except exceptions.TaskNotFound as exc:
status = jobs.Status.FAILED
self.logger.exception(
f"Task was not found: {exc}",
extra=context.log_extra(action="task_not_found", exception=str(exc)),
)
finally:
if retry_at:
await self.job_manager.retry_job(job=job, retry_at=retry_at)
else:
assert status is not None
delete_job = {
DeleteJobCondition.ALWAYS: True,
DeleteJobCondition.NEVER: False,
DeleteJobCondition.SUCCESSFUL: status == jobs.Status.SUCCEEDED,
}[self.delete_jobs]
await self.job_manager.finish_job(
job=job, status=status, delete_job=delete_job
)
self.logger.debug(
f"Acknowledged job completion {job.call_string}",
extra=context.log_extra(action="finish_task", status=status),
)
# Remove job information from the current context
self.context_for_worker(worker_id=worker_id, reset=True)
def load_task(self, task_name: str, worker_id: int) -> tasks.Task:
if task_name in self.known_missing_tasks:
raise exceptions.TaskNotFound(f"Cancelling job for {task_name} (not found)")
try:
# Simple case: the task is already known
return self.app.tasks[task_name]
except KeyError:
pass
# Will raise if not found or not a task
try:
task = tasks.load_task(task_name)
except exceptions.ProcrastinateException:
self.known_missing_tasks.add(task_name)
raise
context = self.context_for_worker(worker_id=worker_id)
self.logger.warning(
f"Task at {task_name} was not registered, it's been loaded dynamically.",
extra=context.log_extra(action="load_dynamic_task", task_name=task_name),
)
self.app.tasks[task_name] = task
return task
async def run_job(self, job: jobs.Job, worker_id: int) -> None:
task_name = job.task_name
task = self.load_task(task_name=task_name, worker_id=worker_id)
context = self.context_for_worker(worker_id=worker_id, task=task)
start_time = context.additional_context["start_timestamp"] = time.time()
self.logger.info(
f"Starting job {job.call_string}",
extra=context.log_extra(action="start_job"),
)
exc_info: Union[bool, Exception]
job_args = []
if task.pass_context:
job_args.append(context)
try:
task_result = task(*job_args, **job.task_kwargs)
if asyncio.iscoroutine(task_result):
task_result = await task_result
elif self.concurrency != 1:
logger.warning(
"When using worker concurrency, non-async tasks will block "
"the whole worker.",
extra=context.log_extra(action="concurrent_sync_task"),
)
except Exception as e:
task_result = None
log_title = "Error"
log_action = "job_error"
log_level = logging.ERROR
exc_info = e
retry_exception = task.get_retry_exception(exception=e, job=job)
if retry_exception:
log_title = "Error, to retry"
log_action = "job_error_retry"
raise retry_exception from e
raise exceptions.JobError() from e
else:
log_title = "Success"
log_action = "job_success"
log_level = logging.INFO
exc_info = False
finally:
end_time = time.time()
duration = end_time - start_time
context.additional_context.update(
{
"end_timestamp": end_time,
"duration": duration,
"result": task_result,
}
)
extra = context.log_extra(action=log_action)
text = (
f"Job {job.call_string} ended with status: {log_title}, "
f"lasted {duration:.3f} s"
)
if task_result:
text += f" - Result: {task_result}"[:250]
self.logger.log(log_level, text, extra=extra, exc_info=exc_info)
def stop(self):
# Ensure worker will stop after finishing their task
self.stop_requested = True
# Ensure workers currently waiting are awakened
if self.notify_event:
self.notify_event.set()
# Logging
self.logger.info(
"Stop requested",
extra=self.base_context.log_extra(action="stopping_worker"),
)
contexts = [
context for context in self.current_contexts.values() if context.job
]
now = time.time()
for context in contexts:
self.logger.info(
"Waiting for job to finish: "
+ context.job_description(current_timestamp=now),
extra=context.log_extra(action="ending_job"),
)