Skip to content

Commit 15a4628

Browse files
committed
docs: add docs on background job plugin for reactive updates
1 parent cbda4c1 commit 15a4628

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ slug: /tutorial/Plugins/background-jobs
55
---
66

77
# Background jobs
8-
BackgroundJobsPlugin adds a durable background-job system to AdminForth. Jobs are stored in your data store (via a resource), executed by registered handlers, and automatically resumed after server restarts.
8+
BackgroundJobsPlugin adds a durable background job system to AdminForth. Jobs are stored in your data store through a resource, executed by registered handlers, and automatically resumed after server restarts.
99

1010

1111
## Setup
@@ -15,7 +15,7 @@ First, install the plugin:
1515
pnpm i @adminforth/background-jobs
1616
```
1717

18-
and create a resource for jobs:
18+
Then create a resource for jobs:
1919

2020

2121
```ts title="./resources/jobs.ts"
@@ -129,7 +129,7 @@ export default {
129129
} as AdminForthResourceInput;
130130
```
131131

132-
Then make add table schema:
132+
Then add the table schema:
133133

134134
```
135135
model jobs {
@@ -145,11 +145,11 @@ model jobs {
145145
}
146146
```
147147

148-
and make migration
148+
Then create a migration.
149149

150150

151151
## Usage
152-
The plugin saves tasks and keeps executing them even after a server restart, so you should register job task handlers at the start of the AdminForth application.
152+
The plugin saves tasks and keeps executing them after a server restart, so you should register job task handlers when the AdminForth application starts.
153153

154154
```ts title="./index.ts"
155155
//diff-add
@@ -212,7 +212,7 @@ The plugin saves tasks and keeps executing them even after a server restart, so
212212
//diff-add
213213
},
214214
//diff-add
215-
//limit of tasks, that are running in parallel
215+
// limit of tasks that are running in parallel
216216
//diff-add
217217
parallelLimit: 1
218218
//diff-add
@@ -308,7 +308,7 @@ If you need to react when the whole job is finished, pass `onAllTasksDone` to `r
308308
```
309309
310310
## Custom job state renderer
311-
There may be cases when you need to display the state of job tasks. For this, you can register a custom component.
311+
There may be cases where you need to display the state of job tasks. For this, you can register a custom component.
312312
313313
314314
```ts title="./custom/JobCustomComponent.vue"
@@ -372,16 +372,6 @@ onUnmounted(() => {
372372
</script>
373373
```
374374
375-
Job state and task state can be reactive on the frontend and updated when something changes on the backend.
376-
However, to avoid unnecessary high-volume backend updates that might not be needed on the frontend,
377-
all updates are disabled out of the box, and you need to specify which fields to subscribe to.
378-
379-
Use `subscribeToJobStateFields(['fieldName'])` for specific job state fields and
380-
`subscribeToJobTaskFields(['fieldName'])` for specific task state fields. The plugin applies
381-
incoming updates to the reactive `job` prop and to the task objects returned by
382-
`getJobTasks()`. Task field subscriptions receive updates for that field from every task
383-
in the opened job. Both helpers return an unsubscribe function; the plugin also unsubscribes
384-
remaining field subscriptions when the job dialog closes.
385375
386376
387377
Now register this component explicitly:
@@ -461,10 +451,25 @@ Finally, register this component alongside the job task handler:
461451

462452
```
463453
454+
### Reactive updates for job state and task state
455+
456+
You can activate automatic reactive updates for task state and job state on the frontend when you call backend state mutation methods like `setJobField` and `setTaskStateField`.
457+
458+
To avoid unnecessary high-volume backend updates for data that might not be needed on the frontend, all reactive updates are disabled by default, and you need to specify which fields to subscribe to.
459+
460+
Use `subscribeToJobStateFields(['fieldName1', 'fieldName2'])` for specific job state fields and
461+
`subscribeToJobTaskFields(['fieldName1', 'fieldName2'])` for specific task state fields.
462+
463+
The plugin applies incoming updates to the reactive `job` prop and to the task objects returned by
464+
`getJobTasks()`. Task field subscriptions receive updates for that field from every task
465+
in the open job. Both helpers return an unsubscribe function, though the plugin also automatically unsubscribes from
466+
remaining field subscriptions when the job dialog closes.
467+
468+
464469
465470
## Frontend API
466471
### Job info popup
467-
If you want to imedeatelly open job info popup, you shoul return job id from you API, that creates job:
472+
If you want to immediately open the job info popup, return the job ID from the API that creates the job:
468473
469474
For example:
470475
@@ -496,9 +501,9 @@ For example:
496501

497502
```
498503
499-
## Backend api
504+
## Backend API
500505
501-
Pluging provides some handy methods, that can be used in different situations:
506+
The plugin provides some handy methods that can be used in different situations:
502507
503508
```ts
504509
//set key:value to the job state in the DB
@@ -509,16 +514,17 @@ getJobField(jobId: string, key: string)
509514
getJobState(jobId: string)
510515
/**
511516
*
512-
* executes code atomically, if you have many task, that can update task state,
513-
* better use this method to avoid cases, when in the task state writes invalid data.
517+
* executes code atomically. If you have many tasks that can update task state,
518+
* use this method to avoid invalid task state writes.
514519
*
515520
**/
516521
updateJobFieldsAtomically(jobId: string, updateFunction: () => Promise<void>)
517522

518523
//for example
519524
backgroundJobsPlugin.updateJobFieldsAtomically(jobId, async () => {
520-
// do all set / get fields in this function to make state update atomic and there is no conflicts when 2 tasks in parallel do get before set.
521-
// don't do long awaits in this callback, since it has exclusive lock.
525+
// Do all set / get field operations in this function to make the state update atomic and avoid conflicts
526+
// when two parallel tasks get the same value before setting it.
527+
// Don't do long awaits in this callback, since it has an exclusive lock.
522528
let totalUsedTokens = await backgroundJobsPlugin.getJobField(jobId, 'totalUsedTokens');
523529
totalUsedTokens += promptCost;
524530
await backgroundJobsPlugin.setJobField(jobId, 'totalUsedTokens', totalUsedTokens);

0 commit comments

Comments
 (0)