@porulle/jobs-pg-boss 0.9.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/README.md +14 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +147 -0
- package/package.json +53 -0
- package/src/index.ts +243 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 unified-commerce-engine contributors
|
|
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/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# `@porulle/jobs-pg-boss`
|
|
2
|
+
|
|
3
|
+
Postgres-native Porulle execution engine backed by pg-boss. It maps task retries, delayed enqueue, keyed exclusivity, and pending-job supersession onto pg-boss queue policies and workers.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { PgBossExecutionEngine } from "@porulle/jobs-pg-boss";
|
|
7
|
+
|
|
8
|
+
const jobs = new PgBossExecutionEngine({
|
|
9
|
+
connectionString: process.env.DATABASE_URL!,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export default defineConfig({ jobs: { adapter: jobs } });
|
|
13
|
+
await jobs.start();
|
|
14
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { EnqueueOptions, ExecutionEngine, ExecutionEngineSetup } from "@porulle/core";
|
|
2
|
+
interface PgBossJobData {
|
|
3
|
+
taskSlug: string;
|
|
4
|
+
input: Record<string, unknown>;
|
|
5
|
+
organizationId: string;
|
|
6
|
+
queue: string;
|
|
7
|
+
maxAttempts: number;
|
|
8
|
+
concurrencyKey?: string;
|
|
9
|
+
}
|
|
10
|
+
interface PgBossJob {
|
|
11
|
+
id: string;
|
|
12
|
+
data: PgBossJobData;
|
|
13
|
+
retryCount: number;
|
|
14
|
+
retryLimit: number;
|
|
15
|
+
}
|
|
16
|
+
interface PgBossClient {
|
|
17
|
+
start(): Promise<unknown>;
|
|
18
|
+
stop(options?: {
|
|
19
|
+
graceful?: boolean;
|
|
20
|
+
timeout?: number;
|
|
21
|
+
}): Promise<unknown>;
|
|
22
|
+
createQueue(name: string, options: {
|
|
23
|
+
policy: "singleton";
|
|
24
|
+
}): Promise<unknown>;
|
|
25
|
+
send(name: string, data: PgBossJobData, options: Record<string, unknown>): Promise<string | null>;
|
|
26
|
+
upsert(name: string, data: PgBossJobData, options: Record<string, unknown>): Promise<{
|
|
27
|
+
jobs: string[];
|
|
28
|
+
}>;
|
|
29
|
+
work(name: string, options: {
|
|
30
|
+
includeMetadata: true;
|
|
31
|
+
localConcurrency: number;
|
|
32
|
+
}, handler: (jobs: PgBossJob[]) => Promise<unknown>): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
export interface PgBossExecutionEngineOptions {
|
|
35
|
+
connectionString?: string;
|
|
36
|
+
client?: PgBossClient;
|
|
37
|
+
queuePrefix?: string;
|
|
38
|
+
localConcurrency?: number;
|
|
39
|
+
}
|
|
40
|
+
export declare class PgBossExecutionEngine implements ExecutionEngine {
|
|
41
|
+
readonly execution: {
|
|
42
|
+
mode: "push";
|
|
43
|
+
};
|
|
44
|
+
private readonly boss;
|
|
45
|
+
private readonly queueName;
|
|
46
|
+
private readonly localConcurrency;
|
|
47
|
+
private worker;
|
|
48
|
+
private started;
|
|
49
|
+
private setup;
|
|
50
|
+
constructor(options: PgBossExecutionEngineOptions);
|
|
51
|
+
register(setup: ExecutionEngineSetup): void;
|
|
52
|
+
start(): Promise<void>;
|
|
53
|
+
stop(): Promise<void>;
|
|
54
|
+
enqueue(taskSlug: string, input: Record<string, unknown>, options: EnqueueOptions): Promise<string>;
|
|
55
|
+
private requireSetup;
|
|
56
|
+
private ensureStarted;
|
|
57
|
+
private ensureWorker;
|
|
58
|
+
private startWorker;
|
|
59
|
+
private runJob;
|
|
60
|
+
private translateSendOptions;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { PgBoss } from "pg-boss";
|
|
3
|
+
export class PgBossExecutionEngine {
|
|
4
|
+
execution = { mode: "push" };
|
|
5
|
+
boss;
|
|
6
|
+
queueName;
|
|
7
|
+
localConcurrency;
|
|
8
|
+
worker;
|
|
9
|
+
started;
|
|
10
|
+
setup;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
if (!options.client && !options.connectionString) {
|
|
13
|
+
throw new Error("PgBossExecutionEngine requires connectionString or an injected client.");
|
|
14
|
+
}
|
|
15
|
+
if (options.client) {
|
|
16
|
+
this.boss = options.client;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
const boss = new PgBoss(options.connectionString);
|
|
20
|
+
this.boss = {
|
|
21
|
+
start: () => boss.start(),
|
|
22
|
+
stop: (stopOptions) => boss.stop(stopOptions),
|
|
23
|
+
createQueue: (name, queueOptions) => boss.createQueue(name, queueOptions),
|
|
24
|
+
send: (name, data, sendOptions) => boss.send(name, data, sendOptions),
|
|
25
|
+
upsert: (name, data, sendOptions) => boss.upsert(name, data, sendOptions),
|
|
26
|
+
work: (name, workOptions, handler) => boss.work(name, workOptions, handler),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
this.queueName = `${options.queuePrefix ?? "porulle"}-jobs`;
|
|
30
|
+
this.localConcurrency = options.localConcurrency ?? 10;
|
|
31
|
+
}
|
|
32
|
+
register(setup) {
|
|
33
|
+
this.setup = setup;
|
|
34
|
+
}
|
|
35
|
+
async start() {
|
|
36
|
+
await this.ensureWorker();
|
|
37
|
+
}
|
|
38
|
+
async stop() {
|
|
39
|
+
if (!this.started)
|
|
40
|
+
return;
|
|
41
|
+
await this.boss.stop({ graceful: true });
|
|
42
|
+
this.started = undefined;
|
|
43
|
+
this.worker = undefined;
|
|
44
|
+
}
|
|
45
|
+
async enqueue(taskSlug, input, options) {
|
|
46
|
+
const setup = this.requireSetup();
|
|
47
|
+
const task = setup.tasks.get(taskSlug);
|
|
48
|
+
if (!task) {
|
|
49
|
+
throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
50
|
+
}
|
|
51
|
+
const organizationId = options.organizationId.trim();
|
|
52
|
+
if (!organizationId) {
|
|
53
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
54
|
+
}
|
|
55
|
+
await this.ensureWorker();
|
|
56
|
+
const concurrencyKey = options.concurrencyKey ?? task.concurrency?.key(input);
|
|
57
|
+
const exclusive = Boolean(task.concurrency && task.concurrency.exclusive !== false);
|
|
58
|
+
const supersedes = options.supersedes ?? task.concurrency?.supersedes;
|
|
59
|
+
const singletonKey = (exclusive || supersedes) && concurrencyKey
|
|
60
|
+
? concurrencyKey
|
|
61
|
+
: randomUUID();
|
|
62
|
+
const maxAttempts = options.maxAttempts ?? task.retries?.attempts ?? 1;
|
|
63
|
+
const data = {
|
|
64
|
+
taskSlug,
|
|
65
|
+
input,
|
|
66
|
+
organizationId,
|
|
67
|
+
queue: options.queue ?? "default",
|
|
68
|
+
maxAttempts,
|
|
69
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
70
|
+
};
|
|
71
|
+
const sendOptions = this.translateSendOptions(task, options, maxAttempts, singletonKey);
|
|
72
|
+
if (supersedes && concurrencyKey) {
|
|
73
|
+
const result = await this.boss.upsert(this.queueName, data, sendOptions);
|
|
74
|
+
const id = result.jobs[0];
|
|
75
|
+
if (!id)
|
|
76
|
+
throw new Error("pg-boss did not return an upserted job id.");
|
|
77
|
+
return id;
|
|
78
|
+
}
|
|
79
|
+
const id = randomUUID();
|
|
80
|
+
const created = await this.boss.send(this.queueName, data, {
|
|
81
|
+
...sendOptions,
|
|
82
|
+
id,
|
|
83
|
+
});
|
|
84
|
+
if (!created)
|
|
85
|
+
throw new Error("pg-boss did not create the job.");
|
|
86
|
+
return created;
|
|
87
|
+
}
|
|
88
|
+
requireSetup() {
|
|
89
|
+
if (!this.setup) {
|
|
90
|
+
throw new Error("PgBossExecutionEngine must be registered before use.");
|
|
91
|
+
}
|
|
92
|
+
return this.setup;
|
|
93
|
+
}
|
|
94
|
+
ensureStarted() {
|
|
95
|
+
this.started ??= this.boss
|
|
96
|
+
.start()
|
|
97
|
+
.then(() => undefined)
|
|
98
|
+
.catch((error) => {
|
|
99
|
+
this.started = undefined;
|
|
100
|
+
throw error;
|
|
101
|
+
});
|
|
102
|
+
return this.started;
|
|
103
|
+
}
|
|
104
|
+
ensureWorker() {
|
|
105
|
+
this.worker ??= this.startWorker().catch((error) => {
|
|
106
|
+
this.worker = undefined;
|
|
107
|
+
throw error;
|
|
108
|
+
});
|
|
109
|
+
return this.worker;
|
|
110
|
+
}
|
|
111
|
+
async startWorker() {
|
|
112
|
+
await this.ensureStarted();
|
|
113
|
+
await this.boss.createQueue(this.queueName, { policy: "singleton" });
|
|
114
|
+
await this.boss.work(this.queueName, { includeMetadata: true, localConcurrency: this.localConcurrency }, async (jobs) => Promise.all(jobs.map((job) => this.runJob(job))));
|
|
115
|
+
}
|
|
116
|
+
async runJob(job) {
|
|
117
|
+
const setup = this.requireSetup();
|
|
118
|
+
const task = setup.tasks.get(job.data.taskSlug);
|
|
119
|
+
if (!task)
|
|
120
|
+
throw new Error(`Unknown task slug: ${job.data.taskSlug}`);
|
|
121
|
+
const result = await task.handler({
|
|
122
|
+
input: job.data.input,
|
|
123
|
+
ctx: setup.context,
|
|
124
|
+
job: {
|
|
125
|
+
attemptNumber: job.retryCount + 1,
|
|
126
|
+
maxAttempts: job.retryLimit + 1,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
return result.output;
|
|
130
|
+
}
|
|
131
|
+
translateSendOptions(task, options, maxAttempts, singletonKey) {
|
|
132
|
+
const backoff = task.retries?.backoff;
|
|
133
|
+
return {
|
|
134
|
+
singletonKey,
|
|
135
|
+
retryLimit: Math.max(maxAttempts - 1, 0),
|
|
136
|
+
...(backoff
|
|
137
|
+
? {
|
|
138
|
+
retryDelay: Math.max(1, Math.ceil(backoff.delay / 1000)),
|
|
139
|
+
retryBackoff: backoff.type === "exponential",
|
|
140
|
+
}
|
|
141
|
+
: {}),
|
|
142
|
+
...(options.delayMs !== undefined
|
|
143
|
+
? { startAfter: new Date(Date.now() + options.delayMs) }
|
|
144
|
+
: {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@porulle/jobs-pg-boss",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"bun": "./src/index.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./src/index.ts"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"pg-boss": "^12.26.1",
|
|
15
|
+
"@porulle/core": "0.9.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^24.5.2",
|
|
19
|
+
"eslint": "^9.39.1",
|
|
20
|
+
"typescript": "5.9.2",
|
|
21
|
+
"vitest": "^3.2.4",
|
|
22
|
+
"@porulle/eslint-config": "0.1.0",
|
|
23
|
+
"@porulle/typescript-config": "0.1.0"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22.12.0"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src",
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md"
|
|
35
|
+
],
|
|
36
|
+
"description": "pg-boss execution engine for Porulle background jobs.",
|
|
37
|
+
"homepage": "https://porulle-docs.vercel.app",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/asyncdotengineering/porulle/issues"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/asyncdotengineering/porulle.git",
|
|
44
|
+
"directory": "packages/jobs-pg-boss"
|
|
45
|
+
},
|
|
46
|
+
"author": "Porulle contributors",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "rm -rf dist tsconfig.build.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
49
|
+
"check-types": "tsc --noEmit",
|
|
50
|
+
"lint": "eslint . --max-warnings 1000",
|
|
51
|
+
"test": "vitest run"
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { PgBoss } from "pg-boss";
|
|
3
|
+
import type {
|
|
4
|
+
EnqueueOptions,
|
|
5
|
+
ExecutionEngine,
|
|
6
|
+
ExecutionEngineSetup,
|
|
7
|
+
TaskDefinition,
|
|
8
|
+
} from "@porulle/core";
|
|
9
|
+
|
|
10
|
+
interface PgBossJobData {
|
|
11
|
+
taskSlug: string;
|
|
12
|
+
input: Record<string, unknown>;
|
|
13
|
+
organizationId: string;
|
|
14
|
+
queue: string;
|
|
15
|
+
maxAttempts: number;
|
|
16
|
+
concurrencyKey?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PgBossJob {
|
|
20
|
+
id: string;
|
|
21
|
+
data: PgBossJobData;
|
|
22
|
+
retryCount: number;
|
|
23
|
+
retryLimit: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface PgBossClient {
|
|
27
|
+
start(): Promise<unknown>;
|
|
28
|
+
stop(options?: { graceful?: boolean; timeout?: number }): Promise<unknown>;
|
|
29
|
+
createQueue(name: string, options: { policy: "singleton" }): Promise<unknown>;
|
|
30
|
+
send(
|
|
31
|
+
name: string,
|
|
32
|
+
data: PgBossJobData,
|
|
33
|
+
options: Record<string, unknown>,
|
|
34
|
+
): Promise<string | null>;
|
|
35
|
+
upsert(
|
|
36
|
+
name: string,
|
|
37
|
+
data: PgBossJobData,
|
|
38
|
+
options: Record<string, unknown>,
|
|
39
|
+
): Promise<{ jobs: string[] }>;
|
|
40
|
+
work(
|
|
41
|
+
name: string,
|
|
42
|
+
options: {
|
|
43
|
+
includeMetadata: true;
|
|
44
|
+
localConcurrency: number;
|
|
45
|
+
},
|
|
46
|
+
handler: (jobs: PgBossJob[]) => Promise<unknown>,
|
|
47
|
+
): Promise<string>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface PgBossExecutionEngineOptions {
|
|
51
|
+
connectionString?: string;
|
|
52
|
+
client?: PgBossClient;
|
|
53
|
+
queuePrefix?: string;
|
|
54
|
+
localConcurrency?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export class PgBossExecutionEngine implements ExecutionEngine {
|
|
58
|
+
readonly execution = { mode: "push" as const };
|
|
59
|
+
|
|
60
|
+
private readonly boss: PgBossClient;
|
|
61
|
+
private readonly queueName: string;
|
|
62
|
+
private readonly localConcurrency: number;
|
|
63
|
+
private worker: Promise<void> | undefined;
|
|
64
|
+
private started: Promise<void> | undefined;
|
|
65
|
+
private setup: ExecutionEngineSetup | undefined;
|
|
66
|
+
|
|
67
|
+
constructor(options: PgBossExecutionEngineOptions) {
|
|
68
|
+
if (!options.client && !options.connectionString) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
"PgBossExecutionEngine requires connectionString or an injected client.",
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
if (options.client) {
|
|
74
|
+
this.boss = options.client;
|
|
75
|
+
} else {
|
|
76
|
+
const boss = new PgBoss(options.connectionString!);
|
|
77
|
+
this.boss = {
|
|
78
|
+
start: () => boss.start(),
|
|
79
|
+
stop: (stopOptions) => boss.stop(stopOptions),
|
|
80
|
+
createQueue: (name, queueOptions) =>
|
|
81
|
+
boss.createQueue(name, queueOptions),
|
|
82
|
+
send: (name, data, sendOptions) => boss.send(name, data, sendOptions),
|
|
83
|
+
upsert: (name, data, sendOptions) =>
|
|
84
|
+
boss.upsert(name, data, sendOptions),
|
|
85
|
+
work: (name, workOptions, handler) =>
|
|
86
|
+
boss.work<PgBossJobData, unknown, typeof workOptions>(
|
|
87
|
+
name,
|
|
88
|
+
workOptions,
|
|
89
|
+
handler,
|
|
90
|
+
),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
this.queueName = `${options.queuePrefix ?? "porulle"}-jobs`;
|
|
94
|
+
this.localConcurrency = options.localConcurrency ?? 10;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
register(setup: ExecutionEngineSetup): void {
|
|
98
|
+
this.setup = setup;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async start(): Promise<void> {
|
|
102
|
+
await this.ensureWorker();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async stop(): Promise<void> {
|
|
106
|
+
if (!this.started) return;
|
|
107
|
+
await this.boss.stop({ graceful: true });
|
|
108
|
+
this.started = undefined;
|
|
109
|
+
this.worker = undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async enqueue(
|
|
113
|
+
taskSlug: string,
|
|
114
|
+
input: Record<string, unknown>,
|
|
115
|
+
options: EnqueueOptions,
|
|
116
|
+
): Promise<string> {
|
|
117
|
+
const setup = this.requireSetup();
|
|
118
|
+
const task = setup.tasks.get(taskSlug);
|
|
119
|
+
if (!task) {
|
|
120
|
+
throw new Error(`Unknown task slug: ${taskSlug}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const organizationId = options.organizationId.trim();
|
|
124
|
+
if (!organizationId) {
|
|
125
|
+
throw new Error("Jobs enqueue requires a non-empty organizationId.");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await this.ensureWorker();
|
|
129
|
+
const concurrencyKey =
|
|
130
|
+
options.concurrencyKey ?? task.concurrency?.key(input);
|
|
131
|
+
const exclusive = Boolean(
|
|
132
|
+
task.concurrency && task.concurrency.exclusive !== false,
|
|
133
|
+
);
|
|
134
|
+
const supersedes = options.supersedes ?? task.concurrency?.supersedes;
|
|
135
|
+
const singletonKey =
|
|
136
|
+
(exclusive || supersedes) && concurrencyKey
|
|
137
|
+
? concurrencyKey
|
|
138
|
+
: randomUUID();
|
|
139
|
+
const maxAttempts = options.maxAttempts ?? task.retries?.attempts ?? 1;
|
|
140
|
+
const data: PgBossJobData = {
|
|
141
|
+
taskSlug,
|
|
142
|
+
input,
|
|
143
|
+
organizationId,
|
|
144
|
+
queue: options.queue ?? "default",
|
|
145
|
+
maxAttempts,
|
|
146
|
+
...(concurrencyKey ? { concurrencyKey } : {}),
|
|
147
|
+
};
|
|
148
|
+
const sendOptions = this.translateSendOptions(
|
|
149
|
+
task,
|
|
150
|
+
options,
|
|
151
|
+
maxAttempts,
|
|
152
|
+
singletonKey,
|
|
153
|
+
);
|
|
154
|
+
if (supersedes && concurrencyKey) {
|
|
155
|
+
const result = await this.boss.upsert(this.queueName, data, sendOptions);
|
|
156
|
+
const id = result.jobs[0];
|
|
157
|
+
if (!id) throw new Error("pg-boss did not return an upserted job id.");
|
|
158
|
+
return id;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const id = randomUUID();
|
|
162
|
+
const created = await this.boss.send(this.queueName, data, {
|
|
163
|
+
...sendOptions,
|
|
164
|
+
id,
|
|
165
|
+
});
|
|
166
|
+
if (!created) throw new Error("pg-boss did not create the job.");
|
|
167
|
+
return created;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private requireSetup(): ExecutionEngineSetup {
|
|
171
|
+
if (!this.setup) {
|
|
172
|
+
throw new Error("PgBossExecutionEngine must be registered before use.");
|
|
173
|
+
}
|
|
174
|
+
return this.setup;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private ensureStarted(): Promise<void> {
|
|
178
|
+
this.started ??= this.boss
|
|
179
|
+
.start()
|
|
180
|
+
.then(() => undefined)
|
|
181
|
+
.catch((error) => {
|
|
182
|
+
this.started = undefined;
|
|
183
|
+
throw error;
|
|
184
|
+
});
|
|
185
|
+
return this.started;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private ensureWorker(): Promise<void> {
|
|
189
|
+
this.worker ??= this.startWorker().catch((error) => {
|
|
190
|
+
this.worker = undefined;
|
|
191
|
+
throw error;
|
|
192
|
+
});
|
|
193
|
+
return this.worker;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private async startWorker(): Promise<void> {
|
|
197
|
+
await this.ensureStarted();
|
|
198
|
+
await this.boss.createQueue(this.queueName, { policy: "singleton" });
|
|
199
|
+
await this.boss.work(
|
|
200
|
+
this.queueName,
|
|
201
|
+
{ includeMetadata: true, localConcurrency: this.localConcurrency },
|
|
202
|
+
async (jobs) => Promise.all(jobs.map((job) => this.runJob(job))),
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private async runJob(job: PgBossJob): Promise<Record<string, unknown>> {
|
|
207
|
+
const setup = this.requireSetup();
|
|
208
|
+
const task = setup.tasks.get(job.data.taskSlug);
|
|
209
|
+
if (!task) throw new Error(`Unknown task slug: ${job.data.taskSlug}`);
|
|
210
|
+
|
|
211
|
+
const result = await task.handler({
|
|
212
|
+
input: job.data.input,
|
|
213
|
+
ctx: setup.context,
|
|
214
|
+
job: {
|
|
215
|
+
attemptNumber: job.retryCount + 1,
|
|
216
|
+
maxAttempts: job.retryLimit + 1,
|
|
217
|
+
},
|
|
218
|
+
});
|
|
219
|
+
return result.output;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private translateSendOptions(
|
|
223
|
+
task: TaskDefinition,
|
|
224
|
+
options: EnqueueOptions,
|
|
225
|
+
maxAttempts: number,
|
|
226
|
+
singletonKey: string,
|
|
227
|
+
): Record<string, unknown> {
|
|
228
|
+
const backoff = task.retries?.backoff;
|
|
229
|
+
return {
|
|
230
|
+
singletonKey,
|
|
231
|
+
retryLimit: Math.max(maxAttempts - 1, 0),
|
|
232
|
+
...(backoff
|
|
233
|
+
? {
|
|
234
|
+
retryDelay: Math.max(1, Math.ceil(backoff.delay / 1000)),
|
|
235
|
+
retryBackoff: backoff.type === "exponential",
|
|
236
|
+
}
|
|
237
|
+
: {}),
|
|
238
|
+
...(options.delayMs !== undefined
|
|
239
|
+
? { startAfter: new Date(Date.now() + options.delayMs) }
|
|
240
|
+
: {}),
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
}
|