background-job 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/README.md +143 -0
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +2 -2
- package/package.json +4 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Mahmuthan Elbir
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mahmuthan Elbir
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Background Job
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/background-job)
|
|
4
|
+
|
|
5
|
+
Managed background jobs for Node.js. Provides suspension, disabling, and controlled execution loops for long-running worker processes.
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm i background-job
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Ships a dual ESM/CJS build, so both forms are supported:
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
// ES Module
|
|
16
|
+
import {BackgroundJob, BackgroundJobRunner} from "background-job";
|
|
17
|
+
|
|
18
|
+
// CommonJS
|
|
19
|
+
const {BackgroundJob, BackgroundJobRunner} = require("background-job");
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
Subclass `BackgroundJob` for the work and `BackgroundJobRunner` to enumerate which jobs run.
|
|
25
|
+
|
|
26
|
+
### Fixed worker pool
|
|
27
|
+
|
|
28
|
+
A set of identical workers that each loop with a delay — sized from config, backing off when there is nothing to do:
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import {BackgroundJob, BackgroundJobRunner} from "background-job";
|
|
32
|
+
|
|
33
|
+
class QueueWorker extends BackgroundJob {
|
|
34
|
+
async execute() {
|
|
35
|
+
const item = await pullNextItem();
|
|
36
|
+
if (!item) {
|
|
37
|
+
return this.disable(); // stop and cool down; a later poll re-enables it
|
|
38
|
+
}
|
|
39
|
+
await processItem(item);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class QueueRunner extends BackgroundJobRunner {
|
|
44
|
+
async getExecutableJobs() {
|
|
45
|
+
const workers = config.get("queue.workers", 3);
|
|
46
|
+
return this.constructor.iteratingJobList(workers, QueueWorker, {delayMs: 1000});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await new QueueRunner("queue").executeJobs(); // runs forever, polling for executable jobs
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### One job per entity
|
|
54
|
+
|
|
55
|
+
When the work is dynamic — one job per row from a data source — return the `BackgroundJobDefinition` objects directly.
|
|
56
|
+
`initParams` carries per-job context into `init()`, and the lifecycle hooks set up and tear down resources:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
import {BackgroundJob, BackgroundJobRunner} from "background-job";
|
|
60
|
+
|
|
61
|
+
class AccountJob extends BackgroundJob {
|
|
62
|
+
init(account) {
|
|
63
|
+
this.account = account; // the definition's initParams
|
|
64
|
+
this.api = createApiClient(account);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async execute() {
|
|
68
|
+
await this.api.sync(this.account);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
destroy() {
|
|
72
|
+
this.api.close(); // called once when the job stops
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class AccountRunner extends BackgroundJobRunner {
|
|
77
|
+
async getExecutableJobs() {
|
|
78
|
+
const accounts = await loadActiveAccounts(); // dynamic, e.g. from a DB
|
|
79
|
+
return accounts.map((account) => ({
|
|
80
|
+
id: account.id,
|
|
81
|
+
jobClass: AccountJob,
|
|
82
|
+
initParams: account,
|
|
83
|
+
delayMs: 2500,
|
|
84
|
+
}));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
await new AccountRunner("account").executeJobs();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Returning a job's id keeps it running; omitting it on a later poll disables it (and triggers its `destroy()`).
|
|
92
|
+
`getExecutableJobs()` is the single source of truth for what should be active.
|
|
93
|
+
|
|
94
|
+
## How it works
|
|
95
|
+
|
|
96
|
+
`executeJobs()` runs a polling loop every `pollIntervalMs`. On each tick it calls `getExecutableJobs()`, lifts any
|
|
97
|
+
expired suspensions, and starts every enabled job that is not already running. Each started job is jittered to avoid a
|
|
98
|
+
thundering herd, then loops its own `execute()` with the configured delay until it is disabled.
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### `BackgroundJob`
|
|
103
|
+
|
|
104
|
+
Base class for an individual job. The runner instantiates it for you — subclass and override the hooks.
|
|
105
|
+
|
|
106
|
+
| Member | Description |
|
|
107
|
+
|-------------------------------------|-------------------------------------------------------------------------------------------------|
|
|
108
|
+
| `async execute()` | **Abstract.** The job's work; must be overridden or it throws. Called repeatedly while enabled. |
|
|
109
|
+
| `init(params)` | Optional setup hook, called once before the loop starts. |
|
|
110
|
+
| `destroy()` | Optional teardown hook, called once after the loop ends. |
|
|
111
|
+
| `isEnabled()` | `boolean` — whether this job is still enabled in the runner. |
|
|
112
|
+
| `disable(suspendDurationMs = 5000)` | Disables the job and suspends it for a cooldown; pass `0` to disable without suspending. |
|
|
113
|
+
| `runner` / `id` / `name` | The owning runner, this job's id, and its derived name (`"<base> #<id>"`). |
|
|
114
|
+
|
|
115
|
+
### `BackgroundJobRunner`
|
|
116
|
+
|
|
117
|
+
Orchestrator that owns enabled/running/suspended state per job id.
|
|
118
|
+
|
|
119
|
+
| Member | Description |
|
|
120
|
+
|-----------------------------------------------------|---------------------------------------------------------------------------------------------------------------|
|
|
121
|
+
| `new BackgroundJobRunner(name = "BackgroundJob")` | `name` is the base name jobs derive from; `" Runner"` is appended for the runner's own name. |
|
|
122
|
+
| `async getExecutableJobs()` | **Abstract.** Return the `BackgroundJobDefinition[]` that should be running; must be overridden or it throws. |
|
|
123
|
+
| `async executeJobs(pollIntervalMs = 1000)` | Starts the main loop. Runs indefinitely. |
|
|
124
|
+
| `isJobEnabled(id)` / `isJobRunning(id)` | `boolean` state checks by id. |
|
|
125
|
+
| `disableJob(id)` | Disables a job by id (stops its loop on the next iteration). |
|
|
126
|
+
| `suspendJob(id, durationMs)` | Suspends a job by id for `durationMs`, preventing re-enable until it elapses. |
|
|
127
|
+
| `static iteratingJobList(count, jobClass, options)` | Builds `count` identical definitions with sequential 1-based index ids. |
|
|
128
|
+
|
|
129
|
+
### `BackgroundJobDefinition`
|
|
130
|
+
|
|
131
|
+
The object returned (per job) by `getExecutableJobs()`:
|
|
132
|
+
|
|
133
|
+
| Field | Type | Description |
|
|
134
|
+
|---------------|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
135
|
+
| `id` | `string \| number` | Unique job identifier. |
|
|
136
|
+
| `jobClass` | `typeof BackgroundJob` | The `BackgroundJob` subclass to instantiate. |
|
|
137
|
+
| `initParams` | `*` | Optional — passed to the job's `init()`. |
|
|
138
|
+
| `delayMs` | `number` | Optional (default `0`) — delay between executions in ms. |
|
|
139
|
+
| `isFixedRate` | `boolean` | Optional (default `false`) — when `true`, execute time counts toward the delay (fixed-rate); otherwise the full delay is waited after each execute (fixed-delay). |
|
|
140
|
+
|
|
141
|
+
## License
|
|
142
|
+
|
|
143
|
+
The MIT License (MIT). Please see [License File](LICENSE) for more information.
|
package/dist/index.cjs
CHANGED
|
@@ -56,9 +56,9 @@ var BackgroundJob = class {
|
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
58
|
* Disables this job and optionally suspends it for a cooldown period.
|
|
59
|
-
* @param {number} [suspendDurationMs=
|
|
59
|
+
* @param {number} [suspendDurationMs=5000] - Cooldown in ms before the job may run again; 0 disables without suspending
|
|
60
60
|
*/
|
|
61
|
-
disable(suspendDurationMs =
|
|
61
|
+
disable(suspendDurationMs = 5e3) {
|
|
62
62
|
this.runner.disableJob(this.id);
|
|
63
63
|
suspendDurationMs && this.runner.suspendJob(this.id, suspendDurationMs);
|
|
64
64
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -55,9 +55,9 @@ var BackgroundJob = class {
|
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
57
|
* Disables this job and optionally suspends it for a cooldown period.
|
|
58
|
-
* @param {number} [suspendDurationMs=
|
|
58
|
+
* @param {number} [suspendDurationMs=5000] - Cooldown in ms before the job may run again; 0 disables without suspending
|
|
59
59
|
*/
|
|
60
|
-
disable(suspendDurationMs =
|
|
60
|
+
disable(suspendDurationMs = 5e3) {
|
|
61
61
|
this.runner.disableJob(this.id);
|
|
62
62
|
suspendDurationMs && this.runner.suspendJob(this.id, suspendDurationMs);
|
|
63
63
|
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "background-job",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Managed background jobs for Node.js — suspension, disabling, and controlled execution loops.",
|
|
6
6
|
"keywords": [
|
|
7
|
-
"background-job",
|
|
8
|
-
"background",
|
|
9
|
-
"job",
|
|
10
|
-
"jobs",
|
|
11
7
|
"worker",
|
|
12
8
|
"runner",
|
|
13
9
|
"scheduler",
|
|
14
10
|
"task",
|
|
15
|
-
"queue"
|
|
16
|
-
"esm"
|
|
11
|
+
"queue"
|
|
17
12
|
],
|
|
18
13
|
"homepage": "https://github.com/mahelbir/background-job#readme",
|
|
19
14
|
"bugs": "https://github.com/mahelbir/background-job/issues",
|
|
@@ -41,10 +36,10 @@
|
|
|
41
36
|
"relock": "rm -rf node_modules package-lock.json && NODE_ENV= npm install --include=dev --include=optional"
|
|
42
37
|
},
|
|
43
38
|
"dependencies": {
|
|
44
|
-
"melperjs": "^17.
|
|
39
|
+
"melperjs": "^17.2.0"
|
|
45
40
|
},
|
|
46
41
|
"devDependencies": {
|
|
47
|
-
"tsdown": "^0.22.
|
|
42
|
+
"tsdown": "^0.22.3"
|
|
48
43
|
},
|
|
49
44
|
"engines": {
|
|
50
45
|
"node": ">=18.0.0"
|