@protobase/server 0.131.1 → 0.132.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/dist/jobs/functions-sql.js +37 -12
- package/dist/jobs/jobs-module.js +5 -4
- package/dist/jobs/runtime.js +9 -5
- package/dist/package.js +1 -1
- package/package.json +6 -6
|
@@ -1,31 +1,44 @@
|
|
|
1
1
|
import { organizationKey } from "./job-data.js";
|
|
2
2
|
/** The comment each Protobase function in the jobs schema carries, which the startup check reads. */
|
|
3
|
-
const functionsComment = `Protobase jobs
|
|
3
|
+
const functionsComment = `Protobase jobs 5`;
|
|
4
4
|
/**
|
|
5
5
|
* `jobs.enqueue(name, payload, run_at, key, group_key, priority)`: a job from SQL, such as a trigger, in the caller's
|
|
6
|
-
* transaction. It inserts what pg-boss's `send()` does, with the queue's retry settings; it
|
|
7
|
-
* worker has created yet. A `key` keeps one waiting job, as `mode: 'dedupe'` does, and an
|
|
8
|
-
* as `group_key`, and an id from `uuidv7()`, which rises within a database session: pg-boss
|
|
9
|
-
* transaction's start, then by id, so the jobs one transaction enqueues run in that order.
|
|
10
|
-
*
|
|
6
|
+
* transaction, working in no organization. It inserts what pg-boss's `send()` does, with the queue's retry settings; it
|
|
7
|
+
* creates a queue that no worker has created yet. A `key` keeps one waiting job, as `mode: 'dedupe'` does, and an
|
|
8
|
+
* ordered job takes its group as `group_key`, and an id from `uuidv7()`, which rises within a database session: pg-boss
|
|
9
|
+
* runs a group's jobs by their transaction's start, then by id, so the jobs one transaction enqueues run in that order.
|
|
10
|
+
* The runtime's keyed enqueue passes an `organization` too, a JSON string or number, which only that overload takes, so
|
|
11
|
+
* a payload never names one: its data is built as `jobData` builds it. Tied to the pg-boss schema version, which the
|
|
12
|
+
* startup check pins.
|
|
11
13
|
*/
|
|
12
14
|
const enqueue = (schema) => `create or replace function ${schema}.enqueue(
|
|
13
15
|
job_name text,
|
|
14
|
-
payload jsonb
|
|
15
|
-
run_at timestamptz
|
|
16
|
-
key text
|
|
17
|
-
group_key text
|
|
18
|
-
priority integer
|
|
16
|
+
payload jsonb,
|
|
17
|
+
run_at timestamptz,
|
|
18
|
+
key text,
|
|
19
|
+
group_key text,
|
|
20
|
+
priority integer,
|
|
21
|
+
organization jsonb
|
|
19
22
|
) returns uuid
|
|
20
23
|
language plpgsql as $$
|
|
21
24
|
declare
|
|
22
25
|
queue ${schema}.queue;
|
|
26
|
+
data jsonb := payload;
|
|
23
27
|
start timestamptz := coalesce(run_at, ${schema}.job_now());
|
|
24
28
|
job_id uuid;
|
|
25
29
|
begin
|
|
26
30
|
if key is not null and group_key is not null then
|
|
27
31
|
raise exception 'Job "%": an ordered job takes a group_key and no key', job_name;
|
|
28
32
|
end if;
|
|
33
|
+
if organization = 'null'::jsonb then
|
|
34
|
+
organization := null;
|
|
35
|
+
end if;
|
|
36
|
+
if organization is not null and jsonb_typeof(organization) not in ('string', 'number') then
|
|
37
|
+
raise exception 'Job "%": an organization is a string or a number', job_name;
|
|
38
|
+
end if;
|
|
39
|
+
if organization is not null or (jsonb_typeof(payload) = 'object' and payload ? '${organizationKey}') then
|
|
40
|
+
data := jsonb_build_object('${organizationKey}', coalesce(organization, 'null'::jsonb), 'payload', coalesce(payload, 'null'::jsonb));
|
|
41
|
+
end if;
|
|
29
42
|
perform ${schema}.create_queue(job_name, jsonb_build_object('policy', case when group_key is null then 'standard' else 'key_strict_fifo' end));
|
|
30
43
|
select * into queue from ${schema}.queue where name = job_name;
|
|
31
44
|
if (queue.policy = 'key_strict_fifo') <> (group_key is not null) then
|
|
@@ -46,7 +59,7 @@ begin
|
|
|
46
59
|
on conflict do nothing
|
|
47
60
|
returning id',
|
|
48
61
|
queue.table_name)
|
|
49
|
-
using job_name,
|
|
62
|
+
using job_name, data, priority, start, coalesce(key, group_key), queue.retry_limit, queue.retry_delay,
|
|
50
63
|
queue.retry_backoff, queue.retry_delay_max, queue.expire_seconds, queue.deletion_seconds, queue.retention_seconds,
|
|
51
64
|
queue.policy, queue.dead_letter, queue.heartbeat_seconds, key is not null,
|
|
52
65
|
case when group_key is null then gen_random_uuid() else uuidv7() end
|
|
@@ -60,6 +73,18 @@ begin
|
|
|
60
73
|
return job_id;
|
|
61
74
|
end
|
|
62
75
|
$$;
|
|
76
|
+
comment on function ${schema}.enqueue(text, jsonb, timestamptz, text, text, integer, jsonb) is '${functionsComment}';
|
|
77
|
+
create or replace function ${schema}.enqueue(
|
|
78
|
+
job_name text,
|
|
79
|
+
payload jsonb default null,
|
|
80
|
+
run_at timestamptz default null,
|
|
81
|
+
key text default null,
|
|
82
|
+
group_key text default null,
|
|
83
|
+
priority integer default 0
|
|
84
|
+
) returns uuid
|
|
85
|
+
language sql as $$
|
|
86
|
+
select ${schema}.enqueue(job_name, payload, run_at, key, group_key, priority, null)
|
|
87
|
+
$$;
|
|
63
88
|
comment on function ${schema}.enqueue(text, jsonb, timestamptz, text, text, integer) is '${functionsComment}';`;
|
|
64
89
|
/**
|
|
65
90
|
* `jobs.next_due()`: when the next job is due, or `null` when none waits: what a host that sleeps reads to know when to
|
package/dist/jobs/jobs-module.js
CHANGED
|
@@ -14,6 +14,7 @@ const commentVersion = (comment) => {
|
|
|
14
14
|
};
|
|
15
15
|
/** Protobase's functions in the jobs schema by the signature `functionsSql` makes them with, so an overload of one is not it. */
|
|
16
16
|
const functionSignatures = [
|
|
17
|
+
"enqueue(text, jsonb, timestamptz, text, text, integer, jsonb)",
|
|
17
18
|
"enqueue(text, jsonb, timestamptz, text, text, integer)",
|
|
18
19
|
"next_due()",
|
|
19
20
|
"failed_retry()",
|
|
@@ -28,10 +29,10 @@ const functionSignatures = [
|
|
|
28
29
|
const functionsModel = (versions) => {
|
|
29
30
|
const known = versions.filter((version) => version !== void 0);
|
|
30
31
|
const newest = Math.max(...known);
|
|
31
|
-
if (newest >
|
|
32
|
+
if (newest > 5) throw new Error(`Protobase's functions of the jobs schema are at version ${newest}, newer than the 5 this version of Protobase writes: upgrade Protobase`);
|
|
32
33
|
if (known.length === 0) return {};
|
|
33
34
|
const oldest = Math.min(...known);
|
|
34
|
-
if (known.length < versions.length && oldest ===
|
|
35
|
+
if (known.length < versions.length && oldest === 5) return {};
|
|
35
36
|
return { functions: oldest };
|
|
36
37
|
};
|
|
37
38
|
/**
|
|
@@ -71,8 +72,8 @@ const jobsModule = {
|
|
|
71
72
|
replaceables: [{
|
|
72
73
|
name: "functions",
|
|
73
74
|
title: "Protobase's functions of the jobs schema",
|
|
74
|
-
version:
|
|
75
|
-
compatibleFrom:
|
|
75
|
+
version: 5,
|
|
76
|
+
compatibleFrom: 5,
|
|
76
77
|
sql: () => functionsSql(jobsSchema)
|
|
77
78
|
}],
|
|
78
79
|
resourceReplaceables: [],
|
package/dist/jobs/runtime.js
CHANGED
|
@@ -19,6 +19,8 @@ import { sql } from "kysely";
|
|
|
19
19
|
import { randomUUID } from "node:crypto";
|
|
20
20
|
import { PgBoss } from "pg-boss";
|
|
21
21
|
//#region src/jobs/runtime.ts
|
|
22
|
+
/** A `jsonb` parameter of `jobs.enqueue`: the value as JSON, or SQL null for none. */
|
|
23
|
+
const asJson = (value) => value === void 0 || value === null ? null : JSON.stringify(value);
|
|
22
24
|
const createBoss = (db, work) => new PgBoss({
|
|
23
25
|
db,
|
|
24
26
|
schema: jobsSchema,
|
|
@@ -97,7 +99,8 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
97
99
|
if (!definitions.has(name)) throw new Error(`Job "${name}" is not among the app's jobs; export it from the module protobase.config.ts passes as \`jobs\``);
|
|
98
100
|
const checked = await validatePayload(definition, payload);
|
|
99
101
|
if ("invalid" in checked) throw new Error(checked.invalid);
|
|
100
|
-
const
|
|
102
|
+
const organization = organizationOf(target);
|
|
103
|
+
const data = jobData(checked.value, organization);
|
|
101
104
|
const orderedId = definition.options.ordered ? (await sql`select uuidv7() as id`.execute(target)).rows[0].id : void 0;
|
|
102
105
|
const send = {
|
|
103
106
|
...sendOptions(definition.options, options),
|
|
@@ -107,7 +110,7 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
107
110
|
const keyed = async (key) => {
|
|
108
111
|
if (options.mode === "replace") return (await boss.upsert(name, data, send)).jobs[0];
|
|
109
112
|
const runAt = send.startAfter === void 0 ? sql`null` : send.startAfter instanceof Date ? sql`${send.startAfter}::timestamptz` : sql`${sql.id(jobsSchema, "job_now")}() + ${send.startAfter} * interval '1 second'`;
|
|
110
|
-
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, ${
|
|
113
|
+
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, ${asJson(checked.value)}::jsonb, ${runAt}, ${key}, null, ${send.priority ?? 0}, ${asJson(organization)}::jsonb) as id`.execute(target);
|
|
111
114
|
return rows[0].id;
|
|
112
115
|
};
|
|
113
116
|
const id = options.key === void 0 ? await boss.send(name, data, send) : await keyed(options.key);
|
|
@@ -124,6 +127,7 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
124
127
|
const checked = await validatePayload(definition, payload);
|
|
125
128
|
if ("invalid" in checked) throw new Error(checked.invalid);
|
|
126
129
|
return {
|
|
130
|
+
payload: checked.value,
|
|
127
131
|
data: jobData(checked.value, organization),
|
|
128
132
|
send: sendOptions(definition.options, options),
|
|
129
133
|
key: options.key
|
|
@@ -149,10 +153,10 @@ const startJobs = async ({ db, scope = noOrganizationTables, jobs, work = true,
|
|
|
149
153
|
}]);
|
|
150
154
|
if (keyed.length > 0) {
|
|
151
155
|
const at = (job) => job.send.startAfter;
|
|
152
|
-
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, j.
|
|
153
|
-
from unnest(${keyed.map((job) =>
|
|
156
|
+
const { rows } = await sql`select ${sql.id(jobsSchema, "enqueue")}(${name}, j.payload, coalesce(j.run_at, ${sql.id(jobsSchema, "job_now")}() + j.delay * interval '1 second'), j.key, null, j.priority, ${asJson(organization)}::jsonb) as id
|
|
157
|
+
from unnest(${keyed.map((job) => asJson(job.payload))}::jsonb[], ${keyed.map((job) => at(job) instanceof Date ? at(job) : null)}::timestamptz[],
|
|
154
158
|
${keyed.map((job) => typeof at(job) === "number" ? at(job) : null)}::float8[], ${keyed.map((job) => job.key)}::text[], ${keyed.map((job) => job.send.priority ?? 0)}::integer[])
|
|
155
|
-
with ordinality as j(
|
|
159
|
+
with ordinality as j(payload, run_at, delay, key, priority, place)
|
|
156
160
|
order by j.place`.execute(target);
|
|
157
161
|
rows.forEach((row, n) => {
|
|
158
162
|
if (!row.id) throw new Error(`Job "${name}" was not enqueued`);
|
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.132.0",
|
|
4
4
|
"description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"@better-auth/sso": "^1.7.12",
|
|
45
45
|
"@livekit/protocol": "1.53.1",
|
|
46
46
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
47
|
-
"@protobase/layout": "0.
|
|
48
|
-
"@protobase/query": "0.
|
|
49
|
-
"@protobase/schema": "0.
|
|
50
|
-
"@protobase/workflow-plugins": "0.
|
|
51
|
-
"@protobase/workflows": "0.
|
|
47
|
+
"@protobase/layout": "0.132.0",
|
|
48
|
+
"@protobase/query": "0.132.0",
|
|
49
|
+
"@protobase/schema": "0.132.0",
|
|
50
|
+
"@protobase/workflow-plugins": "0.132.0",
|
|
51
|
+
"@protobase/workflows": "0.132.0",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.1",
|