Skip to content

Commit a7648cf

Browse files
committed
docs: update background jobs plugin
1 parent dbffecb commit a7648cf

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

adminforth/documentation/docs/tutorial/08-Plugins/23-background-jobs.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,12 @@ After registering a handler, you can create a job. For example:
308308
## Run code after all tasks are done
309309
If you need to react when the whole job is finished, pass `onAllTasksDone` to `registerTaskHandler`.
310310
311+
The callback is called when all currently scheduled tasks are done, before the job is marked as `DONE` or `DONE_WITH_ERRORS`.
312+
This means you can use it as a continuation hook: add more tasks with `addNewTasksToExistingJob`, and the same job will keep running.
313+
After those new tasks finish, `onAllTasksDone` is called again.
314+
315+
`finishAttemptNumber` starts from `1` and increments every time the job reaches this candidate-finish point again. Use it to avoid accidental infinite continuations.
316+
311317
```ts title="./index.ts"
312318
...
313319

@@ -316,13 +322,30 @@ If you need to react when the whole job is finished, pass `onAllTasksDone` to `r
316322
handler: async ({ jobId, setTaskStateField, getTaskStateField, getState }) => {
317323
// task logic
318324
},
319-
onAllTasksDone: async ({ jobId, failedTasks, succeededTasks }) => {
320-
console.log('job finished', { jobId, failedTasks, succeededTasks });
325+
onAllTasksDone: async ({ jobId, failedTasks, succeededTasks, finishAttemptNumber }) => {
326+
console.log('job reached finish point', {
327+
jobId,
328+
failedTasks,
329+
succeededTasks,
330+
finishAttemptNumber,
331+
});
332+
333+
if (finishAttemptNumber > 1) {
334+
return;
335+
}
336+
337+
await backgroundJobsPlugin.addNewTasksToExistingJob(jobId, [
338+
{ state: { userId: 'user-1001' } },
339+
{ state: { userId: 'user-1002' } },
340+
]);
321341
},
322342
});
323343

324344
```
325345
346+
In this example, the job runs its initial tasks, calls `onAllTasksDone` with `finishAttemptNumber: 1`, appends two more tasks, runs them, and then calls `onAllTasksDone` again with `finishAttemptNumber: 2`.
347+
If the second callback does not add more tasks, the job is marked as finished.
348+
326349
## Custom job state renderer
327350
There may be cases where you need to display the state of job tasks. For this, you can register a custom component.
328351

0 commit comments

Comments
 (0)