background-job 1.0.0
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 +21 -0
- package/dist/index.cjs +208 -0
- package/dist/index.mjs +206 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +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
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let melperjs = require("melperjs");
|
|
3
|
+
//#region src/utils.js
|
|
4
|
+
/**
|
|
5
|
+
* Throws to signal an abstract method that a subclass must override.
|
|
6
|
+
* @param {string} method - Name of the method that must be implemented
|
|
7
|
+
* @returns {never}
|
|
8
|
+
*/
|
|
9
|
+
function notImplemented(method) {
|
|
10
|
+
throw new Error(`${method}() must be implemented by a subclass`);
|
|
11
|
+
}
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/background-job.js
|
|
14
|
+
/**
|
|
15
|
+
* Base class for an individual background job.
|
|
16
|
+
* Subclass and override {@link BackgroundJob#execute} for the work, with
|
|
17
|
+
* optional {@link BackgroundJob#init}/{@link BackgroundJob#destroy} lifecycle hooks.
|
|
18
|
+
*/
|
|
19
|
+
var BackgroundJob = class {
|
|
20
|
+
/**
|
|
21
|
+
* @param {import("./background-job-runner.js").BackgroundJobRunner} runner - Runner that owns and orchestrates this job
|
|
22
|
+
* @param {string|number} id - Unique identifier of this job within the runner
|
|
23
|
+
*/
|
|
24
|
+
constructor(runner, id) {
|
|
25
|
+
/** @type {import("./background-job-runner.js").BackgroundJobRunner} */
|
|
26
|
+
this.runner = runner;
|
|
27
|
+
/** @type {string|number} */
|
|
28
|
+
this.id = id;
|
|
29
|
+
/** @type {string} */
|
|
30
|
+
this.name = `${runner.baseName} #${id}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Optional setup hook, called once before the execution loop starts.
|
|
34
|
+
* @param {*} [params] - Arbitrary parameters supplied via the job definition
|
|
35
|
+
* @returns {void}
|
|
36
|
+
*/
|
|
37
|
+
init(params = null) {}
|
|
38
|
+
/**
|
|
39
|
+
* Optional teardown hook, called once after the execution loop ends.
|
|
40
|
+
* @returns {void}
|
|
41
|
+
*/
|
|
42
|
+
destroy() {}
|
|
43
|
+
/**
|
|
44
|
+
* @abstract Override in a subclass to perform the job's work.
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
*/
|
|
47
|
+
async execute() {
|
|
48
|
+
notImplemented("execute");
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Whether this job is currently enabled in the runner.
|
|
52
|
+
* @returns {boolean}
|
|
53
|
+
*/
|
|
54
|
+
isEnabled() {
|
|
55
|
+
return this.runner.isJobEnabled(this.id);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Disables this job and optionally suspends it for a cooldown period.
|
|
59
|
+
* @param {number} [suspendDurationMs=3000] - Cooldown in ms before the job may run again; 0 disables without suspending
|
|
60
|
+
*/
|
|
61
|
+
disable(suspendDurationMs = 3e3) {
|
|
62
|
+
this.runner.disableJob(this.id);
|
|
63
|
+
suspendDurationMs && this.runner.suspendJob(this.id, suspendDurationMs);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/background-job-runner.js
|
|
68
|
+
/**
|
|
69
|
+
* @typedef {Object} BackgroundJobDefinition
|
|
70
|
+
* @property {string|number} id - Unique job identifier
|
|
71
|
+
* @property {typeof import("./background-job.js").BackgroundJob} jobClass - BackgroundJob subclass constructor
|
|
72
|
+
* @property {*} [initParams] - Parameters passed to init()
|
|
73
|
+
* @property {number} [delayMs=0] - Delay between executions in ms
|
|
74
|
+
* @property {boolean} [isFixedRate=false] - When true, execute time counts toward the delay (fixed-rate); otherwise the full delay is waited after each execute (fixed-delay)
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* Orchestrates background jobs, owning enabled/running/suspended state per job id.
|
|
78
|
+
* Subclass and override {@link BackgroundJobRunner#getExecutableJobs} to enumerate work.
|
|
79
|
+
*/
|
|
80
|
+
var BackgroundJobRunner = class {
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} [name="BackgroundJob"] - Base name; jobs derive their name from it and " Runner" is appended to form {@link BackgroundJobRunner#name}
|
|
83
|
+
*/
|
|
84
|
+
constructor(name = "BackgroundJob") {
|
|
85
|
+
/** @type {string} */
|
|
86
|
+
this.baseName = name;
|
|
87
|
+
/** @type {string} */
|
|
88
|
+
this.name = `${name} Runner`;
|
|
89
|
+
/** @type {Object<string|number, boolean>} */
|
|
90
|
+
this.enabledJobs = {};
|
|
91
|
+
/** @type {Object<string|number, import("./background-job.js").BackgroundJob>} */
|
|
92
|
+
this.runningJobs = {};
|
|
93
|
+
/** @type {Object<string|number, number>} */
|
|
94
|
+
this.suspendedJobs = {};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether the job with the given id is currently enabled.
|
|
98
|
+
* @param {string|number} id - Job identifier
|
|
99
|
+
* @returns {boolean}
|
|
100
|
+
*/
|
|
101
|
+
isJobEnabled(id) {
|
|
102
|
+
return id in this.enabledJobs;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Whether the job with the given id is currently running.
|
|
106
|
+
* @param {string|number} id - Job identifier
|
|
107
|
+
* @returns {boolean}
|
|
108
|
+
*/
|
|
109
|
+
isJobRunning(id) {
|
|
110
|
+
return id in this.runningJobs;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Disables the job, stopping its execution loop on the next iteration.
|
|
114
|
+
* @param {string|number} id - Job identifier
|
|
115
|
+
*/
|
|
116
|
+
disableJob(id) {
|
|
117
|
+
delete this.enabledJobs[id];
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Suspends the job until the cooldown elapses, preventing it from being re-enabled.
|
|
121
|
+
* @param {string|number} id - Job identifier
|
|
122
|
+
* @param {number} durationMs - Cooldown duration in ms from now
|
|
123
|
+
*/
|
|
124
|
+
suspendJob(id, durationMs) {
|
|
125
|
+
this.suspendedJobs[id] = Date.now() + durationMs;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Instantiates (or reuses) the job, runs its init/execute/destroy lifecycle,
|
|
129
|
+
* and loops execute() with the configured delay while the job stays enabled.
|
|
130
|
+
* @private
|
|
131
|
+
* @param {BackgroundJobDefinition} definition - Definition describing the job to run
|
|
132
|
+
* @returns {Promise<void>}
|
|
133
|
+
*/
|
|
134
|
+
async _startJob(definition) {
|
|
135
|
+
definition.delayMs = definition.delayMs || 0;
|
|
136
|
+
const id = definition.id;
|
|
137
|
+
if (!this.runningJobs[id]) this.runningJobs[id] = new definition.jobClass(this, id);
|
|
138
|
+
const ref = this.runningJobs[id];
|
|
139
|
+
ref.init(definition.initParams);
|
|
140
|
+
while (this.isJobEnabled(id)) {
|
|
141
|
+
const startTime = Date.now();
|
|
142
|
+
try {
|
|
143
|
+
await ref.execute();
|
|
144
|
+
} catch (e) {
|
|
145
|
+
console.error(`[${ref.name}] error`, e);
|
|
146
|
+
}
|
|
147
|
+
if (definition.delayMs > 0) if (definition.isFixedRate) {
|
|
148
|
+
const remaining = definition.delayMs - (Date.now() - startTime);
|
|
149
|
+
if (remaining > 0) await (0, melperjs.sleepMs)(remaining);
|
|
150
|
+
} else await (0, melperjs.sleepMs)(definition.delayMs);
|
|
151
|
+
}
|
|
152
|
+
ref.destroy();
|
|
153
|
+
delete this.runningJobs[id];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Runs the main loop: periodically refreshes the executable job set, lifts
|
|
157
|
+
* expired suspensions, and starts any enabled job that is not already running.
|
|
158
|
+
* @param {number} [pollIntervalMs=1000] - Interval in ms between job status checks
|
|
159
|
+
* @returns {Promise<void>}
|
|
160
|
+
*/
|
|
161
|
+
async executeJobs(pollIntervalMs = 1e3) {
|
|
162
|
+
await (0, melperjs.forever)(pollIntervalMs, async () => {
|
|
163
|
+
const jobs = await this.getExecutableJobs();
|
|
164
|
+
this.enabledJobs = {};
|
|
165
|
+
for (const job of jobs) {
|
|
166
|
+
const id = job.id;
|
|
167
|
+
if (id in this.suspendedJobs) {
|
|
168
|
+
if (Date.now() > this.suspendedJobs[id]) {
|
|
169
|
+
delete this.suspendedJobs[id];
|
|
170
|
+
this.enabledJobs[id] = true;
|
|
171
|
+
}
|
|
172
|
+
} else this.enabledJobs[id] = true;
|
|
173
|
+
if (this.isJobEnabled(id) && !this.isJobRunning(id)) {
|
|
174
|
+
await (0, melperjs.sleepMs)((0, melperjs.randomInteger)(300, 1e3));
|
|
175
|
+
this._startJob(job).catch(console.error);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}, (e) => {
|
|
179
|
+
console.error(`[${this.name}] error`, e);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* @abstract Override in a subclass to enumerate executable jobs.
|
|
184
|
+
* @returns {Promise<BackgroundJobDefinition[]>}
|
|
185
|
+
*/
|
|
186
|
+
async getExecutableJobs() {
|
|
187
|
+
notImplemented("getExecutableJobs");
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Builds a list of identical job definitions with sequential 1-based ids.
|
|
191
|
+
* @param {number} count - Number of definitions to generate
|
|
192
|
+
* @param {typeof import("./background-job.js").BackgroundJob} jobClass - BackgroundJob subclass to use for every definition
|
|
193
|
+
* @param {{initParams?: *, delayMs?: number, isFixedRate?: boolean}} [options] - Shared options applied to every definition
|
|
194
|
+
* @returns {BackgroundJobDefinition[]}
|
|
195
|
+
*/
|
|
196
|
+
static iteratingJobList(count, jobClass, { initParams = null, delayMs = 0, isFixedRate = false } = {}) {
|
|
197
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
198
|
+
id: i + 1,
|
|
199
|
+
jobClass,
|
|
200
|
+
initParams,
|
|
201
|
+
delayMs,
|
|
202
|
+
isFixedRate
|
|
203
|
+
}));
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
//#endregion
|
|
207
|
+
exports.BackgroundJob = BackgroundJob;
|
|
208
|
+
exports.BackgroundJobRunner = BackgroundJobRunner;
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { forever, randomInteger, sleepMs } from "melperjs";
|
|
2
|
+
//#region src/utils.js
|
|
3
|
+
/**
|
|
4
|
+
* Throws to signal an abstract method that a subclass must override.
|
|
5
|
+
* @param {string} method - Name of the method that must be implemented
|
|
6
|
+
* @returns {never}
|
|
7
|
+
*/
|
|
8
|
+
function notImplemented(method) {
|
|
9
|
+
throw new Error(`${method}() must be implemented by a subclass`);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/background-job.js
|
|
13
|
+
/**
|
|
14
|
+
* Base class for an individual background job.
|
|
15
|
+
* Subclass and override {@link BackgroundJob#execute} for the work, with
|
|
16
|
+
* optional {@link BackgroundJob#init}/{@link BackgroundJob#destroy} lifecycle hooks.
|
|
17
|
+
*/
|
|
18
|
+
var BackgroundJob = class {
|
|
19
|
+
/**
|
|
20
|
+
* @param {import("./background-job-runner.js").BackgroundJobRunner} runner - Runner that owns and orchestrates this job
|
|
21
|
+
* @param {string|number} id - Unique identifier of this job within the runner
|
|
22
|
+
*/
|
|
23
|
+
constructor(runner, id) {
|
|
24
|
+
/** @type {import("./background-job-runner.js").BackgroundJobRunner} */
|
|
25
|
+
this.runner = runner;
|
|
26
|
+
/** @type {string|number} */
|
|
27
|
+
this.id = id;
|
|
28
|
+
/** @type {string} */
|
|
29
|
+
this.name = `${runner.baseName} #${id}`;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Optional setup hook, called once before the execution loop starts.
|
|
33
|
+
* @param {*} [params] - Arbitrary parameters supplied via the job definition
|
|
34
|
+
* @returns {void}
|
|
35
|
+
*/
|
|
36
|
+
init(params = null) {}
|
|
37
|
+
/**
|
|
38
|
+
* Optional teardown hook, called once after the execution loop ends.
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
destroy() {}
|
|
42
|
+
/**
|
|
43
|
+
* @abstract Override in a subclass to perform the job's work.
|
|
44
|
+
* @returns {Promise<void>}
|
|
45
|
+
*/
|
|
46
|
+
async execute() {
|
|
47
|
+
notImplemented("execute");
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether this job is currently enabled in the runner.
|
|
51
|
+
* @returns {boolean}
|
|
52
|
+
*/
|
|
53
|
+
isEnabled() {
|
|
54
|
+
return this.runner.isJobEnabled(this.id);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Disables this job and optionally suspends it for a cooldown period.
|
|
58
|
+
* @param {number} [suspendDurationMs=3000] - Cooldown in ms before the job may run again; 0 disables without suspending
|
|
59
|
+
*/
|
|
60
|
+
disable(suspendDurationMs = 3e3) {
|
|
61
|
+
this.runner.disableJob(this.id);
|
|
62
|
+
suspendDurationMs && this.runner.suspendJob(this.id, suspendDurationMs);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/background-job-runner.js
|
|
67
|
+
/**
|
|
68
|
+
* @typedef {Object} BackgroundJobDefinition
|
|
69
|
+
* @property {string|number} id - Unique job identifier
|
|
70
|
+
* @property {typeof import("./background-job.js").BackgroundJob} jobClass - BackgroundJob subclass constructor
|
|
71
|
+
* @property {*} [initParams] - Parameters passed to init()
|
|
72
|
+
* @property {number} [delayMs=0] - Delay between executions in ms
|
|
73
|
+
* @property {boolean} [isFixedRate=false] - When true, execute time counts toward the delay (fixed-rate); otherwise the full delay is waited after each execute (fixed-delay)
|
|
74
|
+
*/
|
|
75
|
+
/**
|
|
76
|
+
* Orchestrates background jobs, owning enabled/running/suspended state per job id.
|
|
77
|
+
* Subclass and override {@link BackgroundJobRunner#getExecutableJobs} to enumerate work.
|
|
78
|
+
*/
|
|
79
|
+
var BackgroundJobRunner = class {
|
|
80
|
+
/**
|
|
81
|
+
* @param {string} [name="BackgroundJob"] - Base name; jobs derive their name from it and " Runner" is appended to form {@link BackgroundJobRunner#name}
|
|
82
|
+
*/
|
|
83
|
+
constructor(name = "BackgroundJob") {
|
|
84
|
+
/** @type {string} */
|
|
85
|
+
this.baseName = name;
|
|
86
|
+
/** @type {string} */
|
|
87
|
+
this.name = `${name} Runner`;
|
|
88
|
+
/** @type {Object<string|number, boolean>} */
|
|
89
|
+
this.enabledJobs = {};
|
|
90
|
+
/** @type {Object<string|number, import("./background-job.js").BackgroundJob>} */
|
|
91
|
+
this.runningJobs = {};
|
|
92
|
+
/** @type {Object<string|number, number>} */
|
|
93
|
+
this.suspendedJobs = {};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Whether the job with the given id is currently enabled.
|
|
97
|
+
* @param {string|number} id - Job identifier
|
|
98
|
+
* @returns {boolean}
|
|
99
|
+
*/
|
|
100
|
+
isJobEnabled(id) {
|
|
101
|
+
return id in this.enabledJobs;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Whether the job with the given id is currently running.
|
|
105
|
+
* @param {string|number} id - Job identifier
|
|
106
|
+
* @returns {boolean}
|
|
107
|
+
*/
|
|
108
|
+
isJobRunning(id) {
|
|
109
|
+
return id in this.runningJobs;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Disables the job, stopping its execution loop on the next iteration.
|
|
113
|
+
* @param {string|number} id - Job identifier
|
|
114
|
+
*/
|
|
115
|
+
disableJob(id) {
|
|
116
|
+
delete this.enabledJobs[id];
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Suspends the job until the cooldown elapses, preventing it from being re-enabled.
|
|
120
|
+
* @param {string|number} id - Job identifier
|
|
121
|
+
* @param {number} durationMs - Cooldown duration in ms from now
|
|
122
|
+
*/
|
|
123
|
+
suspendJob(id, durationMs) {
|
|
124
|
+
this.suspendedJobs[id] = Date.now() + durationMs;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Instantiates (or reuses) the job, runs its init/execute/destroy lifecycle,
|
|
128
|
+
* and loops execute() with the configured delay while the job stays enabled.
|
|
129
|
+
* @private
|
|
130
|
+
* @param {BackgroundJobDefinition} definition - Definition describing the job to run
|
|
131
|
+
* @returns {Promise<void>}
|
|
132
|
+
*/
|
|
133
|
+
async _startJob(definition) {
|
|
134
|
+
definition.delayMs = definition.delayMs || 0;
|
|
135
|
+
const id = definition.id;
|
|
136
|
+
if (!this.runningJobs[id]) this.runningJobs[id] = new definition.jobClass(this, id);
|
|
137
|
+
const ref = this.runningJobs[id];
|
|
138
|
+
ref.init(definition.initParams);
|
|
139
|
+
while (this.isJobEnabled(id)) {
|
|
140
|
+
const startTime = Date.now();
|
|
141
|
+
try {
|
|
142
|
+
await ref.execute();
|
|
143
|
+
} catch (e) {
|
|
144
|
+
console.error(`[${ref.name}] error`, e);
|
|
145
|
+
}
|
|
146
|
+
if (definition.delayMs > 0) if (definition.isFixedRate) {
|
|
147
|
+
const remaining = definition.delayMs - (Date.now() - startTime);
|
|
148
|
+
if (remaining > 0) await sleepMs(remaining);
|
|
149
|
+
} else await sleepMs(definition.delayMs);
|
|
150
|
+
}
|
|
151
|
+
ref.destroy();
|
|
152
|
+
delete this.runningJobs[id];
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Runs the main loop: periodically refreshes the executable job set, lifts
|
|
156
|
+
* expired suspensions, and starts any enabled job that is not already running.
|
|
157
|
+
* @param {number} [pollIntervalMs=1000] - Interval in ms between job status checks
|
|
158
|
+
* @returns {Promise<void>}
|
|
159
|
+
*/
|
|
160
|
+
async executeJobs(pollIntervalMs = 1e3) {
|
|
161
|
+
await forever(pollIntervalMs, async () => {
|
|
162
|
+
const jobs = await this.getExecutableJobs();
|
|
163
|
+
this.enabledJobs = {};
|
|
164
|
+
for (const job of jobs) {
|
|
165
|
+
const id = job.id;
|
|
166
|
+
if (id in this.suspendedJobs) {
|
|
167
|
+
if (Date.now() > this.suspendedJobs[id]) {
|
|
168
|
+
delete this.suspendedJobs[id];
|
|
169
|
+
this.enabledJobs[id] = true;
|
|
170
|
+
}
|
|
171
|
+
} else this.enabledJobs[id] = true;
|
|
172
|
+
if (this.isJobEnabled(id) && !this.isJobRunning(id)) {
|
|
173
|
+
await sleepMs(randomInteger(300, 1e3));
|
|
174
|
+
this._startJob(job).catch(console.error);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}, (e) => {
|
|
178
|
+
console.error(`[${this.name}] error`, e);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* @abstract Override in a subclass to enumerate executable jobs.
|
|
183
|
+
* @returns {Promise<BackgroundJobDefinition[]>}
|
|
184
|
+
*/
|
|
185
|
+
async getExecutableJobs() {
|
|
186
|
+
notImplemented("getExecutableJobs");
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Builds a list of identical job definitions with sequential 1-based ids.
|
|
190
|
+
* @param {number} count - Number of definitions to generate
|
|
191
|
+
* @param {typeof import("./background-job.js").BackgroundJob} jobClass - BackgroundJob subclass to use for every definition
|
|
192
|
+
* @param {{initParams?: *, delayMs?: number, isFixedRate?: boolean}} [options] - Shared options applied to every definition
|
|
193
|
+
* @returns {BackgroundJobDefinition[]}
|
|
194
|
+
*/
|
|
195
|
+
static iteratingJobList(count, jobClass, { initParams = null, delayMs = 0, isFixedRate = false } = {}) {
|
|
196
|
+
return Array.from({ length: count }, (_, i) => ({
|
|
197
|
+
id: i + 1,
|
|
198
|
+
jobClass,
|
|
199
|
+
initParams,
|
|
200
|
+
delayMs,
|
|
201
|
+
isFixedRate
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
//#endregion
|
|
206
|
+
export { BackgroundJob, BackgroundJobRunner };
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "background-job",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Managed background jobs for Node.js — suspension, disabling, and controlled execution loops.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"background-job",
|
|
8
|
+
"background",
|
|
9
|
+
"job",
|
|
10
|
+
"jobs",
|
|
11
|
+
"worker",
|
|
12
|
+
"runner",
|
|
13
|
+
"scheduler",
|
|
14
|
+
"task",
|
|
15
|
+
"queue",
|
|
16
|
+
"esm"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://github.com/mahelbir/background-job#readme",
|
|
19
|
+
"bugs": "https://github.com/mahelbir/background-job/issues",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/mahelbir/background-job.git"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Mahmuthan Elbir <me@mahmuthanelbir.com.tr>",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"require": "./dist/index.cjs"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist/**/*"
|
|
35
|
+
],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsdown",
|
|
38
|
+
"test": "node --test 'tests/**/*.test.js'",
|
|
39
|
+
"test:watch": "node --test --watch 'tests/**/*.test.js'",
|
|
40
|
+
"prepublishOnly": "npm run build && npm test",
|
|
41
|
+
"relock": "rm -rf node_modules package-lock.json && NODE_ENV= npm install --include=dev --include=optional"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"melperjs": "^17.1.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"tsdown": "^0.22.2"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=18.0.0"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
}
|
|
55
|
+
}
|