@sixb/pg 0.2.2 → 0.2.4
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/auth-storage/auth-storage.d.ts +2 -1
- package/dist/auth-storage/auth-storage.d.ts.map +1 -1
- package/dist/auth-storage/identities.d.ts +4 -0
- package/dist/auth-storage/identities.d.ts.map +1 -1
- package/dist/auth-storage/rows.d.ts +2 -1
- package/dist/auth-storage/rows.d.ts.map +1 -1
- package/dist/auth-storage/users.d.ts.map +1 -1
- package/dist/connector-connection-storage.d.ts.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1807 -279
- package/dist/index.js.map +25 -21
- package/dist/migrations.d.ts.map +1 -1
- package/dist/objects/storage.d.ts +10 -1
- package/dist/objects/storage.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-session.d.ts +5 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
- package/dist/ontology-storage/materialization-writer.d.ts +14 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
- package/dist/ontology-storage/materializations.d.ts.map +1 -1
- package/dist/pg-artifact-storage.d.ts +14 -2
- package/dist/pg-artifact-storage.d.ts.map +1 -1
- package/dist/pg-dataset-ingest-storage.d.ts +6 -0
- package/dist/pg-dataset-ingest-storage.d.ts.map +1 -0
- package/dist/pg-lake-retention-run-storage.d.ts +11 -0
- package/dist/pg-lake-retention-run-storage.d.ts.map +1 -0
- package/dist/pg-maintenance-heartbeat-storage.d.ts +11 -0
- package/dist/pg-maintenance-heartbeat-storage.d.ts.map +1 -0
- package/dist/pg-pipeline-run-storage.d.ts +5 -2
- package/dist/pg-pipeline-run-storage.d.ts.map +1 -1
- package/dist/pg-projection-run-storage.d.ts +5 -1
- package/dist/pg-projection-run-storage.d.ts.map +1 -1
- package/dist/pg-sync-run-storage.d.ts +6 -2
- package/dist/pg-sync-run-storage.d.ts.map +1 -1
- package/dist/pg-webhook-run-storage.d.ts +2 -1
- package/dist/pg-webhook-run-storage.d.ts.map +1 -1
- package/dist/pg-workflow-run-storage.d.ts +4 -3
- package/dist/pg-workflow-run-storage.d.ts.map +1 -1
- package/dist/run-claim-sql.d.ts +37 -0
- package/dist/run-claim-sql.d.ts.map +1 -0
- package/dist/share-session-storage.d.ts +2 -1
- package/dist/share-session-storage.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/auth-storage/auth-storage.ts +66 -0
- package/src/auth-storage/identities.ts +15 -0
- package/src/auth-storage/rows.ts +3 -0
- package/src/auth-storage/users.ts +3 -0
- package/src/connector-connection-storage.ts +33 -16
- package/src/index.ts +15 -0
- package/src/migrations/089-maintenance-heartbeats.sql +10 -0
- package/src/migrations/090-lake-retention-runs.sql +8 -0
- package/src/migrations/091-dataset-ingest-inbox.sql +38 -0
- package/src/migrations/092-run-recovery.sql +23 -0
- package/src/migrations/093-webhook-and-projection-leases.sql +10 -0
- package/src/migrations/094-artifact-board-previews.sql +14 -0
- package/src/migrations/095-artifact-open-versions.sql +101 -0
- package/src/migrations/096-artifact-documents.sql +11 -0
- package/src/migrations/097-artifact-frame-files.sql +8 -0
- package/src/migrations/098-artifact-comments.sql +36 -0
- package/src/migrations/099-object-artifact-refs.sql +12 -0
- package/src/migrations/100-shared-link-guests.sql +17 -0
- package/src/migrations/101-connector-connection-owners.sql +38 -0
- package/src/migrations/102-user-suspension-source.sql +12 -0
- package/src/migrations.ts +40 -0
- package/src/objects/storage.ts +49 -0
- package/src/ontology-storage/materialization-session.ts +4 -0
- package/src/ontology-storage/materialization-writer.ts +68 -1
- package/src/ontology-storage/materializations.ts +3 -1
- package/src/pg-artifact-storage.ts +798 -137
- package/src/pg-dataset-ingest-storage.ts +305 -0
- package/src/pg-lake-retention-run-storage.ts +40 -0
- package/src/pg-maintenance-heartbeat-storage.ts +53 -0
- package/src/pg-pipeline-run-storage.ts +85 -42
- package/src/pg-projection-run-storage.ts +61 -2
- package/src/pg-sync-run-storage.ts +100 -37
- package/src/pg-webhook-run-storage.ts +37 -11
- package/src/pg-workflow-run-storage.ts +39 -100
- package/src/run-claim-sql.ts +173 -0
- package/src/share-session-storage.ts +20 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { JsonValue } from "@sixb/core"
|
|
2
2
|
import { parseSixbFailure, serializeSixbFailure } from "@sixb/core/internal/errors"
|
|
3
|
+
import { holdsRun, mayFinishRun } from "@sixb/core/internal/storage"
|
|
3
4
|
import {
|
|
4
5
|
assertWorkflowAgentNodeRunExecution,
|
|
5
6
|
assertWorkflowRunExecution,
|
|
@@ -7,6 +8,7 @@ import {
|
|
|
7
8
|
import type { WorkflowIOSnapshot } from "@sixb/core/internal/workflows"
|
|
8
9
|
import type {
|
|
9
10
|
CancelWorkflowAgentNodeRunInput,
|
|
11
|
+
ClaimRunInput,
|
|
10
12
|
ConfirmWorkflowAgentNodeRunExecutionOwnershipInput,
|
|
11
13
|
ConfirmWorkflowRunExecutionOwnershipInput,
|
|
12
14
|
CreateWorkflowAgentNodeRunInput,
|
|
@@ -16,6 +18,7 @@ import type {
|
|
|
16
18
|
FinishWorkflowRunInput,
|
|
17
19
|
ListLatestWorkflowRunsInput,
|
|
18
20
|
ListLatestWorkflowRunsResult,
|
|
21
|
+
ListLostRunsInput,
|
|
19
22
|
ListWorkflowAgentNodeRunsInput,
|
|
20
23
|
ListWorkflowAgentNodeRunsResult,
|
|
21
24
|
ListWorkflowNodeRunsInput,
|
|
@@ -24,11 +27,11 @@ import type {
|
|
|
24
27
|
ListWorkflowRunsResult,
|
|
25
28
|
QueueWorkflowRunInput,
|
|
26
29
|
ReclaimWorkflowAgentNodeRunInput,
|
|
27
|
-
|
|
30
|
+
ReleaseRunExecutionInput,
|
|
28
31
|
ResumeWorkflowRunInput,
|
|
32
|
+
RunClaim,
|
|
29
33
|
StartWorkflowAgentNodeRunInput,
|
|
30
34
|
StartWorkflowNodeRunInput,
|
|
31
|
-
StartWorkflowRunInput,
|
|
32
35
|
WaitWorkflowNodeRunInput,
|
|
33
36
|
WaitWorkflowRunInput,
|
|
34
37
|
WorkflowAgentNodeRunRecord,
|
|
@@ -45,10 +48,29 @@ import {
|
|
|
45
48
|
} from "@sixb/core/storage"
|
|
46
49
|
import { queryLatestRunsByOwnerId } from "./latest-run-query"
|
|
47
50
|
import type { SqlParameter } from "./pg-client"
|
|
51
|
+
import {
|
|
52
|
+
claimPgRun,
|
|
53
|
+
confirmPgRunExecution,
|
|
54
|
+
listLostPgRuns,
|
|
55
|
+
type PgRunExecutionColumns,
|
|
56
|
+
type PgRunTable,
|
|
57
|
+
pgRunExecutionFields,
|
|
58
|
+
releasePgRun,
|
|
59
|
+
} from "./run-claim-sql"
|
|
48
60
|
import { appendRunListFilters, hasEmptyStatuses, queryRunList } from "./run-list-query"
|
|
49
61
|
import { isUniqueViolation } from "./storage-errors"
|
|
50
62
|
import { type PgStoreClient, runPgTransaction } from "./transactions"
|
|
51
63
|
|
|
64
|
+
const WORKFLOW_RUNS: PgRunTable<WorkflowRunDatabaseRow, WorkflowRunRecord> = {
|
|
65
|
+
name: "workflow_runs",
|
|
66
|
+
returning: "*",
|
|
67
|
+
toRun: rowToWorkflowRunRecord,
|
|
68
|
+
notFound: (projectId, id) =>
|
|
69
|
+
new WorkflowRunError(`[SixbPg] Workflow run '${id}' not found for project '${projectId}'.`),
|
|
70
|
+
staleExecution: (id) =>
|
|
71
|
+
new WorkflowRunError(`[SixbPg] Execution token is no longer current on workflow run '${id}'.`),
|
|
72
|
+
}
|
|
73
|
+
|
|
52
74
|
export class PgWorkflowRunStorage implements WorkflowRunStorage {
|
|
53
75
|
readonly nodes: PgWorkflowNodeRunStorage
|
|
54
76
|
readonly agentNodes: PgWorkflowAgentNodeRunStorage
|
|
@@ -109,74 +131,22 @@ export class PgWorkflowRunStorage implements WorkflowRunStorage {
|
|
|
109
131
|
}
|
|
110
132
|
}
|
|
111
133
|
|
|
112
|
-
async
|
|
113
|
-
return runPgTransaction(this.sql,
|
|
114
|
-
|
|
115
|
-
const [existing] = await tx<WorkflowRunDatabaseRow[]>`
|
|
116
|
-
SELECT * FROM workflow_runs
|
|
117
|
-
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
118
|
-
FOR UPDATE
|
|
119
|
-
`
|
|
120
|
-
|
|
121
|
-
if (!existing || existing.status !== "queued") {
|
|
122
|
-
throw new WorkflowRunError(
|
|
123
|
-
existing
|
|
124
|
-
? `[SixbPg] Workflow run '${input.id}' cannot start from status '${existing.status}'.`
|
|
125
|
-
: `[SixbPg] Workflow run '${input.id}' not found for project '${input.projectId}'.`
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const [updated] = await tx<WorkflowRunDatabaseRow[]>`
|
|
130
|
-
UPDATE workflow_runs
|
|
131
|
-
SET
|
|
132
|
-
status = ${"running"},
|
|
133
|
-
started_at = ${startedAt},
|
|
134
|
-
finished_at = ${null},
|
|
135
|
-
error = ${null},
|
|
136
|
-
attempt = attempt + 1,
|
|
137
|
-
execution_token = ${input.execution?.token ?? null},
|
|
138
|
-
execution_queue_lease_expires_at = ${input.execution?.queueLeaseExpiresAt ?? null}
|
|
139
|
-
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
140
|
-
RETURNING *
|
|
141
|
-
`
|
|
134
|
+
async startOrReclaim(input: ClaimRunInput): Promise<RunClaim<WorkflowRunRecord>> {
|
|
135
|
+
return runPgTransaction(this.sql, (tx) => claimPgRun(tx, WORKFLOW_RUNS, input))
|
|
136
|
+
}
|
|
142
137
|
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
async listLost(input: ListLostRunsInput): Promise<readonly WorkflowRunRecord[]> {
|
|
139
|
+
return listLostPgRuns(this.sql, WORKFLOW_RUNS, input)
|
|
145
140
|
}
|
|
146
141
|
|
|
147
|
-
async
|
|
148
|
-
|
|
149
|
-
const existing = await lockWorkflowRun(tx, input.projectId, input.id, "running")
|
|
150
|
-
const [updated] = await tx<WorkflowRunDatabaseRow[]>`
|
|
151
|
-
UPDATE workflow_runs SET
|
|
152
|
-
attempt = ${Number(existing.attempt) + 1},
|
|
153
|
-
execution_token = ${input.execution.token},
|
|
154
|
-
execution_queue_lease_expires_at = ${input.execution.queueLeaseExpiresAt}
|
|
155
|
-
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
156
|
-
RETURNING *
|
|
157
|
-
`
|
|
158
|
-
return rowToWorkflowRunRecord(updated)
|
|
159
|
-
})
|
|
142
|
+
async releaseExecution(input: ReleaseRunExecutionInput): Promise<void> {
|
|
143
|
+
await releasePgRun(this.sql, WORKFLOW_RUNS, input)
|
|
160
144
|
}
|
|
161
145
|
|
|
162
146
|
async confirmExecutionOwnership(
|
|
163
147
|
input: ConfirmWorkflowRunExecutionOwnershipInput
|
|
164
148
|
): Promise<WorkflowRunRecord> {
|
|
165
|
-
return runPgTransaction(this.sql,
|
|
166
|
-
const existing = await lockWorkflowRun(tx, input.projectId, input.id, "running")
|
|
167
|
-
assertWorkflowRunExecutionOwnership(existing, input.executionToken)
|
|
168
|
-
const current = existing.execution_queue_lease_expires_at
|
|
169
|
-
? new Date(existing.execution_queue_lease_expires_at)
|
|
170
|
-
: input.queueLeaseExpiresAt
|
|
171
|
-
const [updated] = await tx<WorkflowRunDatabaseRow[]>`
|
|
172
|
-
UPDATE workflow_runs SET execution_queue_lease_expires_at = ${new Date(
|
|
173
|
-
Math.max(current.getTime(), input.queueLeaseExpiresAt.getTime())
|
|
174
|
-
)}
|
|
175
|
-
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
176
|
-
RETURNING *
|
|
177
|
-
`
|
|
178
|
-
return rowToWorkflowRunRecord(updated)
|
|
179
|
-
})
|
|
149
|
+
return runPgTransaction(this.sql, (tx) => confirmPgRunExecution(tx, WORKFLOW_RUNS, input))
|
|
180
150
|
}
|
|
181
151
|
|
|
182
152
|
async finish(input: FinishWorkflowRunInput): Promise<WorkflowRunRecord> {
|
|
@@ -198,7 +168,9 @@ export class PgWorkflowRunStorage implements WorkflowRunStorage {
|
|
|
198
168
|
`[SixbPg] Workflow run '${input.id}' for project '${input.projectId}' cannot be finished from status '${existing.status}'.`
|
|
199
169
|
)
|
|
200
170
|
}
|
|
201
|
-
|
|
171
|
+
if (!mayFinishRun(rowToWorkflowRunRecord(existing), input)) {
|
|
172
|
+
throw WORKFLOW_RUNS.staleExecution(input.id)
|
|
173
|
+
}
|
|
202
174
|
|
|
203
175
|
const [updated] =
|
|
204
176
|
input.status === "succeeded"
|
|
@@ -294,7 +266,7 @@ export class PgWorkflowRunStorage implements WorkflowRunStorage {
|
|
|
294
266
|
status = ${"running"},
|
|
295
267
|
finished_at = ${null},
|
|
296
268
|
error = ${null},
|
|
297
|
-
attempt =
|
|
269
|
+
attempt = ${1},
|
|
298
270
|
execution_token = ${input.execution?.token ?? null},
|
|
299
271
|
execution_queue_lease_expires_at = ${input.execution?.queueLeaseExpiresAt ?? null}
|
|
300
272
|
WHERE project_id = ${input.projectId} AND id = ${input.id}
|
|
@@ -842,15 +814,7 @@ function rowToWorkflowRunRecord(row: WorkflowRunDatabaseRow): WorkflowRunRecord
|
|
|
842
814
|
startedAt: new Date(row.started_at),
|
|
843
815
|
finishedAt: row.finished_at ? new Date(row.finished_at) : undefined,
|
|
844
816
|
error: row.error === null ? undefined : parseSixbFailure(row.error, WORKFLOW_RUN_FAILURE_CODES),
|
|
845
|
-
|
|
846
|
-
...(row.execution_token && row.execution_queue_lease_expires_at
|
|
847
|
-
? {
|
|
848
|
-
execution: {
|
|
849
|
-
token: row.execution_token,
|
|
850
|
-
queueLeaseExpiresAt: new Date(row.execution_queue_lease_expires_at),
|
|
851
|
-
},
|
|
852
|
-
}
|
|
853
|
-
: {}),
|
|
817
|
+
...pgRunExecutionFields(row),
|
|
854
818
|
}
|
|
855
819
|
}
|
|
856
820
|
|
|
@@ -890,7 +854,7 @@ function canFinishWorkflowRun(
|
|
|
890
854
|
)
|
|
891
855
|
}
|
|
892
856
|
|
|
893
|
-
interface WorkflowRunDatabaseRow {
|
|
857
|
+
interface WorkflowRunDatabaseRow extends PgRunExecutionColumns {
|
|
894
858
|
project_id: string
|
|
895
859
|
id: string
|
|
896
860
|
execution_id: string
|
|
@@ -902,9 +866,6 @@ interface WorkflowRunDatabaseRow {
|
|
|
902
866
|
started_at: Date | string
|
|
903
867
|
finished_at: Date | string | null
|
|
904
868
|
error: JsonValue | null
|
|
905
|
-
attempt: number | string
|
|
906
|
-
execution_token: string | null
|
|
907
|
-
execution_queue_lease_expires_at: Date | string | null
|
|
908
869
|
}
|
|
909
870
|
|
|
910
871
|
interface WorkflowNodeRunDatabaseRow {
|
|
@@ -1013,30 +974,8 @@ function parseJson<T>(value: T | string): T {
|
|
|
1013
974
|
return typeof value === "string" ? (JSON.parse(value) as T) : value
|
|
1014
975
|
}
|
|
1015
976
|
|
|
1016
|
-
async function lockWorkflowRun(
|
|
1017
|
-
sql: PgStoreClient,
|
|
1018
|
-
projectId: string,
|
|
1019
|
-
id: string,
|
|
1020
|
-
status: WorkflowRunRecord["status"]
|
|
1021
|
-
): Promise<WorkflowRunDatabaseRow> {
|
|
1022
|
-
const [row] = await sql<WorkflowRunDatabaseRow[]>`
|
|
1023
|
-
SELECT * FROM workflow_runs WHERE project_id = ${projectId} AND id = ${id} FOR UPDATE
|
|
1024
|
-
`
|
|
1025
|
-
if (!row) throw new WorkflowRunError(`[SixbPg] Workflow run '${id}' not found.`)
|
|
1026
|
-
if (row.status !== status) {
|
|
1027
|
-
throw new WorkflowRunError(
|
|
1028
|
-
`[SixbPg] Workflow run '${id}' must be ${status} (status '${row.status}').`
|
|
1029
|
-
)
|
|
1030
|
-
}
|
|
1031
|
-
return row
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
977
|
function assertWorkflowRunExecutionOwnership(row: WorkflowRunDatabaseRow, token?: string): void {
|
|
1035
|
-
if (row
|
|
1036
|
-
throw new WorkflowRunError(
|
|
1037
|
-
`[SixbPg] Execution token is no longer current on workflow run '${row.id}'.`
|
|
1038
|
-
)
|
|
1039
|
-
}
|
|
978
|
+
if (!holdsRun(rowToWorkflowRunRecord(row), token)) throw WORKFLOW_RUNS.staleExecution(row.id)
|
|
1040
979
|
}
|
|
1041
980
|
|
|
1042
981
|
async function assertWorkflowNodeParentExecutionOwnership(
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { type ClaimableRun, laterLease, planRunClaim } from "@sixb/core/internal/storage"
|
|
2
|
+
import type {
|
|
3
|
+
ClaimRunInput,
|
|
4
|
+
ConfirmRunExecutionInput,
|
|
5
|
+
ListLostRunsInput,
|
|
6
|
+
ReleaseRunExecutionInput,
|
|
7
|
+
RunClaim,
|
|
8
|
+
RunExecution,
|
|
9
|
+
} from "@sixb/core/storage"
|
|
10
|
+
import type { PgStoreClient } from "./transactions"
|
|
11
|
+
|
|
12
|
+
/** The columns every recoverable run keeps, whichever table holds it. */
|
|
13
|
+
export interface PgRunExecutionColumns {
|
|
14
|
+
readonly attempt: number | string
|
|
15
|
+
readonly execution_token: string | null
|
|
16
|
+
readonly execution_queue_lease_expires_at: Date | string | null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** One table of recoverable runs. Only its name, and how a row reads, differ between primitives. */
|
|
20
|
+
export interface PgRunTable<TRow extends PgRunExecutionColumns, TRun> {
|
|
21
|
+
readonly name: "sync_runs" | "pipeline_runs" | "workflow_runs"
|
|
22
|
+
readonly returning: string
|
|
23
|
+
toRun(row: TRow): TRun
|
|
24
|
+
notFound(projectId: string, id: string): Error
|
|
25
|
+
staleExecution(id: string): Error
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function pgRunExecutionFields(row: PgRunExecutionColumns): {
|
|
29
|
+
readonly attempt: number
|
|
30
|
+
readonly execution: RunExecution | undefined
|
|
31
|
+
} {
|
|
32
|
+
return {
|
|
33
|
+
attempt: Number(row.attempt),
|
|
34
|
+
execution:
|
|
35
|
+
row.execution_token && row.execution_queue_lease_expires_at
|
|
36
|
+
? {
|
|
37
|
+
token: row.execution_token,
|
|
38
|
+
queueLeaseExpiresAt: new Date(row.execution_queue_lease_expires_at),
|
|
39
|
+
}
|
|
40
|
+
: undefined,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function lockPgRun<TRow extends PgRunExecutionColumns, TRun>(
|
|
45
|
+
tx: PgStoreClient,
|
|
46
|
+
table: PgRunTable<TRow, TRun>,
|
|
47
|
+
projectId: string,
|
|
48
|
+
id: string
|
|
49
|
+
): Promise<TRun> {
|
|
50
|
+
const [row] = await tx.unsafe<TRow[]>(
|
|
51
|
+
`SELECT ${table.returning} FROM ${table.name} WHERE project_id = $1 AND id = $2 FOR UPDATE`,
|
|
52
|
+
[projectId, id]
|
|
53
|
+
)
|
|
54
|
+
if (!row) throw table.notFound(projectId, id)
|
|
55
|
+
|
|
56
|
+
return table.toRun(row)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `sibling` is a run this one must not overlap, read in the same transaction. */
|
|
60
|
+
export async function claimPgRun<TRow extends PgRunExecutionColumns, TRun extends ClaimableRun>(
|
|
61
|
+
tx: PgStoreClient,
|
|
62
|
+
table: PgRunTable<TRow, TRun>,
|
|
63
|
+
input: ClaimRunInput,
|
|
64
|
+
sibling?: ClaimableRun
|
|
65
|
+
): Promise<RunClaim<TRun>> {
|
|
66
|
+
const existing = await lockPgRun(tx, table, input.projectId, input.id)
|
|
67
|
+
|
|
68
|
+
const plan = planRunClaim(existing, input, sibling)
|
|
69
|
+
if (!plan.take) return plan.claim
|
|
70
|
+
|
|
71
|
+
const [updated] = await tx.unsafe<TRow[]>(
|
|
72
|
+
`UPDATE ${table.name}
|
|
73
|
+
SET status = 'running', started_at = $1, attempt = $2, execution_token = $3,
|
|
74
|
+
execution_queue_lease_expires_at = $4, error = NULL
|
|
75
|
+
WHERE project_id = $5 AND id = $6
|
|
76
|
+
RETURNING ${table.returning}`,
|
|
77
|
+
[
|
|
78
|
+
plan.startedAt,
|
|
79
|
+
plan.attempt,
|
|
80
|
+
plan.execution.token,
|
|
81
|
+
plan.execution.queueLeaseExpiresAt,
|
|
82
|
+
input.projectId,
|
|
83
|
+
input.id,
|
|
84
|
+
]
|
|
85
|
+
)
|
|
86
|
+
if (!updated) throw table.notFound(input.projectId, input.id)
|
|
87
|
+
|
|
88
|
+
return { outcome: plan.outcome, run: table.toRun(updated) }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export async function lockHeldPgRun<TRow extends PgRunExecutionColumns, TRun extends ClaimableRun>(
|
|
92
|
+
tx: PgStoreClient,
|
|
93
|
+
table: PgRunTable<TRow, TRun>,
|
|
94
|
+
input: { readonly projectId: string; readonly id: string; readonly executionToken: string }
|
|
95
|
+
): Promise<TRun> {
|
|
96
|
+
const run = await lockPgRun(tx, table, input.projectId, input.id)
|
|
97
|
+
if (run.status !== "running" || run.execution?.token !== input.executionToken) {
|
|
98
|
+
throw table.staleExecution(input.id)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return run
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function confirmPgRunExecution<
|
|
105
|
+
TRow extends PgRunExecutionColumns,
|
|
106
|
+
TRun extends ClaimableRun,
|
|
107
|
+
>(
|
|
108
|
+
tx: PgStoreClient,
|
|
109
|
+
table: PgRunTable<TRow, TRun>,
|
|
110
|
+
input: ConfirmRunExecutionInput
|
|
111
|
+
): Promise<TRun> {
|
|
112
|
+
const existing = await lockHeldPgRun(tx, table, input)
|
|
113
|
+
|
|
114
|
+
const [updated] = await tx.unsafe<TRow[]>(
|
|
115
|
+
`UPDATE ${table.name} SET execution_queue_lease_expires_at = $1
|
|
116
|
+
WHERE project_id = $2 AND id = $3
|
|
117
|
+
RETURNING ${table.returning}`,
|
|
118
|
+
[
|
|
119
|
+
laterLease(existing.execution?.queueLeaseExpiresAt, input.queueLeaseExpiresAt),
|
|
120
|
+
input.projectId,
|
|
121
|
+
input.id,
|
|
122
|
+
]
|
|
123
|
+
)
|
|
124
|
+
if (!updated) throw table.notFound(input.projectId, input.id)
|
|
125
|
+
|
|
126
|
+
return table.toRun(updated)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Starts from `(project_id, status, ...)`, where queued and running rows are few however long the
|
|
131
|
+
* table's history. Running runs come first, so queued ones waiting for a sibling starve nothing.
|
|
132
|
+
*/
|
|
133
|
+
export async function listLostPgRuns<TRow extends PgRunExecutionColumns, TRun>(
|
|
134
|
+
sql: PgStoreClient,
|
|
135
|
+
table: PgRunTable<TRow, TRun>,
|
|
136
|
+
input: ListLostRunsInput
|
|
137
|
+
): Promise<readonly TRun[]> {
|
|
138
|
+
const rows = await sql.unsafe<TRow[]>(
|
|
139
|
+
`SELECT ${table.returning} FROM ${table.name}
|
|
140
|
+
WHERE project_id = $1
|
|
141
|
+
AND status IN ('queued', 'running')
|
|
142
|
+
AND (
|
|
143
|
+
(status = 'queued' AND queued_at <= $2)
|
|
144
|
+
OR (status = 'running' AND execution_queue_lease_expires_at <= $3)
|
|
145
|
+
OR (status = 'running' AND execution_queue_lease_expires_at IS NULL AND started_at <= $4)
|
|
146
|
+
)
|
|
147
|
+
ORDER BY status = 'running' DESC,
|
|
148
|
+
COALESCE(execution_queue_lease_expires_at, started_at, queued_at)
|
|
149
|
+
LIMIT $5`,
|
|
150
|
+
[input.projectId, input.queuedBefore, input.lapsedBy, input.orphanedBefore, input.limit]
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
return rows.map(table.toRun)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function releasePgRun(
|
|
157
|
+
sql: PgStoreClient,
|
|
158
|
+
table: Pick<PgRunTable<never, never>, "name">,
|
|
159
|
+
input: ReleaseRunExecutionInput
|
|
160
|
+
): Promise<void> {
|
|
161
|
+
await sql.unsafe(
|
|
162
|
+
`UPDATE ${table.name}
|
|
163
|
+
SET execution_queue_lease_expires_at = $1, attempt = attempt - $2
|
|
164
|
+
WHERE project_id = $3 AND id = $4 AND status = 'running' AND execution_token = $5`,
|
|
165
|
+
[
|
|
166
|
+
input.releasedAt ?? new Date(),
|
|
167
|
+
input.spentAttempt ? 0 : 1,
|
|
168
|
+
input.projectId,
|
|
169
|
+
input.id,
|
|
170
|
+
input.executionToken,
|
|
171
|
+
]
|
|
172
|
+
)
|
|
173
|
+
}
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
normalizeGetShareSessionByIdInput,
|
|
3
3
|
normalizeRenewShareSessionIfValidInput,
|
|
4
4
|
normalizeRevokeShareSessionInput,
|
|
5
|
+
normalizeSetShareSessionGuestNameInput,
|
|
5
6
|
normalizeShareSessionCreate,
|
|
6
7
|
parseShareSessionRecord,
|
|
7
8
|
} from "@sixb/core/internal/share-session-storage-provider"
|
|
@@ -10,6 +11,7 @@ import type {
|
|
|
10
11
|
GetShareSessionByIdInput,
|
|
11
12
|
RenewShareSessionIfValidInput,
|
|
12
13
|
RevokeShareSessionInput,
|
|
14
|
+
SetShareSessionGuestNameInput,
|
|
13
15
|
ShareSessionRecord,
|
|
14
16
|
ShareSessionStorage,
|
|
15
17
|
} from "@sixb/core/storage"
|
|
@@ -80,6 +82,22 @@ export class PgShareSessionStorage implements ShareSessionStorage {
|
|
|
80
82
|
return row ? recordFromRow(row) : null
|
|
81
83
|
}
|
|
82
84
|
|
|
85
|
+
async setGuestName(input: SetShareSessionGuestNameInput): Promise<ShareSessionRecord | null> {
|
|
86
|
+
const normalized = normalizeSetShareSessionGuestNameInput(input)
|
|
87
|
+
const [row] = await this.sql<PgShareSessionRow[]>`
|
|
88
|
+
UPDATE share_sessions
|
|
89
|
+
SET guest_name = ${normalized.name}
|
|
90
|
+
WHERE project_id = ${normalized.projectId}
|
|
91
|
+
AND id = ${normalized.id}
|
|
92
|
+
AND grant_id = ${normalized.grantId}
|
|
93
|
+
AND revoked_at IS NULL
|
|
94
|
+
AND expires_at > ${normalized.now}
|
|
95
|
+
AND absolute_expires_at > ${normalized.now}
|
|
96
|
+
RETURNING *
|
|
97
|
+
`
|
|
98
|
+
return row ? recordFromRow(row) : null
|
|
99
|
+
}
|
|
100
|
+
|
|
83
101
|
async revoke(input: RevokeShareSessionInput): Promise<ShareSessionRecord | null> {
|
|
84
102
|
const normalized = normalizeRevokeShareSessionInput(input)
|
|
85
103
|
const current = await this.getById(normalized)
|
|
@@ -107,6 +125,7 @@ interface PgShareSessionRow {
|
|
|
107
125
|
readonly expires_at: unknown
|
|
108
126
|
readonly absolute_expires_at: unknown
|
|
109
127
|
readonly revoked_at: unknown
|
|
128
|
+
readonly guest_name: unknown
|
|
110
129
|
}
|
|
111
130
|
|
|
112
131
|
function recordFromRow(row: PgShareSessionRow): ShareSessionRecord {
|
|
@@ -119,6 +138,7 @@ function recordFromRow(row: PgShareSessionRow): ShareSessionRecord {
|
|
|
119
138
|
expiresAt: row.expires_at,
|
|
120
139
|
absoluteExpiresAt: row.absolute_expires_at,
|
|
121
140
|
...(row.revoked_at === null ? {} : { revokedAt: row.revoked_at }),
|
|
141
|
+
...(row.guest_name === null ? {} : { guestName: row.guest_name }),
|
|
122
142
|
})
|
|
123
143
|
}
|
|
124
144
|
|