@fungi.computer/watchdog 0.1.5 → 0.1.6

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/d1.js CHANGED
@@ -10,6 +10,7 @@ export function createD1Watchdog(options) {
10
10
  .bind(...statement.values);
11
11
  const runtime = createAsyncRuntime({
12
12
  dialect: "sqlite",
13
+ shared: options.database,
13
14
  clock: options.now === undefined ? "unixepoch('subsec')*1000" : undefined,
14
15
  query: async (statement) => (await prepare(statement).all()).results,
15
16
  }, options);
@@ -8,6 +8,11 @@ export type Statement = Readonly<{
8
8
  export type SqlDatabase = Readonly<{
9
9
  dialect: "sqlite" | "postgres";
10
10
  clock?: string | undefined;
11
+ /**
12
+ * The native database object runtimes over the same database share. Its
13
+ * schema is verified once for all of them; absent, each store verifies its own.
14
+ */
15
+ shared?: object | undefined;
11
16
  query: (statement: Statement) => Promise<readonly SqlRow[]>;
12
17
  }>;
13
18
  export declare const columns = "sequence, job_id, queue_name, lane, priority, payload_json, not_before, attempt_count,\n recovery_max, recovery_window_ms, recovery_window_started_at, state, outcome, claim_token, recovery_count, cancellation_id, failure_reason";
@@ -15,6 +15,10 @@ export const attempt = (run) => Effect.tryPromise({
15
15
  ? error
16
16
  : new WatchdogError("store_unavailable", error),
17
17
  });
18
+ // One schema verification per native database, shared by every runtime opened
19
+ // on it and by their concurrent first queries. A failed verification is
20
+ // forgotten, so the next query retries it after a migration lands.
21
+ const verifiedSchemas = new WeakMap();
18
22
  export function openAsyncStore(database, now) {
19
23
  // The database checks wall time when the mutation executes, so a delayed
20
24
  // renewal cannot resurrect a lease that expired while the request was in flight.
@@ -26,28 +30,47 @@ export function openAsyncStore(database, now) {
26
30
  // locked head is skipped, so each claimer takes the next job. SQLite and D1
27
31
  // have one writer and no row locks.
28
32
  const skipLocked = database.dialect === "postgres" ? " FOR UPDATE SKIP LOCKED" : "";
29
- let schemaChecked = false;
30
- const query = async (text, values = []) => {
31
- if (!schemaChecked) {
32
- const current = await database.query({
33
- text: database.dialect === "postgres"
34
- ? "SELECT 1 AS current FROM pg_constraint WHERE conrelid = 'watchdog_jobs'::regclass AND conname = 'watchdog_execution_state'"
35
- : "SELECT 1 AS current FROM sqlite_schema WHERE name = 'watchdog_jobs' AND instr(sql, 'watchdog_execution_state') > 0",
33
+ const verifySchema = async () => {
34
+ const current = await database.query({
35
+ text: database.dialect === "postgres"
36
+ ? "SELECT 1 AS current FROM pg_constraint WHERE conrelid = 'watchdog_jobs'::regclass AND conname = 'watchdog_execution_state'"
37
+ : "SELECT 1 AS current FROM sqlite_schema WHERE name = 'watchdog_jobs' AND instr(sql, 'watchdog_execution_state') > 0",
38
+ values: [],
39
+ });
40
+ if (current.length !== 1)
41
+ throw new WatchdogError("store_invalid", new Error("Obsolete Watchdog execution schema"));
42
+ try {
43
+ await database.query({
44
+ text: `SELECT ${columns} FROM watchdog_jobs LIMIT 0`,
36
45
  values: [],
37
46
  });
38
- if (current.length !== 1)
39
- throw new WatchdogError("store_invalid", new Error("Obsolete Watchdog execution schema"));
40
- try {
41
- await database.query({
42
- text: `SELECT ${columns} FROM watchdog_jobs LIMIT 0`,
43
- values: [],
44
- });
45
- }
46
- catch (error) {
47
- throw new WatchdogError("store_invalid", error);
48
- }
49
- schemaChecked = true;
50
47
  }
48
+ catch (error) {
49
+ throw new WatchdogError("store_invalid", error);
50
+ }
51
+ };
52
+ let ownSchema;
53
+ const ensureSchema = () => {
54
+ const known = database.shared === undefined
55
+ ? ownSchema
56
+ : verifiedSchemas.get(database.shared);
57
+ if (known !== undefined)
58
+ return known;
59
+ const pending = verifySchema().catch((error) => {
60
+ if (database.shared === undefined)
61
+ ownSchema = undefined;
62
+ else
63
+ verifiedSchemas.delete(database.shared);
64
+ throw error;
65
+ });
66
+ if (database.shared === undefined)
67
+ ownSchema = pending;
68
+ else
69
+ verifiedSchemas.set(database.shared, pending);
70
+ return pending;
71
+ };
72
+ const query = async (text, values = []) => {
73
+ await ensureSchema();
51
74
  return database.query({ text, values });
52
75
  };
53
76
  const row = async (id) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fungi.computer/watchdog",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "author": "Levi <levi@technomancy.dev>",
5
5
  "description": "Payload-blind durable work scheduling and settlement core",
6
6
  "type": "module",