@cloudhal/schedules 0.0.0-stage → 0.1.1
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/LICENSE +202 -0
- package/README.md +103 -2
- package/dist/src/automation.d.ts +82 -0
- package/dist/src/automation.js +199 -0
- package/dist/src/budget-meter.d.ts +58 -0
- package/dist/src/budget-meter.js +130 -0
- package/dist/src/budget.d.ts +41 -0
- package/dist/src/budget.js +76 -0
- package/dist/src/builtins.d.ts +132 -0
- package/dist/src/builtins.js +473 -0
- package/dist/src/engine.d.ts +16 -0
- package/dist/src/engine.js +30 -0
- package/dist/src/errors.d.ts +22 -0
- package/dist/src/errors.js +56 -0
- package/dist/src/feed-check.d.ts +56 -0
- package/dist/src/feed-check.js +365 -0
- package/dist/src/findings.d.ts +185 -0
- package/dist/src/findings.js +287 -0
- package/dist/src/ids.d.ts +24 -0
- package/dist/src/ids.js +96 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +24 -0
- package/dist/src/instant.d.ts +19 -0
- package/dist/src/instant.js +50 -0
- package/dist/src/notify.d.ts +50 -0
- package/dist/src/notify.js +60 -0
- package/dist/src/recurrence.d.ts +155 -0
- package/dist/src/recurrence.js +939 -0
- package/dist/src/release-when.d.ts +23 -0
- package/dist/src/release-when.js +55 -0
- package/dist/src/release.d.ts +198 -0
- package/dist/src/release.js +552 -0
- package/dist/src/scheduler.d.ts +490 -0
- package/dist/src/scheduler.js +1343 -0
- package/dist/src/sql/finish.d.ts +32 -0
- package/dist/src/sql/finish.js +103 -0
- package/dist/src/sql/index.d.ts +6 -0
- package/dist/src/sql/index.js +15 -0
- package/dist/src/sql/rows.d.ts +125 -0
- package/dist/src/sql/rows.js +297 -0
- package/dist/src/sql/store.d.ts +467 -0
- package/dist/src/sql/store.js +2166 -0
- package/dist/src/taint.d.ts +65 -0
- package/dist/src/taint.js +138 -0
- package/dist/src/text.d.ts +6 -0
- package/dist/src/text.js +25 -0
- package/dist/src/time-entry.d.ts +6 -0
- package/dist/src/time-entry.js +6 -0
- package/dist/src/time.d.ts +61 -0
- package/dist/src/time.js +140 -0
- package/package.json +69 -3
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The statements that end a claimed release (RFC-0008 §6.3 statements 5–7). Temporal-free and
|
|
3
|
+
* storage-free, so `publishRelease` (`@cloudhal/core-ts`, through `@cloudhal/schedules/engine`)
|
|
4
|
+
* appends them to its own guarded batch; `SqlScheduleStore.finishRelease` runs them alone.
|
|
5
|
+
*/
|
|
6
|
+
import type { ScheduleRun } from "@cloudhal/core-contracts";
|
|
7
|
+
import type { SqlStatement } from "@cloudhal/storage";
|
|
8
|
+
import type { ReleaseFinish } from "../scheduler.js";
|
|
9
|
+
/** The run row's state for a release's terminal state. */
|
|
10
|
+
export declare function runStateForRelease(finish: ReleaseFinish): {
|
|
11
|
+
state: ScheduleRun["state"];
|
|
12
|
+
outcome: ScheduleRun["outcome"] | null;
|
|
13
|
+
error: ScheduleRun["error"] | null;
|
|
14
|
+
};
|
|
15
|
+
export interface ReleaseFinishInput {
|
|
16
|
+
releaseId: string;
|
|
17
|
+
runId: string;
|
|
18
|
+
token: string;
|
|
19
|
+
now: string;
|
|
20
|
+
latenessSeconds: number;
|
|
21
|
+
finish: ReleaseFinish;
|
|
22
|
+
/**
|
|
23
|
+
* An extra condition every statement carries — the site CAS of `publishRelease`
|
|
24
|
+
* (`EXISTS (SELECT 1 FROM nodes WHERE id = :site AND hash = :hash')`).
|
|
25
|
+
*/
|
|
26
|
+
guard?: SqlStatement;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Statements 5–7 of §6.3, each guarded on the lease token and, optionally, on `guard`. The release
|
|
30
|
+
* row is updated last, so a caller reads the batch's last result to know the release finished.
|
|
31
|
+
*/
|
|
32
|
+
export declare function releaseFinishStatements(input: ReleaseFinishInput): SqlStatement[];
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The statements that end a claimed release (RFC-0008 §6.3 statements 5–7). Temporal-free and
|
|
3
|
+
* storage-free, so `publishRelease` (`@cloudhal/core-ts`, through `@cloudhal/schedules/engine`)
|
|
4
|
+
* appends them to its own guarded batch; `SqlScheduleStore.finishRelease` runs them alone.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const j = (v ) => (v === undefined || v === null ? null : JSON.stringify(v));
|
|
11
|
+
|
|
12
|
+
/** The run row's state for a release's terminal state. */
|
|
13
|
+
export function runStateForRelease(finish )
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
{
|
|
18
|
+
switch (finish.state) {
|
|
19
|
+
case "published":
|
|
20
|
+
return { state: "succeeded", outcome: "did-work", error: null };
|
|
21
|
+
case "missed":
|
|
22
|
+
return { state: "missed", outcome: null, error: null };
|
|
23
|
+
case "conflict":
|
|
24
|
+
return { state: "failed", outcome: "nothing-to-do", error: { code: "conflict" } };
|
|
25
|
+
case "failed":
|
|
26
|
+
return {
|
|
27
|
+
state: "failed",
|
|
28
|
+
outcome: null,
|
|
29
|
+
error: { code: finish.outcome.kind === "failed" ? finish.outcome.code : "failed" },
|
|
30
|
+
};
|
|
31
|
+
case "expired":
|
|
32
|
+
return {
|
|
33
|
+
state: "failed",
|
|
34
|
+
outcome: null,
|
|
35
|
+
error: {
|
|
36
|
+
code: finish.outcome.kind === "expired" ? `expired_${finish.outcome.reason}` : "expired",
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
case "canceled":
|
|
40
|
+
return { state: "failed", outcome: null, error: { code: "canceled" } };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Statements 5–7 of §6.3, each guarded on the lease token and, optionally, on `guard`. The release
|
|
60
|
+
* row is updated last, so a caller reads the batch's last result to know the release finished.
|
|
61
|
+
*/
|
|
62
|
+
export function releaseFinishStatements(input ) {
|
|
63
|
+
const extra = input.guard ? ` AND ${input.guard.sql}` : "";
|
|
64
|
+
const extraParams = input.guard?.params ?? [];
|
|
65
|
+
const owned =
|
|
66
|
+
"EXISTS (SELECT 1 FROM releases WHERE id = ? AND lease_token = ? AND state = 'running')";
|
|
67
|
+
const run = runStateForRelease(input.finish);
|
|
68
|
+
return [
|
|
69
|
+
{
|
|
70
|
+
sql: `UPDATE schedule_runs SET state = ?, outcome = ?, error = ?, ended_at = ?, lateness_s = ?, lease_until = NULL, rev = rev + 1, updated_at = ?
|
|
71
|
+
WHERE id = ? AND lease_token = ? AND ${owned}${extra}`,
|
|
72
|
+
params: [
|
|
73
|
+
run.state,
|
|
74
|
+
run.outcome ?? null,
|
|
75
|
+
j(run.error),
|
|
76
|
+
input.now,
|
|
77
|
+
input.latenessSeconds,
|
|
78
|
+
input.now,
|
|
79
|
+
input.runId,
|
|
80
|
+
input.token,
|
|
81
|
+
input.releaseId,
|
|
82
|
+
input.token,
|
|
83
|
+
...extraParams,
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
sql: `UPDATE release_items SET held = 0 WHERE release_id = ? AND ${owned}${extra}`,
|
|
88
|
+
params: [input.releaseId, input.releaseId, input.token, ...extraParams],
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
sql: `UPDATE releases SET state = ?, outcome = ?, lease_token = NULL, lease_until = NULL, rev = rev + 1, updated_at = ?
|
|
92
|
+
WHERE id = ? AND lease_token = ? AND state = 'running'${extra}`,
|
|
93
|
+
params: [
|
|
94
|
+
input.finish.state,
|
|
95
|
+
JSON.stringify(input.finish.outcome),
|
|
96
|
+
input.now,
|
|
97
|
+
input.releaseId,
|
|
98
|
+
input.token,
|
|
99
|
+
...extraParams,
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules/sql — `SqlScheduleStore` over `SqlDriver` (SQLite locally and on Node, D1 on
|
|
3
|
+
* Cloudflare) for the tables of migration `0017_schedules`. Worker-safe.
|
|
4
|
+
*/
|
|
5
|
+
export { toAutomation, toRelease, toRun } from "./rows.js";
|
|
6
|
+
export { type CreateAutomationInput, type CreateReleaseInput, type ReleaseChange, type ReleaseRecord, type RunQuery, type ScheduleSettings, SqlScheduleStore, type SqlScheduleStoreOptions, } from "./store.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @cloudhal/schedules/sql — `SqlScheduleStore` over `SqlDriver` (SQLite locally and on Node, D1 on
|
|
3
|
+
* Cloudflare) for the tables of migration `0017_schedules`. Worker-safe.
|
|
4
|
+
*/
|
|
5
|
+
export { toAutomation, toRelease, toRun } from "./rows.js";
|
|
6
|
+
export {
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
SqlScheduleStore,
|
|
14
|
+
|
|
15
|
+
} from "./store.js";
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row ↔ contract mapping for the tables of migration `0017_schedules`.
|
|
3
|
+
*
|
|
4
|
+
* `schedule_runs.subject_kind` is `release`, `builtin` or `agent-task` (the contract's subject kind,
|
|
5
|
+
* with an automation's job kind folded in so a run maps back to its `ScheduleRunSubject` alone).
|
|
6
|
+
*/
|
|
7
|
+
import type { Automation, Release, ReleaseNodeKind, ScheduleRun } from "@cloudhal/core-contracts";
|
|
8
|
+
import type { TickTrigger } from "../budget-meter.js";
|
|
9
|
+
import type { BuiltinCursor, Heartbeat, StoredReleaseItem, SystemJobState } from "../scheduler.js";
|
|
10
|
+
export interface ReleaseRow extends Record<string, unknown> {
|
|
11
|
+
id: string;
|
|
12
|
+
site_id: string;
|
|
13
|
+
title: string | null;
|
|
14
|
+
state: Release["state"];
|
|
15
|
+
hash: string;
|
|
16
|
+
when_kind: "at" | "on-approval" | null;
|
|
17
|
+
execute_at: string | null;
|
|
18
|
+
time_zone: string | null;
|
|
19
|
+
local_time: string | null;
|
|
20
|
+
approval: string | null;
|
|
21
|
+
request_id: string | null;
|
|
22
|
+
outcome: string | null;
|
|
23
|
+
purge_plan: string | null;
|
|
24
|
+
lease_token: string | null;
|
|
25
|
+
lease_until: string | null;
|
|
26
|
+
created_by: string;
|
|
27
|
+
metadata: string | null;
|
|
28
|
+
rev: number;
|
|
29
|
+
created_at: string;
|
|
30
|
+
updated_at: string;
|
|
31
|
+
/** JSON array of item rows (`ITEMS_JSON`). */
|
|
32
|
+
items_json?: string | null;
|
|
33
|
+
}
|
|
34
|
+
export interface ItemRow {
|
|
35
|
+
node_id: string;
|
|
36
|
+
op: "set" | "remove";
|
|
37
|
+
node_kind: ReleaseNodeKind;
|
|
38
|
+
target_rev: number | null;
|
|
39
|
+
target_hash: string | null;
|
|
40
|
+
base_rev: number | null;
|
|
41
|
+
base_hash: string | null;
|
|
42
|
+
deps: string;
|
|
43
|
+
blob_sha: string | null;
|
|
44
|
+
held: number;
|
|
45
|
+
}
|
|
46
|
+
/** A correlated subquery that returns a release's items as one JSON array (one query per release list). */
|
|
47
|
+
export declare const ITEMS_JSON = "(SELECT json_group_array(json_object('node_id', i.node_id, 'op', i.op, 'node_kind', i.node_kind,\n 'target_rev', i.target_rev, 'target_hash', i.target_hash, 'base_rev', i.base_rev, 'base_hash', i.base_hash,\n 'deps', i.deps, 'blob_sha', i.blob_sha, 'held', i.held)) FROM release_items i WHERE i.release_id = r.id) AS items_json";
|
|
48
|
+
export declare function toItem(row: ItemRow): StoredReleaseItem;
|
|
49
|
+
export declare function itemsOf(row: ReleaseRow): StoredReleaseItem[];
|
|
50
|
+
export declare function toRelease(row: ReleaseRow, items: readonly StoredReleaseItem[]): Release;
|
|
51
|
+
export interface AutomationRow extends Record<string, unknown> {
|
|
52
|
+
id: string;
|
|
53
|
+
site_id: string;
|
|
54
|
+
name: string;
|
|
55
|
+
job: string;
|
|
56
|
+
key_id: string | null;
|
|
57
|
+
start_local: string;
|
|
58
|
+
time_zone: string;
|
|
59
|
+
rule: string | null;
|
|
60
|
+
preset: Automation["recurrence"]["preset"];
|
|
61
|
+
state: Automation["state"];
|
|
62
|
+
paused_reason: Automation["pausedReason"] | null;
|
|
63
|
+
consecutive_failures: number;
|
|
64
|
+
owner_id: string;
|
|
65
|
+
owner_name: string | null;
|
|
66
|
+
next_run_at: string | null;
|
|
67
|
+
cursor: string | null;
|
|
68
|
+
lease_token: string | null;
|
|
69
|
+
lease_until: string | null;
|
|
70
|
+
metadata: string | null;
|
|
71
|
+
rev: number;
|
|
72
|
+
created_at: string;
|
|
73
|
+
updated_at: string;
|
|
74
|
+
}
|
|
75
|
+
export declare function toAutomation(row: AutomationRow): Automation;
|
|
76
|
+
export declare function cursorOf(row: AutomationRow): BuiltinCursor | undefined;
|
|
77
|
+
export interface RunRow extends Record<string, unknown> {
|
|
78
|
+
id: string;
|
|
79
|
+
site_id: string;
|
|
80
|
+
subject_kind: "release" | "builtin" | "agent-task";
|
|
81
|
+
subject_id: string;
|
|
82
|
+
occurrence_at: string;
|
|
83
|
+
state: ScheduleRun["state"];
|
|
84
|
+
outcome: ScheduleRun["outcome"] | null;
|
|
85
|
+
trigger: TickTrigger | null;
|
|
86
|
+
attempt: number;
|
|
87
|
+
lease_token: string | null;
|
|
88
|
+
lease_until: string | null;
|
|
89
|
+
window_opens: string | null;
|
|
90
|
+
window_closes: string | null;
|
|
91
|
+
started_at: string | null;
|
|
92
|
+
ended_at: string | null;
|
|
93
|
+
lateness_s: number | null;
|
|
94
|
+
adjusted: ScheduleRun["adjusted"] | null;
|
|
95
|
+
usage: string | null;
|
|
96
|
+
taint: string | null;
|
|
97
|
+
summary: string | null;
|
|
98
|
+
counts: string | null;
|
|
99
|
+
findings: string | null;
|
|
100
|
+
approval_ids: string | null;
|
|
101
|
+
error: string | null;
|
|
102
|
+
rev: number;
|
|
103
|
+
created_at: string;
|
|
104
|
+
updated_at: string;
|
|
105
|
+
}
|
|
106
|
+
export declare function toRun(row: RunRow): ScheduleRun;
|
|
107
|
+
export interface SystemJobRow extends Record<string, unknown> {
|
|
108
|
+
name: string;
|
|
109
|
+
next_run_at: string;
|
|
110
|
+
last_run_at: string | null;
|
|
111
|
+
last_ok_at: string | null;
|
|
112
|
+
lease_token: string | null;
|
|
113
|
+
lease_until: string | null;
|
|
114
|
+
last_error: string | null;
|
|
115
|
+
}
|
|
116
|
+
export declare function toSystemJob(row: SystemJobRow): SystemJobState;
|
|
117
|
+
export interface HeartbeatRow extends Record<string, unknown> {
|
|
118
|
+
at: string | null;
|
|
119
|
+
trigger: TickTrigger | null;
|
|
120
|
+
next_wake_at: string | null;
|
|
121
|
+
ran: number | null;
|
|
122
|
+
more: number | null;
|
|
123
|
+
lag_s: number | null;
|
|
124
|
+
}
|
|
125
|
+
export declare function toHeartbeat(row: HeartbeatRow | undefined): Heartbeat | undefined;
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Row ↔ contract mapping for the tables of migration `0017_schedules`.
|
|
3
|
+
*
|
|
4
|
+
* `schedule_runs.subject_kind` is `release`, `builtin` or `agent-task` (the contract's subject kind,
|
|
5
|
+
* with an automation's job kind folded in so a run maps back to its `ScheduleRunSubject` alone).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
/** A correlated subquery that returns a release's items as one JSON array (one query per release list). */
|
|
60
|
+
export const ITEMS_JSON = `(SELECT json_group_array(json_object('node_id', i.node_id, 'op', i.op, 'node_kind', i.node_kind,
|
|
61
|
+
'target_rev', i.target_rev, 'target_hash', i.target_hash, 'base_rev', i.base_rev, 'base_hash', i.base_hash,
|
|
62
|
+
'deps', i.deps, 'blob_sha', i.blob_sha, 'held', i.held)) FROM release_items i WHERE i.release_id = r.id) AS items_json`;
|
|
63
|
+
|
|
64
|
+
const parse = (s ) =>
|
|
65
|
+
s == null ? undefined : (JSON.parse(s) );
|
|
66
|
+
|
|
67
|
+
export function toItem(row ) {
|
|
68
|
+
const base =
|
|
69
|
+
row.base_rev != null && row.base_hash != null
|
|
70
|
+
? { base: { rev: Number(row.base_rev), hash: row.base_hash } }
|
|
71
|
+
: {};
|
|
72
|
+
const item =
|
|
73
|
+
row.op === "set"
|
|
74
|
+
? {
|
|
75
|
+
kind: "set",
|
|
76
|
+
nodeId: row.node_id,
|
|
77
|
+
nodeKind: row.node_kind,
|
|
78
|
+
target: { rev: Number(row.target_rev), hash: String(row.target_hash) },
|
|
79
|
+
...base,
|
|
80
|
+
}
|
|
81
|
+
: ({ kind: "remove", nodeId: row.node_id, nodeKind: row.node_kind, ...base } );
|
|
82
|
+
return {
|
|
83
|
+
item,
|
|
84
|
+
deps: parse (row.deps) ?? [],
|
|
85
|
+
...(row.blob_sha ? { blobSha: row.blob_sha } : {}),
|
|
86
|
+
held: Number(row.held) === 1,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function itemsOf(row ) {
|
|
91
|
+
const rows = parse (row.items_json ?? "[]") ?? [];
|
|
92
|
+
return rows
|
|
93
|
+
.map(toItem)
|
|
94
|
+
.sort((a, b) => (a.item.nodeId < b.item.nodeId ? -1 : a.item.nodeId > b.item.nodeId ? 1 : 0));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function toRelease(row , items ) {
|
|
98
|
+
const when =
|
|
99
|
+
row.when_kind === "at"
|
|
100
|
+
? {
|
|
101
|
+
when: {
|
|
102
|
+
kind: "at" ,
|
|
103
|
+
at: String(row.execute_at),
|
|
104
|
+
timeZone: String(row.time_zone),
|
|
105
|
+
...(row.local_time ? { localTime: row.local_time } : {}),
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
: row.when_kind === "on-approval"
|
|
109
|
+
? { when: { kind: "on-approval" } }
|
|
110
|
+
: {};
|
|
111
|
+
const approval = parse (row.approval);
|
|
112
|
+
const outcome = parse (row.outcome);
|
|
113
|
+
const metadata = parse (row.metadata);
|
|
114
|
+
return {
|
|
115
|
+
schemaVersion: 1,
|
|
116
|
+
id: row.id,
|
|
117
|
+
siteId: row.site_id,
|
|
118
|
+
...(row.title != null ? { title: row.title } : {}),
|
|
119
|
+
items: items.map((i) => i.item) ,
|
|
120
|
+
...when,
|
|
121
|
+
state: row.state,
|
|
122
|
+
hash: row.hash,
|
|
123
|
+
...(approval ? { approval } : {}),
|
|
124
|
+
...(outcome ? { outcome } : {}),
|
|
125
|
+
createdBy: JSON.parse(row.created_by) ,
|
|
126
|
+
createdAt: row.created_at,
|
|
127
|
+
updatedAt: row.updated_at,
|
|
128
|
+
rev: Number(row.rev),
|
|
129
|
+
...(metadata ? { metadata } : {}),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
export function toAutomation(row ) {
|
|
159
|
+
const metadata = parse (row.metadata);
|
|
160
|
+
return {
|
|
161
|
+
schemaVersion: 1,
|
|
162
|
+
id: row.id,
|
|
163
|
+
siteId: row.site_id,
|
|
164
|
+
name: row.name,
|
|
165
|
+
job: JSON.parse(row.job) ,
|
|
166
|
+
recurrence: {
|
|
167
|
+
start: row.start_local,
|
|
168
|
+
timeZone: row.time_zone,
|
|
169
|
+
...(row.rule ? { rule: row.rule } : {}),
|
|
170
|
+
preset: row.preset,
|
|
171
|
+
},
|
|
172
|
+
state: row.state,
|
|
173
|
+
...(row.paused_reason ? { pausedReason: row.paused_reason } : {}),
|
|
174
|
+
consecutiveFailures: Number(row.consecutive_failures),
|
|
175
|
+
owner: { id: row.owner_id, ...(row.owner_name ? { name: row.owner_name } : {}) },
|
|
176
|
+
...(row.next_run_at ? { nextRunAt: row.next_run_at } : {}),
|
|
177
|
+
createdAt: row.created_at,
|
|
178
|
+
updatedAt: row.updated_at,
|
|
179
|
+
rev: Number(row.rev),
|
|
180
|
+
...(metadata ? { metadata } : {}),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function cursorOf(row ) {
|
|
185
|
+
return parse (row.cursor);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
export function toRun(row ) {
|
|
219
|
+
const subject =
|
|
220
|
+
row.subject_kind === "release"
|
|
221
|
+
? { kind: "release", releaseId: row.subject_id }
|
|
222
|
+
: { kind: "automation", automationId: row.subject_id, job: row.subject_kind };
|
|
223
|
+
const usage = parse (row.usage);
|
|
224
|
+
const taint = parse (row.taint);
|
|
225
|
+
const counts = parse (row.counts);
|
|
226
|
+
const approvalIds = parse (row.approval_ids);
|
|
227
|
+
const error = parse (row.error);
|
|
228
|
+
return {
|
|
229
|
+
schemaVersion: 1,
|
|
230
|
+
id: row.id,
|
|
231
|
+
siteId: row.site_id,
|
|
232
|
+
subject,
|
|
233
|
+
occurrenceAt: row.occurrence_at,
|
|
234
|
+
state: row.state,
|
|
235
|
+
...(row.outcome ? { outcome: row.outcome } : {}),
|
|
236
|
+
...(row.trigger ? { trigger: row.trigger } : {}),
|
|
237
|
+
attempt: Number(row.attempt),
|
|
238
|
+
...(row.started_at ? { startedAt: row.started_at } : {}),
|
|
239
|
+
...(row.ended_at ? { endedAt: row.ended_at } : {}),
|
|
240
|
+
...(row.lateness_s != null ? { latenessSeconds: Number(row.lateness_s) } : {}),
|
|
241
|
+
...(row.adjusted ? { adjusted: row.adjusted } : {}),
|
|
242
|
+
...(row.subject_kind === "agent-task" && row.window_opens && row.window_closes
|
|
243
|
+
? { window: { opensAt: row.window_opens, closesAt: row.window_closes } }
|
|
244
|
+
: {}),
|
|
245
|
+
...(usage ? { usage } : {}),
|
|
246
|
+
...(taint ? { taint } : {}),
|
|
247
|
+
...(row.summary != null ? { summary: row.summary } : {}),
|
|
248
|
+
...(counts ? { counts } : {}),
|
|
249
|
+
...(approvalIds && approvalIds.length > 0 ? { approvalIds } : {}),
|
|
250
|
+
...(error ? { error } : {}),
|
|
251
|
+
createdAt: row.created_at,
|
|
252
|
+
updatedAt: row.updated_at,
|
|
253
|
+
rev: Number(row.rev),
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
export function toSystemJob(row ) {
|
|
268
|
+
return {
|
|
269
|
+
name: row.name,
|
|
270
|
+
nextRunAt: row.next_run_at,
|
|
271
|
+
...(row.last_run_at ? { lastRunAt: row.last_run_at } : {}),
|
|
272
|
+
...(row.last_ok_at ? { lastOkAt: row.last_ok_at } : {}),
|
|
273
|
+
...(row.last_error ? { lastError: row.last_error } : {}),
|
|
274
|
+
...(row.lease_until ? { leaseUntil: row.lease_until } : {}),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
export function toHeartbeat(row ) {
|
|
288
|
+
if (!row?.at || !row.trigger || !row.next_wake_at) return undefined;
|
|
289
|
+
return {
|
|
290
|
+
at: row.at,
|
|
291
|
+
trigger: row.trigger,
|
|
292
|
+
nextWakeAt: row.next_wake_at,
|
|
293
|
+
ran: Number(row.ran ?? 0),
|
|
294
|
+
more: Number(row.more ?? 0) === 1,
|
|
295
|
+
...(row.lag_s != null ? { lagSeconds: Number(row.lag_s) } : {}),
|
|
296
|
+
};
|
|
297
|
+
}
|