pg-boss 12.4.0 → 12.4.1
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/dist/attorney.d.ts +19 -0
- package/dist/attorney.d.ts.map +1 -0
- package/dist/attorney.js +170 -0
- package/dist/boss.d.ts +16 -0
- package/dist/boss.d.ts.map +1 -0
- package/dist/boss.js +135 -0
- package/dist/contractor.d.ts +22 -0
- package/dist/contractor.d.ts.map +1 -0
- package/dist/contractor.js +81 -0
- package/dist/db.d.ts +16 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +46 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +233 -0
- package/dist/manager.d.ts +85 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/manager.js +481 -0
- package/dist/migrationStore.d.ts +7 -0
- package/dist/migrationStore.d.ts.map +1 -0
- package/dist/migrationStore.js +126 -0
- package/dist/plans.d.ts +78 -0
- package/dist/plans.d.ts.map +1 -0
- package/dist/plans.js +885 -0
- package/dist/timekeeper.d.ts +34 -0
- package/dist/timekeeper.d.ts.map +1 -0
- package/dist/timekeeper.js +158 -0
- package/dist/tools.d.ts +18 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +45 -0
- package/dist/types.d.ts +230 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/worker.d.ts +41 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +107 -0
- package/package.json +7 -11
- package/dist/index.d.mts +0 -285
- package/dist/index.mjs +0 -2468
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import EventEmitter from 'node:events';
|
|
2
|
+
import type Manager from './manager.ts';
|
|
3
|
+
import * as types from './types.ts';
|
|
4
|
+
export declare const QUEUES: {
|
|
5
|
+
SEND_IT: string;
|
|
6
|
+
};
|
|
7
|
+
declare class Timekeeper extends EventEmitter implements types.EventsMixin {
|
|
8
|
+
db: types.IDatabase;
|
|
9
|
+
config: types.ResolvedConstructorOptions;
|
|
10
|
+
manager: Manager;
|
|
11
|
+
private stopped;
|
|
12
|
+
private cronMonitorInterval;
|
|
13
|
+
private skewMonitorInterval;
|
|
14
|
+
private timekeeping;
|
|
15
|
+
clockSkew: number;
|
|
16
|
+
events: {
|
|
17
|
+
error: string;
|
|
18
|
+
schedule: string;
|
|
19
|
+
warning: string;
|
|
20
|
+
};
|
|
21
|
+
constructor(db: types.IDatabase, manager: Manager, config: types.ResolvedConstructorOptions);
|
|
22
|
+
start(): Promise<void>;
|
|
23
|
+
stop(): Promise<void>;
|
|
24
|
+
cacheClockSkew(): Promise<void>;
|
|
25
|
+
onCron(): Promise<void>;
|
|
26
|
+
cron(): Promise<void>;
|
|
27
|
+
shouldSendIt(cron: string, tz: string): boolean;
|
|
28
|
+
private onSendIt;
|
|
29
|
+
getSchedules(name?: string, key?: string): Promise<types.Schedule[]>;
|
|
30
|
+
schedule(name: string, cron: string, data?: unknown, options?: types.ScheduleOptions): Promise<void>;
|
|
31
|
+
unschedule(name: string, key?: string): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export default Timekeeper;
|
|
34
|
+
//# sourceMappingURL=timekeeper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timekeeper.d.ts","sourceRoot":"","sources":["../src/timekeeper.ts"],"names":[],"mappings":"AACA,OAAO,YAAY,MAAM,aAAa,CAAA;AAGtC,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AAEvC,OAAO,KAAK,KAAK,MAAM,YAAY,CAAA;AAEnC,eAAO,MAAM,MAAM;;CAElB,CAAA;AAcD,cAAM,UAAW,SAAQ,YAAa,YAAW,KAAK,CAAC,WAAW;IAChE,EAAE,EAAE,KAAK,CAAC,SAAS,CAAA;IACnB,MAAM,EAAE,KAAK,CAAC,0BAA0B,CAAA;IACxC,OAAO,EAAE,OAAO,CAAA;IAEhB,OAAO,CAAC,OAAO,CAAO;IACtB,OAAO,CAAC,mBAAmB,CAAmC;IAC9D,OAAO,CAAC,mBAAmB,CAAmC;IAC9D,OAAO,CAAC,WAAW,CAAqB;IAExC,SAAS,SAAI;IACb,MAAM;;;;MAAS;gBAEF,EAAE,EAAE,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,0BAA0B;IAQtF,KAAK;IAmBL,IAAI;IAoBJ,cAAc;IA4Bd,MAAM;IA0BN,IAAI;IAYV,YAAY,CAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM;YAYxB,QAAQ;IAIhB,YAAY,CAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,SAAK,GAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;IAclE,QAAQ,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO,GAAE,KAAK,CAAC,eAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAoBzG,UAAU,CAAE,IAAI,EAAE,MAAM,EAAE,GAAG,SAAK,GAAG,OAAO,CAAC,IAAI,CAAC;CAIzD;AAED,eAAe,UAAU,CAAA"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { CronExpressionParser } from 'cron-parser';
|
|
2
|
+
import EventEmitter from 'node:events';
|
|
3
|
+
import * as Attorney from "./attorney.js";
|
|
4
|
+
import * as plans from "./plans.js";
|
|
5
|
+
import * as types from "./types.js";
|
|
6
|
+
export const QUEUES = {
|
|
7
|
+
SEND_IT: '__pgboss__send-it'
|
|
8
|
+
};
|
|
9
|
+
const EVENTS = {
|
|
10
|
+
error: 'error',
|
|
11
|
+
schedule: 'schedule',
|
|
12
|
+
warning: 'warning'
|
|
13
|
+
};
|
|
14
|
+
const WARNINGS = {
|
|
15
|
+
CLOCK_SKEW: {
|
|
16
|
+
message: 'Warning: Clock skew between this instance and the database server. This will not break scheduling, but is emitted any time the skew exceeds 60 seconds.'
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
class Timekeeper extends EventEmitter {
|
|
20
|
+
db;
|
|
21
|
+
config;
|
|
22
|
+
manager;
|
|
23
|
+
stopped = true;
|
|
24
|
+
cronMonitorInterval;
|
|
25
|
+
skewMonitorInterval;
|
|
26
|
+
timekeeping;
|
|
27
|
+
clockSkew = 0;
|
|
28
|
+
events = EVENTS;
|
|
29
|
+
constructor(db, manager, config) {
|
|
30
|
+
super();
|
|
31
|
+
this.db = db;
|
|
32
|
+
this.config = config;
|
|
33
|
+
this.manager = manager;
|
|
34
|
+
}
|
|
35
|
+
async start() {
|
|
36
|
+
this.stopped = false;
|
|
37
|
+
await this.cacheClockSkew();
|
|
38
|
+
await this.manager.createQueue(QUEUES.SEND_IT);
|
|
39
|
+
const options = {
|
|
40
|
+
pollingIntervalSeconds: this.config.cronWorkerIntervalSeconds,
|
|
41
|
+
batchSize: 50
|
|
42
|
+
};
|
|
43
|
+
await this.manager.work(QUEUES.SEND_IT, options, (jobs) => this.onSendIt(jobs));
|
|
44
|
+
setImmediate(() => this.onCron());
|
|
45
|
+
this.cronMonitorInterval = setInterval(async () => await this.onCron(), this.config.cronMonitorIntervalSeconds * 1000);
|
|
46
|
+
this.skewMonitorInterval = setInterval(async () => await this.cacheClockSkew(), this.config.clockMonitorIntervalSeconds * 1000);
|
|
47
|
+
}
|
|
48
|
+
async stop() {
|
|
49
|
+
if (this.stopped) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
this.stopped = true;
|
|
53
|
+
await this.manager.offWork(QUEUES.SEND_IT, { wait: true });
|
|
54
|
+
if (this.skewMonitorInterval) {
|
|
55
|
+
clearInterval(this.skewMonitorInterval);
|
|
56
|
+
this.skewMonitorInterval = null;
|
|
57
|
+
}
|
|
58
|
+
if (this.cronMonitorInterval) {
|
|
59
|
+
clearInterval(this.cronMonitorInterval);
|
|
60
|
+
this.cronMonitorInterval = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async cacheClockSkew() {
|
|
64
|
+
let skew = 0;
|
|
65
|
+
try {
|
|
66
|
+
if (this.config.__test__force_clock_monitoring_error) {
|
|
67
|
+
throw new Error(this.config.__test__force_clock_monitoring_error);
|
|
68
|
+
}
|
|
69
|
+
const { rows } = await this.db.executeSql(plans.getTime());
|
|
70
|
+
const local = Date.now();
|
|
71
|
+
const dbTime = parseFloat(rows[0].time);
|
|
72
|
+
skew = dbTime - local;
|
|
73
|
+
const skewSeconds = Math.abs(skew) / 1000;
|
|
74
|
+
if (skewSeconds >= 60 || this.config.__test__force_clock_skew_warning) {
|
|
75
|
+
this.emit(this.events.warning, { message: WARNINGS.CLOCK_SKEW.message, data: { seconds: skewSeconds, direction: skew > 0 ? 'slower' : 'faster' } });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
this.emit(this.events.error, err);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
this.clockSkew = skew;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async onCron() {
|
|
86
|
+
try {
|
|
87
|
+
if (this.stopped || this.timekeeping)
|
|
88
|
+
return;
|
|
89
|
+
if (this.config.__test__force_cron_monitoring_error) {
|
|
90
|
+
throw new Error(this.config.__test__force_cron_monitoring_error);
|
|
91
|
+
}
|
|
92
|
+
this.timekeeping = true;
|
|
93
|
+
const sql = plans.trySetCronTime(this.config.schema, this.config.cronMonitorIntervalSeconds);
|
|
94
|
+
if (!this.stopped) {
|
|
95
|
+
const { rows } = await this.db.executeSql(sql);
|
|
96
|
+
if (!this.stopped && rows.length === 1) {
|
|
97
|
+
await this.cron();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
this.emit(this.events.error, err);
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
this.timekeeping = false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async cron() {
|
|
109
|
+
const schedules = await this.getSchedules();
|
|
110
|
+
const scheduled = schedules
|
|
111
|
+
.filter(i => this.shouldSendIt(i.cron, i.timezone))
|
|
112
|
+
.map(({ name, key, data, options }) => ({ data: { name, data, options }, singletonKey: `${name}__${key}`, singletonSeconds: 60 }));
|
|
113
|
+
if (scheduled.length > 0 && !this.stopped) {
|
|
114
|
+
await this.manager.insert(QUEUES.SEND_IT, scheduled);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
shouldSendIt(cron, tz) {
|
|
118
|
+
const interval = CronExpressionParser.parse(cron, { tz, strict: false });
|
|
119
|
+
const prevTime = interval.prev();
|
|
120
|
+
const databaseTime = Date.now() + this.clockSkew;
|
|
121
|
+
const prevDiff = (databaseTime - prevTime.getTime()) / 1000;
|
|
122
|
+
return prevDiff < 60;
|
|
123
|
+
}
|
|
124
|
+
async onSendIt(jobs) {
|
|
125
|
+
await Promise.allSettled(jobs.map(({ data }) => this.manager.send(data)));
|
|
126
|
+
}
|
|
127
|
+
async getSchedules(name, key = '') {
|
|
128
|
+
let sql = plans.getSchedules(this.config.schema);
|
|
129
|
+
let params = [];
|
|
130
|
+
if (name) {
|
|
131
|
+
sql = plans.getSchedulesByQueue(this.config.schema);
|
|
132
|
+
params = [name, key];
|
|
133
|
+
}
|
|
134
|
+
const { rows } = await this.db.executeSql(sql, params);
|
|
135
|
+
return rows;
|
|
136
|
+
}
|
|
137
|
+
async schedule(name, cron, data, options = {}) {
|
|
138
|
+
const { tz = 'UTC', key = '', ...rest } = options;
|
|
139
|
+
CronExpressionParser.parse(cron, { tz, strict: false });
|
|
140
|
+
Attorney.checkSendArgs([name, data, { ...rest }]);
|
|
141
|
+
Attorney.assertKey(key);
|
|
142
|
+
try {
|
|
143
|
+
const sql = plans.schedule(this.config.schema);
|
|
144
|
+
await this.db.executeSql(sql, [name, key, cron, tz, data, options]);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
if (err.message.includes('foreign key')) {
|
|
148
|
+
err.message = `Queue ${name} not found`;
|
|
149
|
+
}
|
|
150
|
+
throw err;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async unschedule(name, key = '') {
|
|
154
|
+
const sql = plans.unschedule(this.config.schema);
|
|
155
|
+
await this.db.executeSql(sql, [name, key]);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
export default Timekeeper;
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When sql contains multiple queries, result is an array of objects with rows property
|
|
3
|
+
* This function unwraps the result into a single object with rows property
|
|
4
|
+
*/
|
|
5
|
+
declare function unwrapSQLResult(result: {
|
|
6
|
+
rows: any[];
|
|
7
|
+
} | {
|
|
8
|
+
rows: any[];
|
|
9
|
+
}[]): {
|
|
10
|
+
rows: any[];
|
|
11
|
+
};
|
|
12
|
+
export interface AbortablePromise<T> extends Promise<T> {
|
|
13
|
+
abort: () => void;
|
|
14
|
+
}
|
|
15
|
+
declare function delay(ms: number, error?: string, abortController?: AbortController): AbortablePromise<void>;
|
|
16
|
+
declare function resolveWithinSeconds<T>(promise: Promise<T>, seconds: number, message?: string, abortController?: AbortController): Promise<T | void>;
|
|
17
|
+
export { delay, resolveWithinSeconds, unwrapSQLResult };
|
|
18
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAEA;;;EAGE;AACF,iBAAS,eAAe,CAAE,MAAM,EAAE;IAAE,IAAI,EAAE,GAAG,EAAE,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,GAAG,EAAE,CAAA;CAAE,EAAE,GAAG;IAAE,IAAI,EAAE,GAAG,EAAE,CAAA;CAAE,CAMtF;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IACrD,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED,iBAAS,KAAK,CAAE,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,eAAe,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAsBrG;AAED,iBAAe,oBAAoB,CAAC,CAAC,EAAG,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAapJ;AAED,OAAO,EACL,KAAK,EACL,oBAAoB,EACpB,eAAe,EAChB,CAAA"}
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { setTimeout } from 'node:timers/promises';
|
|
2
|
+
/**
|
|
3
|
+
* When sql contains multiple queries, result is an array of objects with rows property
|
|
4
|
+
* This function unwraps the result into a single object with rows property
|
|
5
|
+
*/
|
|
6
|
+
function unwrapSQLResult(result) {
|
|
7
|
+
if (Array.isArray(result)) {
|
|
8
|
+
return { rows: result.flatMap(i => i.rows) };
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
function delay(ms, error, abortController) {
|
|
13
|
+
const ac = abortController || new AbortController();
|
|
14
|
+
const promise = new Promise((resolve, reject) => {
|
|
15
|
+
setTimeout(ms, null, { signal: ac.signal })
|
|
16
|
+
.then(() => {
|
|
17
|
+
if (error) {
|
|
18
|
+
reject(new Error(error));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
resolve();
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
.catch(resolve);
|
|
25
|
+
});
|
|
26
|
+
promise.abort = () => {
|
|
27
|
+
if (!ac.signal.aborted) {
|
|
28
|
+
ac.abort();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return promise;
|
|
32
|
+
}
|
|
33
|
+
async function resolveWithinSeconds(promise, seconds, message, abortController) {
|
|
34
|
+
const timeout = Math.max(1, seconds) * 1000;
|
|
35
|
+
const reject = delay(timeout, message, abortController);
|
|
36
|
+
let result;
|
|
37
|
+
try {
|
|
38
|
+
result = await Promise.race([promise, reject]);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
reject.abort();
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
export { delay, resolveWithinSeconds, unwrapSQLResult };
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
export type JobStates = {
|
|
2
|
+
created: 'created';
|
|
3
|
+
retry: 'retry';
|
|
4
|
+
active: 'active';
|
|
5
|
+
completed: 'completed';
|
|
6
|
+
cancelled: 'cancelled';
|
|
7
|
+
failed: 'failed';
|
|
8
|
+
};
|
|
9
|
+
export type Events = {
|
|
10
|
+
error: 'error';
|
|
11
|
+
warning: 'warning';
|
|
12
|
+
wip: 'wip';
|
|
13
|
+
stopped: 'stopped';
|
|
14
|
+
};
|
|
15
|
+
export interface IDatabase {
|
|
16
|
+
executeSql(text: string, values?: unknown[]): Promise<{
|
|
17
|
+
rows: any[];
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
export interface DatabaseOptions {
|
|
21
|
+
application_name?: string;
|
|
22
|
+
database?: string;
|
|
23
|
+
user?: string;
|
|
24
|
+
password?: string | (() => string) | (() => Promise<string>);
|
|
25
|
+
host?: string;
|
|
26
|
+
port?: number;
|
|
27
|
+
schema?: string;
|
|
28
|
+
ssl?: any;
|
|
29
|
+
connectionString?: string;
|
|
30
|
+
max?: number;
|
|
31
|
+
db?: IDatabase;
|
|
32
|
+
connectionTimeoutMillis?: number;
|
|
33
|
+
}
|
|
34
|
+
export interface SchedulingOptions {
|
|
35
|
+
schedule?: boolean;
|
|
36
|
+
clockMonitorIntervalSeconds?: number;
|
|
37
|
+
cronWorkerIntervalSeconds?: number;
|
|
38
|
+
cronMonitorIntervalSeconds?: number;
|
|
39
|
+
}
|
|
40
|
+
export interface MaintenanceOptions {
|
|
41
|
+
supervise?: boolean;
|
|
42
|
+
migrate?: boolean;
|
|
43
|
+
createSchema?: boolean;
|
|
44
|
+
warningSlowQuerySeconds?: number;
|
|
45
|
+
warningQueueSize?: number;
|
|
46
|
+
superviseIntervalSeconds?: number;
|
|
47
|
+
maintenanceIntervalSeconds?: number;
|
|
48
|
+
queueCacheIntervalSeconds?: number;
|
|
49
|
+
monitorIntervalSeconds?: number;
|
|
50
|
+
}
|
|
51
|
+
export interface Migration {
|
|
52
|
+
release: string;
|
|
53
|
+
version: number;
|
|
54
|
+
previous: number;
|
|
55
|
+
install: string[];
|
|
56
|
+
uninstall: string[];
|
|
57
|
+
}
|
|
58
|
+
export interface ConstructorOptions extends DatabaseOptions, SchedulingOptions, MaintenanceOptions {
|
|
59
|
+
}
|
|
60
|
+
export interface ResolvedConstructorOptions extends ConstructorOptions {
|
|
61
|
+
schema: string;
|
|
62
|
+
monitorIntervalSeconds: number;
|
|
63
|
+
cronMonitorIntervalSeconds: number;
|
|
64
|
+
maintenanceIntervalSeconds: number;
|
|
65
|
+
}
|
|
66
|
+
export interface QueueOptions {
|
|
67
|
+
expireInSeconds?: number;
|
|
68
|
+
retentionSeconds?: number;
|
|
69
|
+
deleteAfterSeconds?: number;
|
|
70
|
+
retryLimit?: number;
|
|
71
|
+
retryDelay?: number;
|
|
72
|
+
retryBackoff?: boolean;
|
|
73
|
+
retryDelayMax?: number;
|
|
74
|
+
}
|
|
75
|
+
export interface JobOptions {
|
|
76
|
+
id?: string;
|
|
77
|
+
priority?: number;
|
|
78
|
+
startAfter?: number | string | Date;
|
|
79
|
+
singletonKey?: string;
|
|
80
|
+
singletonSeconds?: number;
|
|
81
|
+
singletonNextSlot?: boolean;
|
|
82
|
+
keepUntil?: number | string | Date;
|
|
83
|
+
}
|
|
84
|
+
export interface ConnectionOptions {
|
|
85
|
+
db?: IDatabase;
|
|
86
|
+
}
|
|
87
|
+
export type InsertOptions = ConnectionOptions;
|
|
88
|
+
export type SendOptions = JobOptions & QueueOptions & ConnectionOptions;
|
|
89
|
+
export type QueuePolicy = 'standard' | 'short' | 'singleton' | 'stately' | 'exclusive';
|
|
90
|
+
export interface Queue extends QueueOptions {
|
|
91
|
+
name: string;
|
|
92
|
+
policy?: QueuePolicy;
|
|
93
|
+
partition?: boolean;
|
|
94
|
+
deadLetter?: string;
|
|
95
|
+
warningQueueSize?: number;
|
|
96
|
+
}
|
|
97
|
+
export interface QueueResult extends Queue {
|
|
98
|
+
deferredCount: number;
|
|
99
|
+
queuedCount: number;
|
|
100
|
+
activeCount: number;
|
|
101
|
+
totalCount: number;
|
|
102
|
+
table: string;
|
|
103
|
+
createdOn: Date;
|
|
104
|
+
updatedOn: Date;
|
|
105
|
+
singletonsActive: string[] | null;
|
|
106
|
+
}
|
|
107
|
+
export type ScheduleOptions = SendOptions & {
|
|
108
|
+
tz?: string;
|
|
109
|
+
key?: string;
|
|
110
|
+
};
|
|
111
|
+
export interface JobPollingOptions {
|
|
112
|
+
pollingIntervalSeconds?: number;
|
|
113
|
+
}
|
|
114
|
+
export interface JobFetchOptions {
|
|
115
|
+
includeMetadata?: boolean;
|
|
116
|
+
priority?: boolean;
|
|
117
|
+
batchSize?: number;
|
|
118
|
+
ignoreStartAfter?: boolean;
|
|
119
|
+
}
|
|
120
|
+
export type WorkOptions = JobFetchOptions & JobPollingOptions;
|
|
121
|
+
export type FetchOptions = JobFetchOptions & ConnectionOptions;
|
|
122
|
+
export interface ResolvedWorkOptions extends WorkOptions {
|
|
123
|
+
pollingInterval: number;
|
|
124
|
+
}
|
|
125
|
+
export interface WorkHandler<ReqData, ResData = any> {
|
|
126
|
+
(job: Job<ReqData>[]): Promise<ResData>;
|
|
127
|
+
}
|
|
128
|
+
export interface WorkWithMetadataHandler<ReqData, ResData = any> {
|
|
129
|
+
(job: JobWithMetadata<ReqData>[]): Promise<ResData>;
|
|
130
|
+
}
|
|
131
|
+
export interface Request {
|
|
132
|
+
name: string;
|
|
133
|
+
data?: object;
|
|
134
|
+
options?: SendOptions;
|
|
135
|
+
}
|
|
136
|
+
export interface Schedule {
|
|
137
|
+
name: string;
|
|
138
|
+
key: string;
|
|
139
|
+
cron: string;
|
|
140
|
+
timezone: string;
|
|
141
|
+
data?: object;
|
|
142
|
+
options?: SendOptions;
|
|
143
|
+
}
|
|
144
|
+
export interface Job<T = object> {
|
|
145
|
+
id: string;
|
|
146
|
+
name: string;
|
|
147
|
+
data: T;
|
|
148
|
+
expireInSeconds: number;
|
|
149
|
+
signal: AbortSignal;
|
|
150
|
+
}
|
|
151
|
+
export interface JobWithMetadata<T = object> extends Job<T> {
|
|
152
|
+
priority: number;
|
|
153
|
+
state: 'created' | 'retry' | 'active' | 'completed' | 'cancelled' | 'failed';
|
|
154
|
+
retryLimit: number;
|
|
155
|
+
retryCount: number;
|
|
156
|
+
retryDelay: number;
|
|
157
|
+
retryBackoff: boolean;
|
|
158
|
+
retryDelayMax?: number;
|
|
159
|
+
startAfter: Date;
|
|
160
|
+
startedOn: Date;
|
|
161
|
+
singletonKey: string | null;
|
|
162
|
+
singletonOn: Date | null;
|
|
163
|
+
expireInSeconds: number;
|
|
164
|
+
deleteAfterSeconds: number;
|
|
165
|
+
createdOn: Date;
|
|
166
|
+
completedOn: Date | null;
|
|
167
|
+
keepUntil: Date;
|
|
168
|
+
policy: QueuePolicy;
|
|
169
|
+
deadLetter: string;
|
|
170
|
+
output: object;
|
|
171
|
+
}
|
|
172
|
+
export interface JobInsert<T = object> {
|
|
173
|
+
id?: string;
|
|
174
|
+
data?: T;
|
|
175
|
+
priority?: number;
|
|
176
|
+
retryLimit?: number;
|
|
177
|
+
retryDelay?: number;
|
|
178
|
+
retryBackoff?: boolean;
|
|
179
|
+
retryDelayMax?: number;
|
|
180
|
+
startAfter?: number | string | Date;
|
|
181
|
+
singletonKey?: string;
|
|
182
|
+
singletonSeconds?: number;
|
|
183
|
+
expireInSeconds?: number;
|
|
184
|
+
deleteAfterSeconds?: number;
|
|
185
|
+
retentionSeconds?: number;
|
|
186
|
+
}
|
|
187
|
+
export type WorkerState = 'created' | 'active' | 'stopping' | 'stopped';
|
|
188
|
+
export interface WipData {
|
|
189
|
+
id: string;
|
|
190
|
+
name: string;
|
|
191
|
+
options: WorkOptions;
|
|
192
|
+
state: WorkerState;
|
|
193
|
+
count: number;
|
|
194
|
+
createdOn: number;
|
|
195
|
+
lastFetchedOn: number | null;
|
|
196
|
+
lastJobStartedOn: number | null;
|
|
197
|
+
lastJobEndedOn: number | null;
|
|
198
|
+
lastJobDuration: number | null;
|
|
199
|
+
lastError: object | null;
|
|
200
|
+
lastErrorOn: number | null;
|
|
201
|
+
}
|
|
202
|
+
export interface StopOptions {
|
|
203
|
+
close?: boolean;
|
|
204
|
+
graceful?: boolean;
|
|
205
|
+
timeout?: number;
|
|
206
|
+
}
|
|
207
|
+
export interface OffWorkOptions {
|
|
208
|
+
id?: string;
|
|
209
|
+
wait?: boolean;
|
|
210
|
+
}
|
|
211
|
+
export interface EventsMixin extends NodeJS.EventEmitter {
|
|
212
|
+
events: Record<string, string>;
|
|
213
|
+
}
|
|
214
|
+
export interface FunctionsMixin {
|
|
215
|
+
functions: Function[];
|
|
216
|
+
}
|
|
217
|
+
export type UpdateQueueOptions = Omit<Queue, 'name' | 'partition' | 'policy'>;
|
|
218
|
+
export interface Warning {
|
|
219
|
+
message: string;
|
|
220
|
+
data: object;
|
|
221
|
+
}
|
|
222
|
+
export interface CommandResponse {
|
|
223
|
+
}
|
|
224
|
+
export type PgBossEventMap = {
|
|
225
|
+
error: [error: Error];
|
|
226
|
+
warning: [warning: Warning];
|
|
227
|
+
wip: [data: WipData[]];
|
|
228
|
+
stopped: [];
|
|
229
|
+
};
|
|
230
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[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,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE;AAE1D,MAAM,WAAW,eAAe;CAO/B;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;IACrB,OAAO,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IAC3B,GAAG,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,CAAC,CAAA;IACtB,OAAO,EAAE,EAAE,CAAA;CACZ,CAAA"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type * as types from './types.ts';
|
|
2
|
+
interface WorkerOptions<T> {
|
|
3
|
+
id: string;
|
|
4
|
+
name: string;
|
|
5
|
+
options: types.WorkOptions;
|
|
6
|
+
interval: number;
|
|
7
|
+
fetch: () => Promise<types.Job<T>[]>;
|
|
8
|
+
onFetch: (jobs: types.Job<T>[]) => Promise<void>;
|
|
9
|
+
onError: (err: any) => void;
|
|
10
|
+
}
|
|
11
|
+
declare class Worker<T = unknown> {
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly name: string;
|
|
14
|
+
readonly options: types.WorkOptions;
|
|
15
|
+
readonly fetch: () => Promise<types.Job<T>[]>;
|
|
16
|
+
readonly onFetch: (jobs: types.Job<T>[]) => Promise<void>;
|
|
17
|
+
readonly onError: (err: any) => void;
|
|
18
|
+
readonly interval: number;
|
|
19
|
+
jobs: types.Job<T>[];
|
|
20
|
+
createdOn: number;
|
|
21
|
+
state: types.WorkerState;
|
|
22
|
+
lastFetchedOn: number | null;
|
|
23
|
+
lastJobStartedOn: number | null;
|
|
24
|
+
lastJobEndedOn: number | null;
|
|
25
|
+
lastJobDuration: number | null;
|
|
26
|
+
lastError: any;
|
|
27
|
+
lastErrorOn: number | null;
|
|
28
|
+
stopping: boolean;
|
|
29
|
+
stopped: boolean;
|
|
30
|
+
private loopDelayPromise;
|
|
31
|
+
private beenNotified;
|
|
32
|
+
private runPromise;
|
|
33
|
+
constructor({ id, name, options, interval, fetch, onFetch, onError }: WorkerOptions<T>);
|
|
34
|
+
start(): void;
|
|
35
|
+
private run;
|
|
36
|
+
notify(): void;
|
|
37
|
+
stop(): Promise<void>;
|
|
38
|
+
toWipData(): types.WipData;
|
|
39
|
+
}
|
|
40
|
+
export default Worker;
|
|
41
|
+
//# sourceMappingURL=worker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,KAAK,MAAM,YAAY,CAAA;AASxC,UAAU,aAAa,CAAC,CAAC;IACvB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,KAAK,CAAC,WAAW,CAAA;IAC1B,QAAQ,EAAE,MAAM,CAAA;IAChB,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IACpC,OAAO,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAChD,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAA;CAC5B;AAED,cAAM,MAAM,CAAC,CAAC,GAAG,OAAO;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,KAAK,CAAC,WAAW,CAAA;IACnC,QAAQ,CAAC,KAAK,EAAE,MAAM,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IAC7C,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IACzD,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,IAAI,CAAA;IACpC,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAA;IAEzB,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAK;IACzB,SAAS,SAAa;IACtB,KAAK,EAAE,KAAK,CAAC,WAAW,CAAwB;IAChD,aAAa,EAAE,MAAM,GAAG,IAAI,CAAO;IACnC,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAO;IACtC,cAAc,EAAE,MAAM,GAAG,IAAI,CAAO;IACpC,eAAe,EAAE,MAAM,GAAG,IAAI,CAAO;IACrC,SAAS,EAAE,GAAG,CAAO;IACrB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAO;IACjC,QAAQ,UAAQ;IAChB,OAAO,UAAQ;IACf,OAAO,CAAC,gBAAgB,CAAsC;IAC9D,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,UAAU,CAA6B;gBAElC,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,aAAa,CAAC,CAAC,CAAC;IAUvF,KAAK;YAIS,GAAG;IAgDjB,MAAM;IAQA,IAAI,IAAK,OAAO,CAAC,IAAI,CAAC;IAW5B,SAAS,IAAK,KAAK,CAAC,OAAO;CAgB5B;AAED,eAAe,MAAM,CAAA"}
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { delay } from "./tools.js";
|
|
2
|
+
const WORKER_STATES = {
|
|
3
|
+
created: 'created',
|
|
4
|
+
active: 'active',
|
|
5
|
+
stopping: 'stopping',
|
|
6
|
+
stopped: 'stopped'
|
|
7
|
+
};
|
|
8
|
+
class Worker {
|
|
9
|
+
id;
|
|
10
|
+
name;
|
|
11
|
+
options;
|
|
12
|
+
fetch;
|
|
13
|
+
onFetch;
|
|
14
|
+
onError;
|
|
15
|
+
interval;
|
|
16
|
+
jobs = [];
|
|
17
|
+
createdOn = Date.now();
|
|
18
|
+
state = WORKER_STATES.created;
|
|
19
|
+
lastFetchedOn = null;
|
|
20
|
+
lastJobStartedOn = null;
|
|
21
|
+
lastJobEndedOn = null;
|
|
22
|
+
lastJobDuration = null;
|
|
23
|
+
lastError = null;
|
|
24
|
+
lastErrorOn = null;
|
|
25
|
+
stopping = false;
|
|
26
|
+
stopped = false;
|
|
27
|
+
loopDelayPromise = null;
|
|
28
|
+
beenNotified = false;
|
|
29
|
+
runPromise = null;
|
|
30
|
+
constructor({ id, name, options, interval, fetch, onFetch, onError }) {
|
|
31
|
+
this.id = id;
|
|
32
|
+
this.name = name;
|
|
33
|
+
this.options = options;
|
|
34
|
+
this.fetch = fetch;
|
|
35
|
+
this.onFetch = onFetch;
|
|
36
|
+
this.onError = onError;
|
|
37
|
+
this.interval = interval;
|
|
38
|
+
}
|
|
39
|
+
start() {
|
|
40
|
+
this.runPromise = this.run();
|
|
41
|
+
}
|
|
42
|
+
async run() {
|
|
43
|
+
this.state = WORKER_STATES.active;
|
|
44
|
+
while (!this.stopping) {
|
|
45
|
+
const started = Date.now();
|
|
46
|
+
try {
|
|
47
|
+
this.beenNotified = false;
|
|
48
|
+
const jobs = await this.fetch();
|
|
49
|
+
this.lastFetchedOn = Date.now();
|
|
50
|
+
if (jobs) {
|
|
51
|
+
this.jobs = jobs;
|
|
52
|
+
this.lastJobStartedOn = this.lastFetchedOn;
|
|
53
|
+
await this.onFetch(jobs);
|
|
54
|
+
this.lastJobEndedOn = Date.now();
|
|
55
|
+
this.jobs = [];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (err) {
|
|
59
|
+
this.lastErrorOn = Date.now();
|
|
60
|
+
this.lastError = err;
|
|
61
|
+
err.message = `${err.message} (Queue: ${this.name}, Worker: ${this.id})`;
|
|
62
|
+
this.onError(err);
|
|
63
|
+
}
|
|
64
|
+
const duration = Date.now() - started;
|
|
65
|
+
this.lastJobDuration = duration;
|
|
66
|
+
if (!this.stopping && !this.beenNotified && (this.interval - duration) > 100) {
|
|
67
|
+
this.loopDelayPromise = delay(this.interval - duration);
|
|
68
|
+
await this.loopDelayPromise;
|
|
69
|
+
this.loopDelayPromise = null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
this.stopping = false;
|
|
73
|
+
this.stopped = true;
|
|
74
|
+
this.state = WORKER_STATES.stopped;
|
|
75
|
+
}
|
|
76
|
+
notify() {
|
|
77
|
+
this.beenNotified = true;
|
|
78
|
+
if (this.loopDelayPromise) {
|
|
79
|
+
this.loopDelayPromise.abort();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async stop() {
|
|
83
|
+
this.stopping = true;
|
|
84
|
+
this.state = WORKER_STATES.stopping;
|
|
85
|
+
if (this.loopDelayPromise) {
|
|
86
|
+
this.loopDelayPromise.abort();
|
|
87
|
+
}
|
|
88
|
+
await this.runPromise;
|
|
89
|
+
}
|
|
90
|
+
toWipData() {
|
|
91
|
+
return {
|
|
92
|
+
id: this.id,
|
|
93
|
+
name: this.name,
|
|
94
|
+
options: this.options,
|
|
95
|
+
state: this.state,
|
|
96
|
+
count: this.jobs.length,
|
|
97
|
+
createdOn: this.createdOn,
|
|
98
|
+
lastFetchedOn: this.lastFetchedOn,
|
|
99
|
+
lastJobStartedOn: this.lastJobStartedOn,
|
|
100
|
+
lastJobEndedOn: this.lastJobEndedOn,
|
|
101
|
+
lastError: this.lastError,
|
|
102
|
+
lastErrorOn: this.lastErrorOn,
|
|
103
|
+
lastJobDuration: this.lastJobDuration
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export default Worker;
|