|
| 1 | +# Procrastinate Integration |
| 2 | + |
| 3 | +==Since v2.1.0== |
| 4 | + |
| 5 | +The Procrastinate integration lets you automatically create and update Task Badger tasks from |
| 6 | +[Procrastinate](https://procrastinate.readthedocs.io/){:target="_blank"} jobs. It tracks the full |
| 7 | +lifecycle of a job — `pending` when the job is deferred, `processing` when the worker starts it, and |
| 8 | +`success` or `error` when it finishes. |
| 9 | + |
| 10 | +Install the optional extra to pull in Procrastinate: |
| 11 | + |
| 12 | +```bash |
| 13 | +uv add 'taskbadger[procrastinate]' |
| 14 | +# or: pip install 'taskbadger[procrastinate]' |
| 15 | +``` |
| 16 | + |
| 17 | +There are two ways to use the integration: |
| 18 | + |
| 19 | +1. Use the `ProcrastinateSystemIntegration` to automatically track all tasks on a Procrastinate `App`. |
| 20 | +2. Use the `@track` decorator on individual tasks you wish to track. |
| 21 | + |
| 22 | +You can use both at the same time. The `@track` decorator is also useful when you want to access the |
| 23 | +Task Badger task object within the body of a job. |
| 24 | + |
| 25 | +## Procrastinate System Integration |
| 26 | + |
| 27 | +To track every task on a Procrastinate `App`, register the `ProcrastinateSystemIntegration` when |
| 28 | +initializing Task Badger: |
| 29 | + |
| 30 | +```python |
| 31 | +import taskbadger |
| 32 | +from taskbadger.systems.procrastinate import ProcrastinateSystemIntegration |
| 33 | + |
| 34 | +taskbadger.init( |
| 35 | + token="YOUR_API_KEY", |
| 36 | + systems=[ProcrastinateSystemIntegration(app=my_procrastinate_app)], |
| 37 | + tags={"environment": "production"}, |
| 38 | +) |
| 39 | +``` |
| 40 | + |
| 41 | +### System Integration Options |
| 42 | + |
| 43 | +The `ProcrastinateSystemIntegration` class takes the following parameters: |
| 44 | + |
| 45 | +- `app`: The `procrastinate.App` instance to instrument. **Required.** |
| 46 | +- `auto_track_tasks`: Set this to `False` to disable automatic tracking of tasks. Tasks decorated with |
| 47 | + `@track` will still be tracked. |
| 48 | +- `includes`: A list of task names or regex patterns to include. If this is set, only tasks that match |
| 49 | + one of the patterns will be tracked. |
| 50 | +- `excludes`: A list of task names or regex patterns to exclude. If this is set, tasks that match one of |
| 51 | + the patterns will not be tracked. |
| 52 | +- `record_task_args`: If `True`, the job's keyword arguments will be recorded in the Task Badger task |
| 53 | + data under `procrastinate_task_kwargs`. |
| 54 | + |
| 55 | +Patterns are matched against the full task name using `re.fullmatch`. Exclusions take precedence over |
| 56 | +inclusions, so if a task name matches both an include and an exclude, it will be excluded. |
| 57 | + |
| 58 | +Procrastinate's built-in housekeeping tasks (those named `builtin:...` or `procrastinate.*`) are never |
| 59 | +auto-tracked. |
| 60 | + |
| 61 | +```python |
| 62 | +taskbadger.init( |
| 63 | + token="YOUR_API_KEY", |
| 64 | + systems=[ProcrastinateSystemIntegration( |
| 65 | + app=app, |
| 66 | + includes=[r"myapp\..*"], |
| 67 | + excludes=[r"myapp\.cleanup\..*"], |
| 68 | + record_task_args=True, |
| 69 | + )], |
| 70 | +) |
| 71 | +``` |
| 72 | + |
| 73 | +!!! info |
| 74 | + |
| 75 | + Construct the integration **after** all tasks and blueprints have been registered on the `App`. |
| 76 | + Tasks added via `app.add_tasks_from(blueprint)` after the integration is constructed are not |
| 77 | + auto-instrumented — apply `@track` to those tasks explicitly. |
| 78 | + |
| 79 | +## The `track` Decorator |
| 80 | + |
| 81 | +To track individual tasks, or if you want access to the Task Badger task object within the body of a |
| 82 | +job, apply the `@track` decorator **outside** (above) the `@app.task` decorator: |
| 83 | + |
| 84 | +```python |
| 85 | +import procrastinate |
| 86 | +from taskbadger.procrastinate import track |
| 87 | + |
| 88 | +app = procrastinate.App(connector=...) |
| 89 | + |
| 90 | + |
| 91 | +@track |
| 92 | +@app.task(queue="default") |
| 93 | +async def add(a, b): |
| 94 | + return a + b |
| 95 | +``` |
| 96 | + |
| 97 | +With this change a Task Badger task is created in the `pending` state when the job is deferred, and it |
| 98 | +is updated to `processing` and then `success` or `error` as the worker runs it. |
| 99 | + |
| 100 | +Tasks decorated with `@track` are always tracked, irrespective of the `includes` / `excludes` rules on |
| 101 | +the `ProcrastinateSystemIntegration`. |
| 102 | + |
| 103 | +### Task Customization |
| 104 | + |
| 105 | +The `@track` decorator accepts the following optional keyword arguments, applied when the Task Badger |
| 106 | +task is created: |
| 107 | + |
| 108 | +- `name`: The Task Badger task name. Defaults to the Procrastinate task's name. |
| 109 | +- `value_max`: The maximum value for the task. |
| 110 | +- `tags`: A dictionary of tags applied to the task. |
| 111 | +- `data`: A dictionary of initial data merged into the task. |
| 112 | +- `record_task_args`: If `True`, the job's keyword arguments are recorded under |
| 113 | + `data["procrastinate_task_kwargs"]`. Defaults to inheriting the value from the |
| 114 | + `ProcrastinateSystemIntegration` if one is configured, otherwise `False`. |
| 115 | + |
| 116 | +```python |
| 117 | +@track(name="report", value_max=100, tags={"env": "prod"}, record_task_args=True) |
| 118 | +@app.task |
| 119 | +async def report(rows): |
| 120 | + ... |
| 121 | +``` |
| 122 | + |
| 123 | +## Accessing the Task Object |
| 124 | + |
| 125 | +Use `current_task()` to get the Task Badger task for the currently-running job. This is useful for |
| 126 | +updating progress or data from within the job body: |
| 127 | + |
| 128 | +```python |
| 129 | +from taskbadger.procrastinate import track, current_task |
| 130 | + |
| 131 | + |
| 132 | +@track |
| 133 | +@app.task |
| 134 | +async def report(rows): |
| 135 | + tb = current_task() |
| 136 | + for i, row in enumerate(rows): |
| 137 | + await process(row) |
| 138 | + if i % 10 == 0: |
| 139 | + tb.update(value=i, value_max=len(rows)) |
| 140 | +``` |
| 141 | + |
| 142 | +!!! note |
| 143 | + |
| 144 | + `current_task()` returns `None` outside of a tracked job, if Task Badger has not been |
| 145 | + [configured](python.md#configure), or if the task could not be fetched. |
| 146 | + |
| 147 | +## Periodic Tasks |
| 148 | + |
| 149 | +Periodic tasks scheduled with `@app.periodic` are tracked as well. Each periodic deferral creates a new |
| 150 | +Task Badger task, so you can monitor the history and health of your scheduled jobs. |
| 151 | + |
| 152 | +## Queue Field |
| 153 | + |
| 154 | +The name of the Procrastinate queue a task is deferred to is automatically recorded on the Task Badger |
| 155 | +task's [`queue`](data_model.md#queue) field. |
| 156 | + |
| 157 | +## Known Limitations |
| 158 | + |
| 159 | +Procrastinate has no signals or middleware system, so the integration works by wrapping the task |
| 160 | +function and the `defer` methods. This leads to a few cases that are not tracked: |
| 161 | + |
| 162 | +- **`task.configure(...).defer(...)` is not tracked.** `configure()` returns a separate `JobDeferrer` |
| 163 | + whose methods bypass the wrapper. Use `task.defer(...)` directly for tracked deferrals. Jobs deferred |
| 164 | + via `configure().defer()` will still run normally, they just won't appear in Task Badger. |
| 165 | +- **`task.batch_defer*` is not tracked**, for the same reason. |
| 166 | +- **Tasks added via `app.add_tasks_from(blueprint)` after the `ProcrastinateSystemIntegration` is |
| 167 | + constructed are not auto-instrumented.** Construct the integration after all blueprints are |
| 168 | + registered, or apply `@track` to those tasks explicitly. |
| 169 | + |
| 170 | +Cancelled, aborted, and retried jobs surface as `error` in Task Badger. |
0 commit comments