|
| 1 | +from ._internal import import_attribute |
| 2 | +from .exceptions import TaskImportError |
| 3 | +from .timeouts import UnixSignalDeathPenalty |
| 4 | + |
| 5 | + |
| 6 | +class BaseRunner: |
| 7 | + """ |
| 8 | + Base implementation of the task runner. |
| 9 | + """ |
| 10 | + |
| 11 | + def __init__(self, tiger): |
| 12 | + self.tiger = tiger |
| 13 | + |
| 14 | + def run_single_task(self, task, hard_timeout): |
| 15 | + """ |
| 16 | + Run the given task using the hard timeout in seconds. |
| 17 | +
|
| 18 | + This is called inside of the forked process. |
| 19 | + """ |
| 20 | + raise NotImplementedError("Single tasks are not supported.") |
| 21 | + |
| 22 | + def run_batch_tasks(self, tasks, hard_timeout): |
| 23 | + """ |
| 24 | + Run the given tasks using the hard timeout in seconds. |
| 25 | +
|
| 26 | + This is called inside of the forked process. |
| 27 | + """ |
| 28 | + raise NotImplementedError("Batch tasks are not supported.") |
| 29 | + |
| 30 | + def run_eager_task(self, task): |
| 31 | + """ |
| 32 | + Run the task eagerly and return the value. |
| 33 | +
|
| 34 | + Note that the task function could be a batch function. |
| 35 | + """ |
| 36 | + raise NotImplementedError("Eager tasks are not supported.") |
| 37 | + |
| 38 | + def on_permanent_error(self, task, execution): |
| 39 | + """ |
| 40 | + Called if the task fails permanently. |
| 41 | +
|
| 42 | + A task fails permanently if its status is set to ERROR and it is no |
| 43 | + longer retried. |
| 44 | +
|
| 45 | + This is called in the main worker process. |
| 46 | + """ |
| 47 | + |
| 48 | + |
| 49 | +class DefaultRunner(BaseRunner): |
| 50 | + """ |
| 51 | + Default implementation of the task runner. |
| 52 | + """ |
| 53 | + |
| 54 | + def run_single_task(self, task, hard_timeout): |
| 55 | + with UnixSignalDeathPenalty(hard_timeout): |
| 56 | + task.func(*task.args, **task.kwargs) |
| 57 | + |
| 58 | + def run_batch_tasks(self, tasks, hard_timeout): |
| 59 | + params = [{'args': task.args, 'kwargs': task.kwargs} for task in tasks] |
| 60 | + func = tasks[0].func |
| 61 | + with UnixSignalDeathPenalty(hard_timeout): |
| 62 | + func(params) |
| 63 | + |
| 64 | + def run_eager_task(self, task): |
| 65 | + func = task.func |
| 66 | + is_batch_func = getattr(func, '_task_batch', False) |
| 67 | + |
| 68 | + if is_batch_func: |
| 69 | + return func([{'args': task.args, 'kwargs': task.kwargs}]) |
| 70 | + else: |
| 71 | + return func(*task.args, **task.kwargs) |
| 72 | + |
| 73 | + |
| 74 | +def get_runner_class(log, tasks): |
| 75 | + runner_class_paths = {task.serialized_runner_class for task in tasks} |
| 76 | + if len(runner_class_paths) > 1: |
| 77 | + log.error( |
| 78 | + "cannot mix multiple runner classes", |
| 79 | + runner_class_paths=", ".join(str(p) for p in runner_class_paths), |
| 80 | + ) |
| 81 | + raise ValueError("Found multiple runner classes in batch task.") |
| 82 | + |
| 83 | + runner_class_path = runner_class_paths.pop() |
| 84 | + if runner_class_path: |
| 85 | + try: |
| 86 | + return import_attribute(runner_class_path) |
| 87 | + except TaskImportError: |
| 88 | + log.error('could not import runner class', func=retry_func) |
| 89 | + raise |
| 90 | + return DefaultRunner |
0 commit comments