@cnstra/pg-boss 0.1.0 → 0.2.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 +44 -15
- package/dist/chunk-2JTKI4GS.mjs +10 -0
- package/dist/chunk-2JTKI4GS.mjs.map +1 -0
- package/dist/chunk-XJFC72LU.mjs +35 -0
- package/dist/chunk-XJFC72LU.mjs.map +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +61 -0
- package/dist/cli.js.map +1 -0
- package/dist/cli.mjs +40 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.d.mts +33 -40
- package/dist/index.d.ts +33 -40
- package/dist/index.js +75 -73
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +78 -73
- package/dist/index.mjs.map +1 -1
- package/dist/postgres-progress.d.mts +75 -0
- package/dist/postgres-progress.d.ts +75 -0
- package/dist/postgres-progress.js +142 -0
- package/dist/postgres-progress.js.map +1 -0
- package/dist/postgres-progress.mjs +94 -0
- package/dist/postgres-progress.mjs.map +1 -0
- package/package.json +39 -7
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_TABLE,
|
|
3
|
+
assertSafeTable,
|
|
4
|
+
ensureSchema,
|
|
5
|
+
pgBossProgressSchemaSql
|
|
6
|
+
} from "./chunk-XJFC72LU.mjs";
|
|
7
|
+
import {
|
|
8
|
+
__name,
|
|
9
|
+
__publicField
|
|
10
|
+
} from "./chunk-2JTKI4GS.mjs";
|
|
11
|
+
|
|
12
|
+
// src/CNSPgBossProgressRepository.ts
|
|
13
|
+
import { Pool } from "pg";
|
|
14
|
+
var _CNSPgBossProgressRepository = class _CNSPgBossProgressRepository {
|
|
15
|
+
constructor(opts) {
|
|
16
|
+
__publicField(this, "pool");
|
|
17
|
+
__publicField(this, "ownsPool");
|
|
18
|
+
__publicField(this, "table");
|
|
19
|
+
__publicField(this, "autoMigrate");
|
|
20
|
+
__publicField(this, "ready");
|
|
21
|
+
var _a, _b;
|
|
22
|
+
if (opts.pool) {
|
|
23
|
+
this.pool = opts.pool;
|
|
24
|
+
this.ownsPool = false;
|
|
25
|
+
} else if (opts.connectionString) {
|
|
26
|
+
this.pool = new Pool({
|
|
27
|
+
connectionString: opts.connectionString
|
|
28
|
+
});
|
|
29
|
+
this.ownsPool = true;
|
|
30
|
+
} else {
|
|
31
|
+
throw new Error("[@cnstra/pg-boss] CNSPgBossProgressRepository needs `connectionString` or `pool`");
|
|
32
|
+
}
|
|
33
|
+
this.table = assertSafeTable((_a = opts.table) != null ? _a : DEFAULT_TABLE);
|
|
34
|
+
this.autoMigrate = (_b = opts.autoMigrate) != null ? _b : true;
|
|
35
|
+
}
|
|
36
|
+
ensure() {
|
|
37
|
+
var _a;
|
|
38
|
+
if (!this.autoMigrate) return Promise.resolve();
|
|
39
|
+
return (_a = this.ready) != null ? _a : this.ready = ensureSchema(this.pool, this.table);
|
|
40
|
+
}
|
|
41
|
+
async save(key, progress) {
|
|
42
|
+
await this.ensure();
|
|
43
|
+
await this.pool.query(`INSERT INTO ${this.table} (key, progress, updated_at)
|
|
44
|
+
VALUES ($1, $2::jsonb, now())
|
|
45
|
+
ON CONFLICT (key) DO UPDATE SET progress = EXCLUDED.progress, updated_at = now()`, [
|
|
46
|
+
key,
|
|
47
|
+
JSON.stringify(progress)
|
|
48
|
+
]);
|
|
49
|
+
}
|
|
50
|
+
async load(key) {
|
|
51
|
+
var _a;
|
|
52
|
+
await this.ensure();
|
|
53
|
+
const res = await this.pool.query(`SELECT progress FROM ${this.table} WHERE key = $1`, [
|
|
54
|
+
key
|
|
55
|
+
]);
|
|
56
|
+
return (_a = res.rows[0]) == null ? void 0 : _a.progress;
|
|
57
|
+
}
|
|
58
|
+
async delete(key) {
|
|
59
|
+
await this.ensure();
|
|
60
|
+
await this.pool.query(`DELETE FROM ${this.table} WHERE key = $1`, [
|
|
61
|
+
key
|
|
62
|
+
]);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Prune rows not touched for `olderThanMs` — the safety net for jobs that failed
|
|
66
|
+
* PERMANENTLY (retries exhausted): their frontier row is never `delete`d (pg-boss
|
|
67
|
+
* has no terminal-failure hook), so it would leak. A still-retrying job keeps its
|
|
68
|
+
* `updated_at` fresh, so choose a threshold comfortably larger than your longest
|
|
69
|
+
* retry backoff window and run this on a schedule. Returns the number of rows removed.
|
|
70
|
+
*/
|
|
71
|
+
async deleteStale(olderThanMs) {
|
|
72
|
+
var _a;
|
|
73
|
+
await this.ensure();
|
|
74
|
+
const res = await this.pool.query(`DELETE FROM ${this.table}
|
|
75
|
+
WHERE updated_at < now() - ($1::bigint * interval '1 millisecond')`, [
|
|
76
|
+
Math.max(0, Math.floor(olderThanMs))
|
|
77
|
+
]);
|
|
78
|
+
return (_a = res.rowCount) != null ? _a : 0;
|
|
79
|
+
}
|
|
80
|
+
/** Close the owned pool (no-op when a pool was injected). */
|
|
81
|
+
async close() {
|
|
82
|
+
if (this.ownsPool) await this.pool.end();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
__name(_CNSPgBossProgressRepository, "CNSPgBossProgressRepository");
|
|
86
|
+
var CNSPgBossProgressRepository = _CNSPgBossProgressRepository;
|
|
87
|
+
export {
|
|
88
|
+
CNSPgBossProgressRepository,
|
|
89
|
+
DEFAULT_TABLE,
|
|
90
|
+
assertSafeTable,
|
|
91
|
+
ensureSchema,
|
|
92
|
+
pgBossProgressSchemaSql
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=postgres-progress.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/CNSPgBossProgressRepository.ts"],"sourcesContent":["/**\n * Postgres-backed resume-progress store for pg-boss, keyed by the pg-boss **job id**.\n *\n * In pg-boss a run = stimulation = job (one entity), so the checkpoint is keyed by\n * `job.id`; there is no separate run store. pg-boss can't hold arbitrary per-hop\n * progress on its job row (its `Job` has data/output/signal but no mutable progress\n * field), so the frontier lives in its own table (`cns_pgboss_progress`, default) on\n * the same Postgres. One upserted row per key; dropped when the job completes.\n *\n * OPTIONAL: this is an opt-in export (`@cnstra/pg-boss/postgres-progress`) — importing\n * `@cnstra/pg-boss` (worker/enqueue) does NOT pull in `pg`. It's just one\n * `ICNSProgressRepository`; the progress store is independent of the broker, so you can\n * run pg-boss with `@cnstra/progress-redis` (or any impl) instead.\n */\nimport { Pool } from 'pg';\nimport type { ICNSProgressRepository } from '@cnstra/persist';\nimport type { TCNSProgress } from '@cnstra/persist-dto';\nimport { DEFAULT_TABLE, assertSafeTable, ensureSchema } from './migrate';\n\nexport interface TCNSPgBossProgressRepositoryOptions {\n /** Connection string (a Pool is created and owned by the repo). */\n connectionString?: string;\n /** Or inject an existing pg Pool (not closed by `close()`). */\n pool?: Pool;\n /** Table name (default `cns_pgboss_progress`). */\n table?: string;\n /** Lazily `ensureSchema` on first use (default true). Set false to manage migrations yourself. */\n autoMigrate?: boolean;\n}\n\nexport class CNSPgBossProgressRepository implements ICNSProgressRepository {\n private readonly pool: Pool;\n private readonly ownsPool: boolean;\n private readonly table: string;\n private readonly autoMigrate: boolean;\n private ready: Promise<void> | undefined;\n\n constructor(opts: TCNSPgBossProgressRepositoryOptions) {\n if (opts.pool) {\n this.pool = opts.pool;\n this.ownsPool = false;\n } else if (opts.connectionString) {\n this.pool = new Pool({ connectionString: opts.connectionString });\n this.ownsPool = true;\n } else {\n throw new Error(\n '[@cnstra/pg-boss] CNSPgBossProgressRepository needs `connectionString` or `pool`'\n );\n }\n this.table = assertSafeTable(opts.table ?? DEFAULT_TABLE);\n this.autoMigrate = opts.autoMigrate ?? true;\n }\n\n private ensure(): Promise<void> {\n if (!this.autoMigrate) return Promise.resolve();\n return (this.ready ??= ensureSchema(this.pool, this.table));\n }\n\n async save(key: string, progress: TCNSProgress): Promise<void> {\n await this.ensure();\n await this.pool.query(\n `INSERT INTO ${this.table} (key, progress, updated_at)\n VALUES ($1, $2::jsonb, now())\n ON CONFLICT (key) DO UPDATE SET progress = EXCLUDED.progress, updated_at = now()`,\n [key, JSON.stringify(progress)]\n );\n }\n\n async load(key: string): Promise<TCNSProgress | undefined> {\n await this.ensure();\n const res = await this.pool.query<{ progress: TCNSProgress }>(\n `SELECT progress FROM ${this.table} WHERE key = $1`,\n [key]\n );\n return res.rows[0]?.progress;\n }\n\n async delete(key: string): Promise<void> {\n await this.ensure();\n await this.pool.query(`DELETE FROM ${this.table} WHERE key = $1`, [key]);\n }\n\n /**\n * Prune rows not touched for `olderThanMs` — the safety net for jobs that failed\n * PERMANENTLY (retries exhausted): their frontier row is never `delete`d (pg-boss\n * has no terminal-failure hook), so it would leak. A still-retrying job keeps its\n * `updated_at` fresh, so choose a threshold comfortably larger than your longest\n * retry backoff window and run this on a schedule. Returns the number of rows removed.\n */\n async deleteStale(olderThanMs: number): Promise<number> {\n await this.ensure();\n const res = await this.pool.query(\n `DELETE FROM ${this.table}\n WHERE updated_at < now() - ($1::bigint * interval '1 millisecond')`,\n [Math.max(0, Math.floor(olderThanMs))]\n );\n return res.rowCount ?? 0;\n }\n\n /** Close the owned pool (no-op when a pool was injected). */\n async close(): Promise<void> {\n if (this.ownsPool) await this.pool.end();\n }\n}\n"],"mappings":";;;;;;;;;;;;AAcA,SAASA,YAAY;AAgBd,IAAMC,+BAAN,MAAMA,6BAAAA;EAOT,YAAYC,MAA2C;AANtCC;AACAC;AACAC;AACAC;AACTC;AAnCZ;AAsCQ,QAAIL,KAAKC,MAAM;AACX,WAAKA,OAAOD,KAAKC;AACjB,WAAKC,WAAW;IACpB,WAAWF,KAAKM,kBAAkB;AAC9B,WAAKL,OAAO,IAAIM,KAAK;QAAED,kBAAkBN,KAAKM;MAAiB,CAAA;AAC/D,WAAKJ,WAAW;IACpB,OAAO;AACH,YAAM,IAAIM,MACN,kFAAA;IAER;AACA,SAAKL,QAAQM,iBAAgBT,UAAKG,UAALH,YAAcU,aAAAA;AAC3C,SAAKN,eAAcJ,UAAKI,gBAALJ,YAAoB;EAC3C;EAEQW,SAAwB;AArDpC;AAsDQ,QAAI,CAAC,KAAKP,YAAa,QAAOQ,QAAQC,QAAO;AAC7C,YAAQ,UAAKR,UAAL,iBAAKA,QAAUS,aAAa,KAAKb,MAAM,KAAKE,KAAK;EAC7D;EAEA,MAAMY,KAAKC,KAAaC,UAAuC;AAC3D,UAAM,KAAKN,OAAM;AACjB,UAAM,KAAKV,KAAKiB,MACZ,eAAe,KAAKf,KAAK;;gGAGzB;MAACa;MAAKG,KAAKC,UAAUH,QAAAA;KAAU;EAEvC;EAEA,MAAMI,KAAKL,KAAgD;AApE/D;AAqEQ,UAAM,KAAKL,OAAM;AACjB,UAAMW,MAAM,MAAM,KAAKrB,KAAKiB,MACxB,wBAAwB,KAAKf,KAAK,mBAClC;MAACa;KAAI;AAET,YAAOM,SAAIC,KAAK,CAAA,MAATD,mBAAaL;EACxB;EAEA,MAAMO,OAAOR,KAA4B;AACrC,UAAM,KAAKL,OAAM;AACjB,UAAM,KAAKV,KAAKiB,MAAM,eAAe,KAAKf,KAAK,mBAAmB;MAACa;KAAI;EAC3E;;;;;;;;EASA,MAAMS,YAAYC,aAAsC;AAzF5D;AA0FQ,UAAM,KAAKf,OAAM;AACjB,UAAMW,MAAM,MAAM,KAAKrB,KAAKiB,MACxB,eAAe,KAAKf,KAAK;kFAEzB;MAACwB,KAAKC,IAAI,GAAGD,KAAKE,MAAMH,WAAAA,CAAAA;KAAc;AAE1C,YAAOJ,SAAIQ,aAAJR,YAAgB;EAC3B;;EAGA,MAAMS,QAAuB;AACzB,QAAI,KAAK7B,SAAU,OAAM,KAAKD,KAAK+B,IAAG;EAC1C;AACJ;AAzEajC;AAAN,IAAMA,8BAAN;","names":["Pool","CNSPgBossProgressRepository","opts","pool","ownsPool","table","autoMigrate","ready","connectionString","Pool","Error","assertSafeTable","DEFAULT_TABLE","ensure","Promise","resolve","ensureSchema","save","key","progress","query","JSON","stringify","load","res","rows","delete","deleteStale","olderThanMs","Math","max","floor","rowCount","close","end"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cnstra/pg-boss",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "pg-boss (Postgres) integration for CNStra: run stimulations as jobs and stream per-hop progress",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cnstra",
|
|
@@ -18,34 +18,66 @@
|
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
19
19
|
"import": "./dist/index.mjs",
|
|
20
20
|
"require": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./postgres-progress": {
|
|
23
|
+
"types": "./dist/postgres-progress.d.ts",
|
|
24
|
+
"import": "./dist/postgres-progress.mjs",
|
|
25
|
+
"require": "./dist/postgres-progress.js"
|
|
21
26
|
}
|
|
22
27
|
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"cnstra-pgboss-progress": "./dist/cli.js"
|
|
30
|
+
},
|
|
23
31
|
"sideEffects": false,
|
|
24
32
|
"files": [
|
|
25
33
|
"dist"
|
|
26
34
|
],
|
|
27
35
|
"scripts": {
|
|
28
36
|
"build": "tsup",
|
|
37
|
+
"test": "jest",
|
|
29
38
|
"test:types": "tsc --noEmit",
|
|
30
39
|
"clean": "rm -rf dist"
|
|
31
40
|
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@cnstra/types": "^0.1.0"
|
|
43
|
+
},
|
|
32
44
|
"peerDependencies": {
|
|
33
45
|
"@cnstra/core": "^2.4.0",
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"pg
|
|
38
|
-
"optional": true
|
|
39
|
-
}
|
|
46
|
+
"@cnstra/persist": "^0.1.0",
|
|
47
|
+
"@cnstra/persist-dto": "^0.1.0",
|
|
48
|
+
"pg-boss": ">=9",
|
|
49
|
+
"pg": ">=8"
|
|
40
50
|
},
|
|
41
51
|
"devDependencies": {
|
|
42
52
|
"@cnstra/core": "^2.4.0",
|
|
53
|
+
"@cnstra/persist": "^0.1.0",
|
|
54
|
+
"@cnstra/persist-dto": "^0.1.0",
|
|
55
|
+
"@types/jest": "29.5.12",
|
|
56
|
+
"@types/pg": "^8.11.0",
|
|
57
|
+
"jest": "29.7.0",
|
|
58
|
+
"pg": "^8.11.0",
|
|
59
|
+
"ts-jest": "29.1.2",
|
|
43
60
|
"tsup": "8.5.1",
|
|
44
61
|
"typescript": "5.7.2"
|
|
45
62
|
},
|
|
63
|
+
"peerDependenciesMeta": {
|
|
64
|
+
"pg-boss": {
|
|
65
|
+
"optional": true
|
|
66
|
+
},
|
|
67
|
+
"pg": {
|
|
68
|
+
"optional": true
|
|
69
|
+
}
|
|
70
|
+
},
|
|
46
71
|
"author": "Andrei Baikov <abaikov23@gmail.com>",
|
|
47
72
|
"license": "MIT",
|
|
48
73
|
"publishConfig": {
|
|
49
74
|
"access": "public"
|
|
75
|
+
},
|
|
76
|
+
"typesVersions": {
|
|
77
|
+
"*": {
|
|
78
|
+
"postgres-progress": [
|
|
79
|
+
"./dist/postgres-progress.d.ts"
|
|
80
|
+
]
|
|
81
|
+
}
|
|
50
82
|
}
|
|
51
83
|
}
|