express-file-cluster 0.3.15 → 0.7.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/README.md +71 -33
- package/dist/auth/index.cjs +12 -2
- package/dist/auth/index.cjs.map +1 -1
- package/dist/auth/index.d.cts +13 -2
- package/dist/auth/index.d.ts +13 -2
- package/dist/auth/index.js +12 -2
- package/dist/chunk-277BSYVH.js +127 -0
- package/dist/chunk-277BSYVH.js.map +1 -0
- package/dist/chunk-6OGOVV57.js +3847 -0
- package/dist/chunk-6OGOVV57.js.map +1 -0
- package/dist/chunk-7YEX4MQV.cjs +38 -0
- package/dist/chunk-7YEX4MQV.cjs.map +1 -0
- package/dist/chunk-AZYXQU3Z.cjs +3847 -0
- package/dist/chunk-AZYXQU3Z.cjs.map +1 -0
- package/dist/chunk-BUBXO7WU.js +170 -0
- package/dist/chunk-BUBXO7WU.js.map +1 -0
- package/dist/chunk-LJNSJFOL.js +38 -0
- package/dist/chunk-LJNSJFOL.js.map +1 -0
- package/dist/chunk-MGH4VDV3.cjs +170 -0
- package/dist/chunk-MGH4VDV3.cjs.map +1 -0
- package/dist/chunk-R34R7QPD.cjs +116 -0
- package/dist/chunk-R34R7QPD.cjs.map +1 -0
- package/dist/chunk-RS4MXCPW.js +116 -0
- package/dist/chunk-RS4MXCPW.js.map +1 -0
- package/dist/chunk-T4OHFU5T.cjs +154 -0
- package/dist/chunk-T4OHFU5T.cjs.map +1 -0
- package/dist/chunk-TI24L4XE.cjs.map +1 -1
- package/dist/chunk-TRC4DUZB.js +438 -0
- package/dist/chunk-TRC4DUZB.js.map +1 -0
- package/dist/chunk-TYR2GZZE.cjs +196 -0
- package/dist/chunk-TYR2GZZE.cjs.map +1 -0
- package/dist/chunk-UBQYQPBU.js +154 -0
- package/dist/chunk-UBQYQPBU.js.map +1 -0
- package/dist/chunk-UTM4PEVG.js +196 -0
- package/dist/chunk-UTM4PEVG.js.map +1 -0
- package/dist/chunk-YL37D6KM.cjs +127 -0
- package/dist/chunk-YL37D6KM.cjs.map +1 -0
- package/dist/chunk-ZDM5JHVK.cjs +438 -0
- package/dist/chunk-ZDM5JHVK.cjs.map +1 -0
- package/dist/cli/db-runner.cjs +497 -0
- package/dist/cli/db-runner.cjs.map +1 -0
- package/dist/cli/db-runner.d.cts +2 -0
- package/dist/cli/db-runner.d.ts +2 -0
- package/dist/cli/db-runner.js +497 -0
- package/dist/cli/db-runner.js.map +1 -0
- package/dist/cli/index.cjs +447 -69
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +473 -95
- package/dist/cli/index.js.map +1 -1
- package/dist/docs/api-reference/auth.md +232 -0
- package/dist/docs/api-reference/compose.md +78 -0
- package/dist/docs/api-reference/db.md +351 -0
- package/dist/docs/api-reference/http-error.md +109 -0
- package/dist/docs/api-reference/ignite.md +326 -0
- package/dist/docs/api-reference/table.md +458 -0
- package/dist/docs/api-reference/tasks.md +140 -0
- package/dist/docs/cli/db.md +38 -0
- package/dist/docs/cli/diagnostics.md +145 -0
- package/dist/docs/cli/generate.md +120 -0
- package/dist/docs/cli/index.md +48 -0
- package/dist/docs/cli/start.md +112 -0
- package/dist/docs/contributing/index.md +139 -0
- package/dist/docs/core-concepts/background-tasks.md +228 -0
- package/dist/docs/core-concepts/clustering.md +114 -0
- package/dist/docs/core-concepts/file-based-routing.md +235 -0
- package/dist/docs/core-concepts/index.md +79 -0
- package/dist/docs/core-concepts/middleware.md +144 -0
- package/dist/docs/getting-started/index.md +91 -0
- package/dist/docs/getting-started/project-structure.md +154 -0
- package/dist/docs/guides/ai-agents.md +48 -0
- package/dist/docs/guides/authentication.md +196 -0
- package/dist/docs/guides/caching.md +124 -0
- package/dist/docs/guides/database.md +217 -0
- package/dist/docs/guides/deployment.md +171 -0
- package/dist/docs/guides/environment-variables.md +106 -0
- package/dist/docs/guides/error-handling.md +172 -0
- package/dist/docs/guides/generated-portals.md +115 -0
- package/dist/docs/guides/logging.md +108 -0
- package/dist/docs/guides/mailer.md +166 -0
- package/dist/docs/guides/migrations.md +106 -0
- package/dist/docs/guides/rbac.md +89 -0
- package/dist/docs/guides/validation.md +95 -0
- package/dist/docs/index.md +93 -0
- package/dist/docs/skills/SKILL.md +44 -0
- package/dist/docs/skills/api-surface.md +309 -0
- package/dist/docs/skills/auth-internals.md +141 -0
- package/dist/docs/skills/pre-flight.md +142 -0
- package/dist/docs/skills/routing-rules.md +114 -0
- package/dist/docs/skills/tasks-internals.md +172 -0
- package/dist/docs/skills/what-not-to-invent.md +251 -0
- package/dist/index.cjs +264 -391
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +699 -7
- package/dist/index.d.ts +699 -7
- package/dist/index.js +258 -385
- package/dist/index.js.map +1 -1
- package/dist/pgboss-backend-QKBXVJ6B.js +14 -0
- package/dist/pgboss-backend-QKBXVJ6B.js.map +1 -0
- package/dist/pgboss-backend-TCHI7CHM.cjs +14 -0
- package/dist/pgboss-backend-TCHI7CHM.cjs.map +1 -0
- package/dist/tasks/index.cjs.map +1 -1
- package/dist/tasks/index.d.cts +1 -1
- package/dist/tasks/index.d.ts +1 -1
- package/dist/types-B6lVw4bj.d.cts +588 -0
- package/dist/types-B6lVw4bj.d.ts +588 -0
- package/package.json +28 -5
- package/dist/chunk-6522HS2J.cjs +0 -99
- package/dist/chunk-6522HS2J.cjs.map +0 -1
- package/dist/chunk-DGVOJNR6.cjs +0 -55
- package/dist/chunk-DGVOJNR6.cjs.map +0 -1
- package/dist/chunk-KOHLLUN7.js +0 -55
- package/dist/chunk-KOHLLUN7.js.map +0 -1
- package/dist/chunk-VIP3PFWZ.js +0 -99
- package/dist/chunk-VIP3PFWZ.js.map +0 -1
- package/dist/types-BusbSenn.d.cts +0 -162
- package/dist/types-BusbSenn.d.ts +0 -162
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunk7YEX4MQVcjs = require('./chunk-7YEX4MQV.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunkTI24L4XEcjs = require('./chunk-TI24L4XE.cjs');
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
var _chunkYL37D6KMcjs = require('./chunk-YL37D6KM.cjs');
|
|
11
|
+
|
|
12
|
+
// src/tasks/pgboss-backend.ts
|
|
13
|
+
var BOX = "__efcValue";
|
|
14
|
+
var box = (payload) => payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : { [BOX]: payload };
|
|
15
|
+
var unbox = (data) => data !== null && typeof data === "object" && BOX in data ? data[BOX] : data;
|
|
16
|
+
var retryOptions = (def) => ({
|
|
17
|
+
// pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.
|
|
18
|
+
retryLimit: _nullishCoalesce(def.options.retries, () => ( 3)),
|
|
19
|
+
retryDelay: 1,
|
|
20
|
+
retryBackoff: (_nullishCoalesce(def.options.backoff, () => ( "exponential"))) === "exponential"
|
|
21
|
+
});
|
|
22
|
+
async function loadPgBoss() {
|
|
23
|
+
try {
|
|
24
|
+
const mod = await Promise.resolve().then(() => _interopRequireWildcard(require("pg-boss")));
|
|
25
|
+
const ctor = _nullishCoalesce(mod.PgBoss, () => ( (typeof mod.default === "function" ? mod.default : mod.default.PgBoss)));
|
|
26
|
+
return ctor;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
throw new Error("[EFC] pg-boss is not installed. Run: npm install pg-boss");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
var closeImpl = null;
|
|
32
|
+
async function closePgBoss() {
|
|
33
|
+
const close = closeImpl;
|
|
34
|
+
closeImpl = null;
|
|
35
|
+
await _optionalChain([close, 'optionalCall', _2 => _2()]);
|
|
36
|
+
}
|
|
37
|
+
async function initPgBoss(opts) {
|
|
38
|
+
const PgBoss = await loadPgBoss();
|
|
39
|
+
const boss = new PgBoss({
|
|
40
|
+
connectionString: opts.connectionString,
|
|
41
|
+
...opts.schema !== void 0 && { schema: opts.schema }
|
|
42
|
+
});
|
|
43
|
+
boss.on("error", (err) => _chunkYL37D6KMcjs.getLogger.call(void 0, ).error({ err }, "pg-boss error"));
|
|
44
|
+
await boss.start();
|
|
45
|
+
const queueFor = (name) => `${opts.queue}.${name}`;
|
|
46
|
+
const wantedSchedules = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const def of _chunkTI24L4XEcjs.taskRegistry.values()) {
|
|
48
|
+
const queue = queueFor(def.name);
|
|
49
|
+
await boss.createQueue(queue, retryOptions(def));
|
|
50
|
+
await boss.work(
|
|
51
|
+
queue,
|
|
52
|
+
{ localConcurrency: _nullishCoalesce(def.options.concurrency, () => ( opts.concurrency)) },
|
|
53
|
+
async (jobs) => {
|
|
54
|
+
for (const job of jobs) {
|
|
55
|
+
const payload = unbox(job.data);
|
|
56
|
+
try {
|
|
57
|
+
if (def.options.thread && def.filePath) await _chunk7YEX4MQVcjs.runInThread.call(void 0, def.filePath, payload);
|
|
58
|
+
else await def.handler(payload);
|
|
59
|
+
_chunkYL37D6KMcjs.getLogger.call(void 0, ).info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
_chunkYL37D6KMcjs.getLogger.call(void 0, ).error(
|
|
62
|
+
{ task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },
|
|
63
|
+
`Task ${def.name} failed`
|
|
64
|
+
);
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
if (def.options.schedule) {
|
|
71
|
+
wantedSchedules.add(queue);
|
|
72
|
+
await boss.schedule(
|
|
73
|
+
queue,
|
|
74
|
+
def.options.schedule,
|
|
75
|
+
{},
|
|
76
|
+
{
|
|
77
|
+
...retryOptions(def),
|
|
78
|
+
...def.options.timezone !== void 0 && { tz: def.options.timezone }
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const s of await boss.getSchedules()) {
|
|
84
|
+
if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {
|
|
85
|
+
await boss.unschedule(s.name);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (wantedSchedules.size > 0) _chunkYL37D6KMcjs.getLogger.call(void 0, ).info(`Scheduled ${wantedSchedules.size} cron task(s)`);
|
|
89
|
+
_chunkTI24L4XEcjs.setEnqueueImpl.call(void 0, async (name, payload) => {
|
|
90
|
+
const def = _chunkTI24L4XEcjs.taskRegistry.get(name);
|
|
91
|
+
if (!def) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`[EFC] Cannot enqueue unknown task: "${name}". Is the task file in tasksDir?`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
let timer;
|
|
97
|
+
const timeout = new Promise((_, reject) => {
|
|
98
|
+
timer = setTimeout(
|
|
99
|
+
() => reject(
|
|
100
|
+
new Error(
|
|
101
|
+
`[EFC] Cannot enqueue task "${name}": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`
|
|
102
|
+
)
|
|
103
|
+
),
|
|
104
|
+
opts.enqueueTimeout
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
if (err instanceof Error && err.message.startsWith("[EFC]")) throw err;
|
|
111
|
+
throw new Error(
|
|
112
|
+
`[EFC] Cannot enqueue task "${name}": ${err instanceof Error ? err.message : String(err)}`
|
|
113
|
+
);
|
|
114
|
+
} finally {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
closeImpl = async () => {
|
|
119
|
+
await boss.stop({ graceful: true, timeout: 1e4 });
|
|
120
|
+
};
|
|
121
|
+
_chunkYL37D6KMcjs.getLogger.call(void 0, ).info("pg-boss backend initialised");
|
|
122
|
+
}
|
|
123
|
+
async function pgBossAdmin(connectionString, queue = "efc", schema = "pgboss") {
|
|
124
|
+
const PgBoss = await loadPgBoss();
|
|
125
|
+
const boss = new PgBoss({
|
|
126
|
+
connectionString,
|
|
127
|
+
schema,
|
|
128
|
+
supervise: false,
|
|
129
|
+
schedule: false,
|
|
130
|
+
migrate: false
|
|
131
|
+
});
|
|
132
|
+
boss.on("error", () => {
|
|
133
|
+
});
|
|
134
|
+
await boss.start();
|
|
135
|
+
const db = boss.getDb();
|
|
136
|
+
const prefix = `${queue}.`;
|
|
137
|
+
const taskOf = (name) => name.slice(prefix.length);
|
|
138
|
+
const table = `"${schema.replace(/"/g, '""')}".job`;
|
|
139
|
+
const failedRows = async (ids, limit = 1e4) => (await db.executeSql(
|
|
140
|
+
`SELECT id, name, data, output, retry_count, completed_on FROM ${table}
|
|
141
|
+
WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? "AND id = ANY($3::uuid[])" : ""}
|
|
142
|
+
ORDER BY completed_on DESC NULLS LAST LIMIT $2`,
|
|
143
|
+
ids.length ? [prefix, limit, ids] : [prefix, limit]
|
|
144
|
+
)).rows;
|
|
145
|
+
const byQueue = (rows) => {
|
|
146
|
+
const groups = /* @__PURE__ */ new Map();
|
|
147
|
+
for (const r of rows) groups.set(r.name, [..._nullishCoalesce(groups.get(r.name), () => ( [])), r.id]);
|
|
148
|
+
return groups;
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
async counts() {
|
|
152
|
+
const { rows } = await db.executeSql(
|
|
153
|
+
`SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,
|
|
154
|
+
[prefix]
|
|
155
|
+
);
|
|
156
|
+
return Object.fromEntries(
|
|
157
|
+
rows.map((r) => [r.state, r.n])
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
async listFailed({ limit, name }) {
|
|
161
|
+
const rows = (await failedRows([], name ? 1e4 : limit)).filter(
|
|
162
|
+
(r) => !name || taskOf(r.name) === name
|
|
163
|
+
);
|
|
164
|
+
return rows.slice(0, limit).map(
|
|
165
|
+
(r) => ({
|
|
166
|
+
id: r.id,
|
|
167
|
+
name: taskOf(r.name),
|
|
168
|
+
data: unbox(r.data),
|
|
169
|
+
error: String(_nullishCoalesce(_nullishCoalesce(_optionalChain([r, 'access', _3 => _3.output, 'optionalAccess', _4 => _4.message]), () => ( r.output)), () => ( ""))),
|
|
170
|
+
attempts: r.retry_count + 1,
|
|
171
|
+
failedAt: _nullishCoalesce(r.completed_on, () => ( void 0))
|
|
172
|
+
})
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
async retry(ids) {
|
|
176
|
+
const rows = await failedRows(ids);
|
|
177
|
+
for (const [q, list] of byQueue(rows)) await boss.retry(q, list);
|
|
178
|
+
return rows.length;
|
|
179
|
+
},
|
|
180
|
+
async purge(ids) {
|
|
181
|
+
const rows = await failedRows(ids);
|
|
182
|
+
for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);
|
|
183
|
+
return rows.length;
|
|
184
|
+
},
|
|
185
|
+
async close() {
|
|
186
|
+
await boss.stop({ graceful: false });
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
exports.closePgBoss = closePgBoss; exports.initPgBoss = initPgBoss; exports.pgBossAdmin = pgBossAdmin;
|
|
196
|
+
//# sourceMappingURL=chunk-TYR2GZZE.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/chunk-TYR2GZZE.cjs","../src/tasks/pgboss-backend.ts"],"names":[],"mappings":"AAAA;AACE;AACF,wDAA6B;AAC7B;AACE;AACA;AACF,wDAA6B;AAC7B;AACE;AACF,wDAA6B;AAC7B;AACA;ACQA,IAAM,IAAA,EAAM,YAAA;AACZ,IAAM,IAAA,EAAM,CAAC,OAAA,EAAA,GACX,QAAA,IAAY,KAAA,GAAQ,OAAO,QAAA,IAAY,SAAA,GAAY,CAAC,KAAA,CAAM,OAAA,CAAQ,OAAO,EAAA,EACpE,QAAA,EACD,EAAE,CAAC,GAAG,CAAA,EAAG,QAAQ,CAAA;AACvB,IAAM,MAAA,EAAQ,CAAC,IAAA,EAAA,GACb,KAAA,IAAS,KAAA,GAAQ,OAAO,KAAA,IAAS,SAAA,GAAY,IAAA,GAAO,KAAA,EAC/C,IAAA,CAAiC,GAAG,EAAA,EACrC,IAAA;AAEN,IAAM,aAAA,EAAe,CAAC,GAAA,EAAA,GAAA,CAAyB;AAAA;AAAA,EAE7C,UAAA,mBAAY,GAAA,CAAI,OAAA,CAAQ,OAAA,UAAW,GAAA;AAAA,EACnC,UAAA,EAAY,CAAA;AAAA,EACZ,YAAA,EAAA,kBAAe,GAAA,CAAI,OAAA,CAAQ,OAAA,UAAW,eAAA,EAAA,IAAmB;AAC3D,CAAA,CAAA;AAEA,MAAA,SAAe,UAAA,CAAA,EAAyC;AACtD,EAAA,IAAI;AACF,IAAA,MAAM,IAAA,EAAO,MAAM,4DAAA,CAAO,SAAS,GAAA;AAInC,IAAA,MAAM,KAAA,mBACJ,GAAA,CAAI,MAAA,U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AA;AACI,MAAA;AACK,MAAA;AACZ,MAAA;AACd,IAAA;AACiB,IAAA;AACI,MAAA;AACK,MAAA;AACZ,MAAA;AACd,IAAA;AACc,IAAA;AACM,MAAA;AACpB,IAAA;AACF,EAAA;AACF;ADpD8B;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/express-file-cluster/express-file-cluster/packages/core/dist/chunk-TYR2GZZE.cjs","sourcesContent":[null,"import type { PgBoss as PgBossType } from 'pg-boss';\nimport type { TaskDefinition } from '../types.js';\nimport { taskRegistry, setEnqueueImpl } from './index.js';\nimport { runInThread } from './thread-runner.js';\nimport { getLogger } from '../logger.js';\nimport type { FailedJob, TaskAdmin } from './admin.js';\n\n// pg-boss keeps one queue per job name, so each task gets its own pg-boss queue\n// named `<tasks.queue>.<TaskName>` — the prefix keeps apps sharing a database apart.\n\ninterface PgBossOpts {\n connectionString: string;\n concurrency: number;\n enqueueTimeout: number;\n queue: string;\n schema?: string;\n}\n\n// pg-boss payloads must be objects; other values are boxed so any enqueue() payload works.\nconst BOX = '__efcValue';\nconst box = (payload: unknown): object =>\n payload !== null && typeof payload === 'object' && !Array.isArray(payload)\n ? (payload as object)\n : { [BOX]: payload };\nconst unbox = (data: unknown): unknown =>\n data !== null && typeof data === 'object' && BOX in data\n ? (data as Record<string, unknown>)[BOX]\n : data;\n\nconst retryOptions = (def: TaskDefinition) => ({\n // pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.\n retryLimit: def.options.retries ?? 3,\n retryDelay: 1,\n retryBackoff: (def.options.backoff ?? 'exponential') === 'exponential',\n});\n\nasync function loadPgBoss(): Promise<typeof PgBossType> {\n try {\n const mod = (await import('pg-boss')) as unknown as {\n PgBoss?: typeof PgBossType;\n default?: typeof PgBossType | { PgBoss: typeof PgBossType };\n };\n const ctor =\n mod.PgBoss ??\n (typeof mod.default === 'function'\n ? mod.default\n : (mod.default as { PgBoss: typeof PgBossType }).PgBoss);\n return ctor;\n } catch {\n throw new Error('[EFC] pg-boss is not installed. Run: npm install pg-boss');\n }\n}\n\nlet closeImpl: (() => Promise<void>) | null = null;\n\nexport async function closePgBoss(): Promise<void> {\n const close = closeImpl;\n closeImpl = null;\n await close?.();\n}\n\nexport async function initPgBoss(opts: PgBossOpts): Promise<void> {\n const PgBoss = await loadPgBoss();\n const boss = new PgBoss({\n connectionString: opts.connectionString,\n ...(opts.schema !== undefined && { schema: opts.schema }),\n });\n boss.on('error', (err: Error) => getLogger().error({ err }, 'pg-boss error'));\n await boss.start();\n\n const queueFor = (name: string) => `${opts.queue}.${name}`;\n const wantedSchedules = new Set<string>();\n\n for (const def of taskRegistry.values()) {\n const queue = queueFor(def.name);\n await boss.createQueue(queue, retryOptions(def));\n await boss.work(\n queue,\n { localConcurrency: def.options.concurrency ?? opts.concurrency },\n async (jobs: { id: string; data: unknown; retryCount: number }[]) => {\n for (const job of jobs) {\n const payload = unbox(job.data);\n try {\n if (def.options.thread && def.filePath) await runInThread(def.filePath, payload);\n else await def.handler(payload);\n getLogger().info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);\n } catch (err) {\n getLogger().error(\n { task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },\n `Task ${def.name} failed`,\n );\n throw err;\n }\n }\n },\n );\n if (def.options.schedule) {\n wantedSchedules.add(queue);\n await boss.schedule(\n queue,\n def.options.schedule,\n {},\n {\n ...retryOptions(def),\n ...(def.options.timezone !== undefined && { tz: def.options.timezone }),\n },\n );\n }\n }\n\n // Drop schedules for tasks that lost their `schedule` or no longer exist.\n for (const s of await boss.getSchedules()) {\n if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {\n await boss.unschedule(s.name);\n }\n }\n if (wantedSchedules.size > 0) getLogger().info(`Scheduled ${wantedSchedules.size} cron task(s)`);\n\n setEnqueueImpl(async (name, payload) => {\n const def = taskRegistry.get(name);\n if (!def) {\n throw new Error(\n `[EFC] Cannot enqueue unknown task: \"${name}\". Is the task file in tasksDir?`,\n );\n }\n let timer: NodeJS.Timeout | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(\n () =>\n reject(\n new Error(\n `[EFC] Cannot enqueue task \"${name}\": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`,\n ),\n ),\n opts.enqueueTimeout,\n );\n });\n try {\n await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);\n } catch (err) {\n if (err instanceof Error && err.message.startsWith('[EFC]')) throw err;\n throw new Error(\n `[EFC] Cannot enqueue task \"${name}\": ${err instanceof Error ? err.message : String(err)}`,\n );\n } finally {\n clearTimeout(timer);\n }\n });\n\n closeImpl = async () => {\n // graceful: lets in-flight jobs finish before the connection pool closes.\n await boss.stop({ graceful: true, timeout: 10_000 });\n };\n\n getLogger().info('pg-boss backend initialised');\n}\n\n/** Dead-letter inspection for `efc tasks failed | retry | purge` on pg-boss. */\nexport async function pgBossAdmin(\n connectionString: string,\n queue = 'efc',\n schema = 'pgboss',\n): Promise<TaskAdmin> {\n const PgBoss = await loadPgBoss();\n // No supervision or cron here: this instance only reads and settles jobs.\n const boss = new PgBoss({\n connectionString,\n schema,\n supervise: false,\n schedule: false,\n migrate: false,\n });\n boss.on('error', () => {});\n await boss.start();\n const db = boss.getDb();\n const prefix = `${queue}.`;\n const taskOf = (name: string) => name.slice(prefix.length);\n const table = `\"${schema.replace(/\"/g, '\"\"')}\".job`;\n\n const failedRows = async (ids: string[], limit = 10_000) =>\n (\n await db.executeSql(\n `SELECT id, name, data, output, retry_count, completed_on FROM ${table}\n WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? 'AND id = ANY($3::uuid[])' : ''}\n ORDER BY completed_on DESC NULLS LAST LIMIT $2`,\n ids.length ? [prefix, limit, ids] : [prefix, limit],\n )\n ).rows as {\n id: string;\n name: string;\n data: unknown;\n output: unknown;\n retry_count: number;\n completed_on: Date | null;\n }[];\n\n const byQueue = (rows: { id: string; name: string }[]) => {\n const groups = new Map<string, string[]>();\n for (const r of rows) groups.set(r.name, [...(groups.get(r.name) ?? []), r.id]);\n return groups;\n };\n\n return {\n async counts() {\n const { rows } = await db.executeSql(\n `SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,\n [prefix],\n );\n return Object.fromEntries(\n (rows as { state: string; n: number }[]).map((r) => [r.state, r.n]),\n );\n },\n async listFailed({ limit, name }) {\n const rows = (await failedRows([], name ? 10_000 : limit)).filter(\n (r) => !name || taskOf(r.name) === name,\n );\n return rows.slice(0, limit).map(\n (r): FailedJob => ({\n id: r.id,\n name: taskOf(r.name),\n data: unbox(r.data),\n error: String((r.output as { message?: string } | null)?.message ?? r.output ?? ''),\n attempts: r.retry_count + 1,\n failedAt: r.completed_on ?? undefined,\n }),\n );\n },\n async retry(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.retry(q, list);\n return rows.length;\n },\n async purge(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);\n return rows.length;\n },\n async close() {\n await boss.stop({ graceful: false });\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import {
|
|
2
|
+
runInThread
|
|
3
|
+
} from "./chunk-LJNSJFOL.js";
|
|
4
|
+
import {
|
|
5
|
+
setEnqueueImpl,
|
|
6
|
+
taskRegistry
|
|
7
|
+
} from "./chunk-HBYSKULC.js";
|
|
8
|
+
import {
|
|
9
|
+
getLogger
|
|
10
|
+
} from "./chunk-277BSYVH.js";
|
|
11
|
+
|
|
12
|
+
// src/tasks/bullmq-backend.ts
|
|
13
|
+
var closeImpl = null;
|
|
14
|
+
var schedulesSynced = Promise.resolve();
|
|
15
|
+
var SCHEDULER_PREFIX = "efc-cron:";
|
|
16
|
+
function jobOptions(def) {
|
|
17
|
+
return {
|
|
18
|
+
// BullMQ's `attempts` counts the first run; `retries` counts only the re-runs.
|
|
19
|
+
attempts: (def.options.retries ?? 3) + 1,
|
|
20
|
+
backoff: { type: def.options.backoff ?? "exponential", delay: 1e3 }
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
async function syncSchedules(queue) {
|
|
24
|
+
const wanted = /* @__PURE__ */ new Set();
|
|
25
|
+
for (const def of taskRegistry.values()) {
|
|
26
|
+
if (!def.options.schedule) continue;
|
|
27
|
+
const id = SCHEDULER_PREFIX + def.name;
|
|
28
|
+
wanted.add(id);
|
|
29
|
+
await queue.upsertJobScheduler(
|
|
30
|
+
id,
|
|
31
|
+
{
|
|
32
|
+
pattern: def.options.schedule,
|
|
33
|
+
...def.options.timezone !== void 0 && { tz: def.options.timezone }
|
|
34
|
+
},
|
|
35
|
+
{ name: def.name, data: {}, opts: jobOptions(def) }
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
for (const s of await queue.getJobSchedulers(0, -1)) {
|
|
39
|
+
if (s.key.startsWith(SCHEDULER_PREFIX) && !wanted.has(s.key)) {
|
|
40
|
+
await queue.removeJobScheduler(s.key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (wanted.size > 0) getLogger().info(`Scheduled ${wanted.size} cron task(s)`);
|
|
44
|
+
}
|
|
45
|
+
async function closeBullMQ() {
|
|
46
|
+
const close = closeImpl;
|
|
47
|
+
closeImpl = null;
|
|
48
|
+
await close?.();
|
|
49
|
+
}
|
|
50
|
+
async function initBullMQ(opts) {
|
|
51
|
+
let bullmq;
|
|
52
|
+
try {
|
|
53
|
+
bullmq = await import("bullmq");
|
|
54
|
+
} catch {
|
|
55
|
+
throw new Error("[EFC] bullmq is not installed. Run: npm install bullmq");
|
|
56
|
+
}
|
|
57
|
+
const connection = parseRedisUrl(opts.redisUrl);
|
|
58
|
+
const queue = new bullmq.Queue(opts.queue, {
|
|
59
|
+
connection: { ...connection, enableOfflineQueue: false }
|
|
60
|
+
});
|
|
61
|
+
queue.on("error", () => {
|
|
62
|
+
});
|
|
63
|
+
const worker = new bullmq.Worker(
|
|
64
|
+
opts.queue,
|
|
65
|
+
async (job) => {
|
|
66
|
+
const def = taskRegistry.get(job.name);
|
|
67
|
+
if (!def) {
|
|
68
|
+
throw new Error(`[EFC] No task registered for name: ${job.name}`);
|
|
69
|
+
}
|
|
70
|
+
if (def.options.thread && def.filePath) {
|
|
71
|
+
await runInThread(def.filePath, job.data);
|
|
72
|
+
} else {
|
|
73
|
+
await def.handler(job.data);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
{ connection, concurrency: opts.concurrency }
|
|
77
|
+
);
|
|
78
|
+
worker.on("failed", (job, err) => {
|
|
79
|
+
getLogger().error(
|
|
80
|
+
{ task: job?.name, jobId: job?.id, attempt: job?.attemptsMade, err },
|
|
81
|
+
`Task ${job?.name ?? "unknown"} failed`
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
worker.on("error", (err) => {
|
|
85
|
+
getLogger().error(`Task worker error: ${err.message}`);
|
|
86
|
+
});
|
|
87
|
+
worker.on("completed", (job) => {
|
|
88
|
+
getLogger().info({ task: job.name, jobId: job.id }, `Task ${job.name} completed`);
|
|
89
|
+
});
|
|
90
|
+
setEnqueueImpl(async (name, payload) => {
|
|
91
|
+
const def = taskRegistry.get(name);
|
|
92
|
+
if (!def) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`[EFC] Cannot enqueue unknown task: "${name}". Is the task file in tasksDir?`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
const add = queue.waitUntilReady().then(() => queue.add(name, payload, jobOptions(def)));
|
|
98
|
+
let timer;
|
|
99
|
+
const timeout = new Promise((_, reject) => {
|
|
100
|
+
timer = setTimeout(
|
|
101
|
+
() => reject(
|
|
102
|
+
new Error(
|
|
103
|
+
`[EFC] Cannot enqueue task "${name}": Redis at ${connection.host}:${connection.port} did not respond within ${opts.enqueueTimeout}ms`
|
|
104
|
+
)
|
|
105
|
+
),
|
|
106
|
+
opts.enqueueTimeout
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
try {
|
|
110
|
+
await Promise.race([add, timeout]);
|
|
111
|
+
} catch (err) {
|
|
112
|
+
if (err instanceof Error && err.message.startsWith("[EFC]")) throw err;
|
|
113
|
+
throw new Error(
|
|
114
|
+
`[EFC] Cannot enqueue task "${name}": ${err instanceof Error ? err.message : String(err)}`
|
|
115
|
+
);
|
|
116
|
+
} finally {
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
let closing = false;
|
|
121
|
+
schedulesSynced = queue.waitUntilReady().then(() => syncSchedules(queue)).catch((err) => {
|
|
122
|
+
if (!closing) getLogger().error({ err }, "Failed to sync cron task schedules");
|
|
123
|
+
});
|
|
124
|
+
closeImpl = async () => {
|
|
125
|
+
closing = true;
|
|
126
|
+
await worker.close();
|
|
127
|
+
await queue.close();
|
|
128
|
+
};
|
|
129
|
+
getLogger().info("BullMQ backend initialised");
|
|
130
|
+
}
|
|
131
|
+
function parseRedisUrl(url) {
|
|
132
|
+
try {
|
|
133
|
+
const u = new URL(url);
|
|
134
|
+
const result = {
|
|
135
|
+
host: u.hostname || "localhost",
|
|
136
|
+
port: parseInt(u.port || "6379", 10)
|
|
137
|
+
};
|
|
138
|
+
if (u.username) result.username = decodeURIComponent(u.username);
|
|
139
|
+
if (u.password) result.password = decodeURIComponent(u.password);
|
|
140
|
+
const db = parseInt(u.pathname.slice(1), 10);
|
|
141
|
+
if (!Number.isNaN(db)) result.db = db;
|
|
142
|
+
if (u.protocol === "rediss:") result.tls = {};
|
|
143
|
+
return result;
|
|
144
|
+
} catch {
|
|
145
|
+
return { host: "localhost", port: 6379 };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export {
|
|
150
|
+
closeBullMQ,
|
|
151
|
+
initBullMQ,
|
|
152
|
+
parseRedisUrl
|
|
153
|
+
};
|
|
154
|
+
//# sourceMappingURL=chunk-UBQYQPBU.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/tasks/bullmq-backend.ts"],"sourcesContent":["import type { Job } from 'bullmq';\nimport type * as BullMQNS from 'bullmq';\nimport { taskRegistry, setEnqueueImpl } from './index.js';\nimport type { TaskDefinition } from '../types.js';\nimport { runInThread } from './thread-runner.js';\nimport { getLogger } from '../logger.js';\n\ninterface BullMQOpts {\n redisUrl: string;\n concurrency: number;\n enqueueTimeout: number;\n queue: string;\n}\n\nlet closeImpl: (() => Promise<void>) | null = null;\nlet schedulesSynced: Promise<void> = Promise.resolve();\n\n/** Resolves once cron schedulers have been reconciled with the registry (tests, CLI). */\nexport function whenSchedulesSynced(): Promise<void> {\n return schedulesSynced;\n}\n\nexport const SCHEDULER_PREFIX = 'efc-cron:';\n\nfunction jobOptions(def: TaskDefinition) {\n return {\n // BullMQ's `attempts` counts the first run; `retries` counts only the re-runs.\n attempts: (def.options.retries ?? 3) + 1,\n backoff: { type: def.options.backoff ?? 'exponential', delay: 1000 },\n };\n}\n\n// One job scheduler per task with a `schedule`, keyed by task name. upsert is\n// idempotent, so every cluster worker can run this without creating duplicates;\n// schedulers whose task lost its `schedule` (or was deleted) are removed.\nasync function syncSchedules(queue: BullMQNS.Queue): Promise<void> {\n const wanted = new Set<string>();\n for (const def of taskRegistry.values()) {\n if (!def.options.schedule) continue;\n const id = SCHEDULER_PREFIX + def.name;\n wanted.add(id);\n await queue.upsertJobScheduler(\n id,\n {\n pattern: def.options.schedule,\n ...(def.options.timezone !== undefined && { tz: def.options.timezone }),\n },\n { name: def.name, data: {}, opts: jobOptions(def) },\n );\n }\n for (const s of await queue.getJobSchedulers(0, -1)) {\n if (s.key.startsWith(SCHEDULER_PREFIX) && !wanted.has(s.key)) {\n await queue.removeJobScheduler(s.key);\n }\n }\n if (wanted.size > 0) getLogger().info(`Scheduled ${wanted.size} cron task(s)`);\n}\n\n/** Drains the BullMQ worker and closes both Redis connections. No-op if never initialised. */\nexport async function closeBullMQ(): Promise<void> {\n const close = closeImpl;\n closeImpl = null;\n await close?.();\n}\n\nexport async function initBullMQ(opts: BullMQOpts): Promise<void> {\n let bullmq: typeof BullMQNS;\n try {\n bullmq = await import('bullmq');\n } catch {\n throw new Error('[EFC] bullmq is not installed. Run: npm install bullmq');\n }\n\n const connection = parseRedisUrl(opts.redisUrl);\n // With the offline queue on (ioredis default) queue.add() buffers forever while Redis is\n // down, so enqueue() never settles. Off, commands reject once the connection drops; the\n // initial connect is bounded by the enqueueTimeout race below.\n const queue = new bullmq.Queue(opts.queue, {\n connection: { ...connection, enableOfflineQueue: false },\n });\n // Without a listener, ioredis connection errors surface as unhandled 'error' events.\n queue.on('error', () => {});\n\n const worker = new bullmq.Worker(\n opts.queue,\n async (job: Job) => {\n const def = taskRegistry.get(job.name);\n if (!def) {\n throw new Error(`[EFC] No task registered for name: ${job.name}`);\n }\n\n if (def.options.thread && def.filePath) {\n await runInThread(def.filePath, job.data);\n } else {\n await def.handler(job.data);\n }\n },\n { connection, concurrency: opts.concurrency },\n );\n\n worker.on('failed', (job: Job | undefined, err: Error) => {\n getLogger().error(\n { task: job?.name, jobId: job?.id, attempt: job?.attemptsMade, err },\n `Task ${job?.name ?? 'unknown'} failed`,\n );\n });\n\n worker.on('error', (err: Error) => {\n getLogger().error(`Task worker error: ${err.message}`);\n });\n\n worker.on('completed', (job: Job) => {\n getLogger().info({ task: job.name, jobId: job.id }, `Task ${job.name} completed`);\n });\n\n setEnqueueImpl(async (name, payload) => {\n const def = taskRegistry.get(name);\n if (!def) {\n throw new Error(\n `[EFC] Cannot enqueue unknown task: \"${name}\". Is the task file in tasksDir?`,\n );\n }\n\n const add = queue.waitUntilReady().then(() => queue.add(name, payload, jobOptions(def)));\n\n let timer: NodeJS.Timeout | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(\n () =>\n reject(\n new Error(\n `[EFC] Cannot enqueue task \"${name}\": Redis at ${connection.host}:${connection.port} did not respond within ${opts.enqueueTimeout}ms`,\n ),\n ),\n opts.enqueueTimeout,\n );\n });\n\n try {\n await Promise.race([add, timeout]);\n } catch (err) {\n if (err instanceof Error && err.message.startsWith('[EFC]')) throw err;\n throw new Error(\n `[EFC] Cannot enqueue task \"${name}\": ${err instanceof Error ? err.message : String(err)}`,\n );\n } finally {\n clearTimeout(timer);\n }\n });\n\n let closing = false;\n // Not awaited: a Redis outage at boot must not block the HTTP server from starting.\n schedulesSynced = queue\n .waitUntilReady()\n .then(() => syncSchedules(queue))\n .catch((err: unknown) => {\n // Shutting down before Redis ever answered aborts the wait — not a real failure.\n if (!closing) getLogger().error({ err }, 'Failed to sync cron task schedules');\n });\n\n closeImpl = async () => {\n closing = true;\n await worker.close();\n await queue.close();\n };\n\n getLogger().info('BullMQ backend initialised');\n}\n\ninterface RedisConnection {\n host: string;\n port: number;\n username?: string;\n password?: string;\n db?: number;\n tls?: Record<string, never>;\n}\n\nexport function parseRedisUrl(url: string): RedisConnection {\n try {\n const u = new URL(url);\n const result: RedisConnection = {\n host: u.hostname || 'localhost',\n port: parseInt(u.port || '6379', 10),\n };\n if (u.username) result.username = decodeURIComponent(u.username);\n if (u.password) result.password = decodeURIComponent(u.password);\n const db = parseInt(u.pathname.slice(1), 10);\n if (!Number.isNaN(db)) result.db = db;\n if (u.protocol === 'rediss:') result.tls = {};\n return result;\n } catch {\n return { host: 'localhost', port: 6379 };\n }\n}\n"],"mappings":";;;;;;;;;;;;AAcA,IAAI,YAA0C;AAC9C,IAAI,kBAAiC,QAAQ,QAAQ;AAO9C,IAAM,mBAAmB;AAEhC,SAAS,WAAW,KAAqB;AACvC,SAAO;AAAA;AAAA,IAEL,WAAW,IAAI,QAAQ,WAAW,KAAK;AAAA,IACvC,SAAS,EAAE,MAAM,IAAI,QAAQ,WAAW,eAAe,OAAO,IAAK;AAAA,EACrE;AACF;AAKA,eAAe,cAAc,OAAsC;AACjE,QAAM,SAAS,oBAAI,IAAY;AAC/B,aAAW,OAAO,aAAa,OAAO,GAAG;AACvC,QAAI,CAAC,IAAI,QAAQ,SAAU;AAC3B,UAAM,KAAK,mBAAmB,IAAI;AAClC,WAAO,IAAI,EAAE;A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ry
|
|
7
|
+
} from "./chunk-HBYSKULC.js";
|
|
8
|
+
import {
|
|
9
|
+
getLogger
|
|
10
|
+
} from "./chunk-277BSYVH.js";
|
|
11
|
+
|
|
12
|
+
// src/tasks/pgboss-backend.ts
|
|
13
|
+
var BOX = "__efcValue";
|
|
14
|
+
var box = (payload) => payload !== null && typeof payload === "object" && !Array.isArray(payload) ? payload : { [BOX]: payload };
|
|
15
|
+
var unbox = (data) => data !== null && typeof data === "object" && BOX in data ? data[BOX] : data;
|
|
16
|
+
var retryOptions = (def) => ({
|
|
17
|
+
// pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.
|
|
18
|
+
retryLimit: def.options.retries ?? 3,
|
|
19
|
+
retryDelay: 1,
|
|
20
|
+
retryBackoff: (def.options.backoff ?? "exponential") === "exponential"
|
|
21
|
+
});
|
|
22
|
+
async function loadPgBoss() {
|
|
23
|
+
try {
|
|
24
|
+
const mod = await import("pg-boss");
|
|
25
|
+
const ctor = mod.PgBoss ?? (typeof mod.default === "function" ? mod.default : mod.default.PgBoss);
|
|
26
|
+
return ctor;
|
|
27
|
+
} catch {
|
|
28
|
+
throw new Error("[EFC] pg-boss is not installed. Run: npm install pg-boss");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
var closeImpl = null;
|
|
32
|
+
async function closePgBoss() {
|
|
33
|
+
const close = closeImpl;
|
|
34
|
+
closeImpl = null;
|
|
35
|
+
await close?.();
|
|
36
|
+
}
|
|
37
|
+
async function initPgBoss(opts) {
|
|
38
|
+
const PgBoss = await loadPgBoss();
|
|
39
|
+
const boss = new PgBoss({
|
|
40
|
+
connectionString: opts.connectionString,
|
|
41
|
+
...opts.schema !== void 0 && { schema: opts.schema }
|
|
42
|
+
});
|
|
43
|
+
boss.on("error", (err) => getLogger().error({ err }, "pg-boss error"));
|
|
44
|
+
await boss.start();
|
|
45
|
+
const queueFor = (name) => `${opts.queue}.${name}`;
|
|
46
|
+
const wantedSchedules = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const def of taskRegistry.values()) {
|
|
48
|
+
const queue = queueFor(def.name);
|
|
49
|
+
await boss.createQueue(queue, retryOptions(def));
|
|
50
|
+
await boss.work(
|
|
51
|
+
queue,
|
|
52
|
+
{ localConcurrency: def.options.concurrency ?? opts.concurrency },
|
|
53
|
+
async (jobs) => {
|
|
54
|
+
for (const job of jobs) {
|
|
55
|
+
const payload = unbox(job.data);
|
|
56
|
+
try {
|
|
57
|
+
if (def.options.thread && def.filePath) await runInThread(def.filePath, payload);
|
|
58
|
+
else await def.handler(payload);
|
|
59
|
+
getLogger().info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);
|
|
60
|
+
} catch (err) {
|
|
61
|
+
getLogger().error(
|
|
62
|
+
{ task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },
|
|
63
|
+
`Task ${def.name} failed`
|
|
64
|
+
);
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
if (def.options.schedule) {
|
|
71
|
+
wantedSchedules.add(queue);
|
|
72
|
+
await boss.schedule(
|
|
73
|
+
queue,
|
|
74
|
+
def.options.schedule,
|
|
75
|
+
{},
|
|
76
|
+
{
|
|
77
|
+
...retryOptions(def),
|
|
78
|
+
...def.options.timezone !== void 0 && { tz: def.options.timezone }
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const s of await boss.getSchedules()) {
|
|
84
|
+
if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {
|
|
85
|
+
await boss.unschedule(s.name);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (wantedSchedules.size > 0) getLogger().info(`Scheduled ${wantedSchedules.size} cron task(s)`);
|
|
89
|
+
setEnqueueImpl(async (name, payload) => {
|
|
90
|
+
const def = taskRegistry.get(name);
|
|
91
|
+
if (!def) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`[EFC] Cannot enqueue unknown task: "${name}". Is the task file in tasksDir?`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
let timer;
|
|
97
|
+
const timeout = new Promise((_, reject) => {
|
|
98
|
+
timer = setTimeout(
|
|
99
|
+
() => reject(
|
|
100
|
+
new Error(
|
|
101
|
+
`[EFC] Cannot enqueue task "${name}": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`
|
|
102
|
+
)
|
|
103
|
+
),
|
|
104
|
+
opts.enqueueTimeout
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
try {
|
|
108
|
+
await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);
|
|
109
|
+
} catch (err) {
|
|
110
|
+
if (err instanceof Error && err.message.startsWith("[EFC]")) throw err;
|
|
111
|
+
throw new Error(
|
|
112
|
+
`[EFC] Cannot enqueue task "${name}": ${err instanceof Error ? err.message : String(err)}`
|
|
113
|
+
);
|
|
114
|
+
} finally {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
closeImpl = async () => {
|
|
119
|
+
await boss.stop({ graceful: true, timeout: 1e4 });
|
|
120
|
+
};
|
|
121
|
+
getLogger().info("pg-boss backend initialised");
|
|
122
|
+
}
|
|
123
|
+
async function pgBossAdmin(connectionString, queue = "efc", schema = "pgboss") {
|
|
124
|
+
const PgBoss = await loadPgBoss();
|
|
125
|
+
const boss = new PgBoss({
|
|
126
|
+
connectionString,
|
|
127
|
+
schema,
|
|
128
|
+
supervise: false,
|
|
129
|
+
schedule: false,
|
|
130
|
+
migrate: false
|
|
131
|
+
});
|
|
132
|
+
boss.on("error", () => {
|
|
133
|
+
});
|
|
134
|
+
await boss.start();
|
|
135
|
+
const db = boss.getDb();
|
|
136
|
+
const prefix = `${queue}.`;
|
|
137
|
+
const taskOf = (name) => name.slice(prefix.length);
|
|
138
|
+
const table = `"${schema.replace(/"/g, '""')}".job`;
|
|
139
|
+
const failedRows = async (ids, limit = 1e4) => (await db.executeSql(
|
|
140
|
+
`SELECT id, name, data, output, retry_count, completed_on FROM ${table}
|
|
141
|
+
WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? "AND id = ANY($3::uuid[])" : ""}
|
|
142
|
+
ORDER BY completed_on DESC NULLS LAST LIMIT $2`,
|
|
143
|
+
ids.length ? [prefix, limit, ids] : [prefix, limit]
|
|
144
|
+
)).rows;
|
|
145
|
+
const byQueue = (rows) => {
|
|
146
|
+
const groups = /* @__PURE__ */ new Map();
|
|
147
|
+
for (const r of rows) groups.set(r.name, [...groups.get(r.name) ?? [], r.id]);
|
|
148
|
+
return groups;
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
async counts() {
|
|
152
|
+
const { rows } = await db.executeSql(
|
|
153
|
+
`SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,
|
|
154
|
+
[prefix]
|
|
155
|
+
);
|
|
156
|
+
return Object.fromEntries(
|
|
157
|
+
rows.map((r) => [r.state, r.n])
|
|
158
|
+
);
|
|
159
|
+
},
|
|
160
|
+
async listFailed({ limit, name }) {
|
|
161
|
+
const rows = (await failedRows([], name ? 1e4 : limit)).filter(
|
|
162
|
+
(r) => !name || taskOf(r.name) === name
|
|
163
|
+
);
|
|
164
|
+
return rows.slice(0, limit).map(
|
|
165
|
+
(r) => ({
|
|
166
|
+
id: r.id,
|
|
167
|
+
name: taskOf(r.name),
|
|
168
|
+
data: unbox(r.data),
|
|
169
|
+
error: String(r.output?.message ?? r.output ?? ""),
|
|
170
|
+
attempts: r.retry_count + 1,
|
|
171
|
+
failedAt: r.completed_on ?? void 0
|
|
172
|
+
})
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
async retry(ids) {
|
|
176
|
+
const rows = await failedRows(ids);
|
|
177
|
+
for (const [q, list] of byQueue(rows)) await boss.retry(q, list);
|
|
178
|
+
return rows.length;
|
|
179
|
+
},
|
|
180
|
+
async purge(ids) {
|
|
181
|
+
const rows = await failedRows(ids);
|
|
182
|
+
for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);
|
|
183
|
+
return rows.length;
|
|
184
|
+
},
|
|
185
|
+
async close() {
|
|
186
|
+
await boss.stop({ graceful: false });
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export {
|
|
192
|
+
closePgBoss,
|
|
193
|
+
initPgBoss,
|
|
194
|
+
pgBossAdmin
|
|
195
|
+
};
|
|
196
|
+
//# sourceMappingURL=chunk-UTM4PEVG.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/tasks/pgboss-backend.ts"],"sourcesContent":["import type { PgBoss as PgBossType } from 'pg-boss';\nimport type { TaskDefinition } from '../types.js';\nimport { taskRegistry, setEnqueueImpl } from './index.js';\nimport { runInThread } from './thread-runner.js';\nimport { getLogger } from '../logger.js';\nimport type { FailedJob, TaskAdmin } from './admin.js';\n\n// pg-boss keeps one queue per job name, so each task gets its own pg-boss queue\n// named `<tasks.queue>.<TaskName>` — the prefix keeps apps sharing a database apart.\n\ninterface PgBossOpts {\n connectionString: string;\n concurrency: number;\n enqueueTimeout: number;\n queue: string;\n schema?: string;\n}\n\n// pg-boss payloads must be objects; other values are boxed so any enqueue() payload works.\nconst BOX = '__efcValue';\nconst box = (payload: unknown): object =>\n payload !== null && typeof payload === 'object' && !Array.isArray(payload)\n ? (payload as object)\n : { [BOX]: payload };\nconst unbox = (data: unknown): unknown =>\n data !== null && typeof data === 'object' && BOX in data\n ? (data as Record<string, unknown>)[BOX]\n : data;\n\nconst retryOptions = (def: TaskDefinition) => ({\n // pg-boss's retryLimit counts re-runs, exactly like EFC's `retries`.\n retryLimit: def.options.retries ?? 3,\n retryDelay: 1,\n retryBackoff: (def.options.backoff ?? 'exponential') === 'exponential',\n});\n\nasync function loadPgBoss(): Promise<typeof PgBossType> {\n try {\n const mod = (await import('pg-boss')) as unknown as {\n PgBoss?: typeof PgBossType;\n default?: typeof PgBossType | { PgBoss: typeof PgBossType };\n };\n const ctor =\n mod.PgBoss ??\n (typeof mod.default === 'function'\n ? mod.default\n : (mod.default as { PgBoss: typeof PgBossType }).PgBoss);\n return ctor;\n } catch {\n throw new Error('[EFC] pg-boss is not installed. Run: npm install pg-boss');\n }\n}\n\nlet closeImpl: (() => Promise<void>) | null = null;\n\nexport async function closePgBoss(): Promise<void> {\n const close = closeImpl;\n closeImpl = null;\n await close?.();\n}\n\nexport async function initPgBoss(opts: PgBossOpts): Promise<void> {\n const PgBoss = await loadPgBoss();\n const boss = new PgBoss({\n connectionString: opts.connectionString,\n ...(opts.schema !== undefined && { schema: opts.schema }),\n });\n boss.on('error', (err: Error) => getLogger().error({ err }, 'pg-boss error'));\n await boss.start();\n\n const queueFor = (name: string) => `${opts.queue}.${name}`;\n const wantedSchedules = new Set<string>();\n\n for (const def of taskRegistry.values()) {\n const queue = queueFor(def.name);\n await boss.createQueue(queue, retryOptions(def));\n await boss.work(\n queue,\n { localConcurrency: def.options.concurrency ?? opts.concurrency },\n async (jobs: { id: string; data: unknown; retryCount: number }[]) => {\n for (const job of jobs) {\n const payload = unbox(job.data);\n try {\n if (def.options.thread && def.filePath) await runInThread(def.filePath, payload);\n else await def.handler(payload);\n getLogger().info({ task: def.name, jobId: job.id }, `Task ${def.name} completed`);\n } catch (err) {\n getLogger().error(\n { task: def.name, jobId: job.id, attempt: job.retryCount + 1, err },\n `Task ${def.name} failed`,\n );\n throw err;\n }\n }\n },\n );\n if (def.options.schedule) {\n wantedSchedules.add(queue);\n await boss.schedule(\n queue,\n def.options.schedule,\n {},\n {\n ...retryOptions(def),\n ...(def.options.timezone !== undefined && { tz: def.options.timezone }),\n },\n );\n }\n }\n\n // Drop schedules for tasks that lost their `schedule` or no longer exist.\n for (const s of await boss.getSchedules()) {\n if (s.name.startsWith(`${opts.queue}.`) && !wantedSchedules.has(s.name)) {\n await boss.unschedule(s.name);\n }\n }\n if (wantedSchedules.size > 0) getLogger().info(`Scheduled ${wantedSchedules.size} cron task(s)`);\n\n setEnqueueImpl(async (name, payload) => {\n const def = taskRegistry.get(name);\n if (!def) {\n throw new Error(\n `[EFC] Cannot enqueue unknown task: \"${name}\". Is the task file in tasksDir?`,\n );\n }\n let timer: NodeJS.Timeout | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(\n () =>\n reject(\n new Error(\n `[EFC] Cannot enqueue task \"${name}\": PostgreSQL did not respond within ${opts.enqueueTimeout}ms`,\n ),\n ),\n opts.enqueueTimeout,\n );\n });\n try {\n await Promise.race([boss.send(queueFor(name), box(payload), retryOptions(def)), timeout]);\n } catch (err) {\n if (err instanceof Error && err.message.startsWith('[EFC]')) throw err;\n throw new Error(\n `[EFC] Cannot enqueue task \"${name}\": ${err instanceof Error ? err.message : String(err)}`,\n );\n } finally {\n clearTimeout(timer);\n }\n });\n\n closeImpl = async () => {\n // graceful: lets in-flight jobs finish before the connection pool closes.\n await boss.stop({ graceful: true, timeout: 10_000 });\n };\n\n getLogger().info('pg-boss backend initialised');\n}\n\n/** Dead-letter inspection for `efc tasks failed | retry | purge` on pg-boss. */\nexport async function pgBossAdmin(\n connectionString: string,\n queue = 'efc',\n schema = 'pgboss',\n): Promise<TaskAdmin> {\n const PgBoss = await loadPgBoss();\n // No supervision or cron here: this instance only reads and settles jobs.\n const boss = new PgBoss({\n connectionString,\n schema,\n supervise: false,\n schedule: false,\n migrate: false,\n });\n boss.on('error', () => {});\n await boss.start();\n const db = boss.getDb();\n const prefix = `${queue}.`;\n const taskOf = (name: string) => name.slice(prefix.length);\n const table = `\"${schema.replace(/\"/g, '\"\"')}\".job`;\n\n const failedRows = async (ids: string[], limit = 10_000) =>\n (\n await db.executeSql(\n `SELECT id, name, data, output, retry_count, completed_on FROM ${table}\n WHERE state = 'failed' AND starts_with(name, $1) ${ids.length ? 'AND id = ANY($3::uuid[])' : ''}\n ORDER BY completed_on DESC NULLS LAST LIMIT $2`,\n ids.length ? [prefix, limit, ids] : [prefix, limit],\n )\n ).rows as {\n id: string;\n name: string;\n data: unknown;\n output: unknown;\n retry_count: number;\n completed_on: Date | null;\n }[];\n\n const byQueue = (rows: { id: string; name: string }[]) => {\n const groups = new Map<string, string[]>();\n for (const r of rows) groups.set(r.name, [...(groups.get(r.name) ?? []), r.id]);\n return groups;\n };\n\n return {\n async counts() {\n const { rows } = await db.executeSql(\n `SELECT state, count(*)::int AS n FROM ${table} WHERE starts_with(name, $1) GROUP BY state`,\n [prefix],\n );\n return Object.fromEntries(\n (rows as { state: string; n: number }[]).map((r) => [r.state, r.n]),\n );\n },\n async listFailed({ limit, name }) {\n const rows = (await failedRows([], name ? 10_000 : limit)).filter(\n (r) => !name || taskOf(r.name) === name,\n );\n return rows.slice(0, limit).map(\n (r): FailedJob => ({\n id: r.id,\n name: taskOf(r.name),\n data: unbox(r.data),\n error: String((r.output as { message?: string } | null)?.message ?? r.output ?? ''),\n attempts: r.retry_count + 1,\n failedAt: r.completed_on ?? undefined,\n }),\n );\n },\n async retry(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.retry(q, list);\n return rows.length;\n },\n async purge(ids) {\n const rows = await failedRows(ids);\n for (const [q, list] of byQueue(rows)) await boss.deleteJob(q, list);\n return rows.length;\n },\n async close() {\n await boss.stop({ graceful: false });\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;AAmBA,IAAM,MAAM;AACZ,IAAM,MAAM,CAAC,YACX,YAAY,QAAQ,OAAO,YAAY,YAAY,CAAC,MAAM,QAAQ,OAAO,IACpE,UACD,EAAE,CAAC,GAAG,GAAG,QAAQ;AACvB,IAAM,QAAQ,CAAC,SACb,SAAS,QAAQ,OAAO,SAAS,YAAY,OAAO,OAC/C,KAAiC,GAAG,IACrC;AAEN,IAAM,eAAe,CAAC,SAAyB;AAAA;AAAA,EAE7C,YAAY,IAAI,QAAQ,WAAW;AAAA,EACnC,YAAY;AAAA,EACZ,eAAe,IAAI,QAAQ,WAAW,mB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