@connextar/house 0.23.1 → 0.24.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 +55 -13
- package/dist/analytics/browser.d.ts +1 -0
- package/dist/analytics/browser.d.ts.map +1 -1
- package/dist/analytics/browser.js +22 -5
- package/dist/analytics/browser.js.map +1 -1
- package/dist/analytics/config.d.ts +5 -0
- package/dist/analytics/config.d.ts.map +1 -1
- package/dist/analytics/config.js +4 -1
- package/dist/analytics/config.js.map +1 -1
- package/dist/analytics/posthog-extensions.d.ts +19 -0
- package/dist/analytics/posthog-extensions.d.ts.map +1 -0
- package/dist/analytics/posthog-extensions.js +19 -0
- package/dist/analytics/posthog-extensions.js.map +1 -0
- package/dist/analytics/react.d.ts.map +1 -1
- package/dist/analytics/react.js +4 -2
- package/dist/analytics/react.js.map +1 -1
- package/dist/analytics/redact.d.ts.map +1 -1
- package/dist/analytics/redact.js +6 -1
- package/dist/analytics/redact.js.map +1 -1
- package/dist/house.css +19 -0
- package/package.json +17 -3
- package/src/account/data-contract.ts +112 -0
- package/src/account/index.ts +11 -0
- package/src/admin/access.ts +267 -0
- package/src/admin/audit.ts +238 -0
- package/src/admin/bootstrap.ts +220 -0
- package/src/admin/client-index.ts +19 -0
- package/src/admin/client.tsx +187 -0
- package/src/admin/index.ts +91 -0
- package/src/admin/proof.ts +155 -0
- package/src/admin/second-factor-index.ts +16 -0
- package/src/admin/second-factor.ts +403 -0
- package/src/admin/testing-index.ts +13 -0
- package/src/admin/testing.ts +299 -0
- package/src/admin/view-as.ts +511 -0
- package/src/ai/config-index.ts +40 -0
- package/src/ai/config.ts +176 -0
- package/src/ai/images.ts +18 -0
- package/src/ai/index.ts +21 -0
- package/src/ai/pricing.ts +107 -0
- package/src/ai/provider.ts +403 -0
- package/src/analytics/browser.ts +400 -0
- package/src/analytics/client-index.ts +12 -0
- package/src/analytics/config.ts +89 -0
- package/src/analytics/index.ts +17 -0
- package/src/analytics/ingest.ts +81 -0
- package/src/analytics/posthog-extensions.ts +19 -0
- package/src/analytics/react-index.ts +16 -0
- package/src/analytics/react.tsx +241 -0
- package/src/analytics/redact.ts +146 -0
- package/src/analytics/server-index.ts +17 -0
- package/src/analytics/server.ts +277 -0
- package/src/auth/index.ts +30 -0
- package/src/auth/second-factor-index.ts +16 -0
- package/src/auth/second-factor-next.ts +136 -0
- package/src/auth/second-factor-routes.ts +40 -0
- package/src/auth/second-factor.ts +226 -0
- package/src/auth/step-up-client-index.ts +13 -0
- package/src/auth/step-up-client.ts +23 -0
- package/src/auth/step-up-index.ts +19 -0
- package/src/auth/step-up-routes.ts +99 -0
- package/src/auth/step-up.ts +432 -0
- package/src/auth/two-factor-policy.ts +112 -0
- package/src/changelog/changelog.tsx +367 -0
- package/src/changelog/index.ts +12 -0
- package/src/editor/block-handle.tsx +312 -0
- package/src/editor/blocks.ts +169 -0
- package/src/editor/bubble-toolbar.tsx +208 -0
- package/src/editor/collapsible.ts +159 -0
- package/src/editor/content-index.ts +1 -0
- package/src/editor/content.tsx +343 -0
- package/src/editor/editor.tsx +333 -0
- package/src/editor/emoji-index.ts +1 -0
- package/src/editor/emoji-picker.tsx +114 -0
- package/src/editor/emoji.ts +436 -0
- package/src/editor/index.ts +6 -0
- package/src/editor/sections.ts +83 -0
- package/src/editor/suggestion-popup.tsx +136 -0
- package/src/editor/suggestions.ts +309 -0
- package/src/editor/ui.tsx +171 -0
- package/src/errors/error-shell.tsx +72 -0
- package/src/errors/global-error-shell.tsx +116 -0
- package/src/errors/index.ts +2 -0
- package/src/grid/columns.ts +48 -0
- package/src/grid/compare.ts +35 -0
- package/src/grid/hidden-columns.ts +95 -0
- package/src/grid/index.ts +27 -0
- package/src/grid/use-grid.ts +282 -0
- package/src/grid/views.ts +70 -0
- package/src/guides/guide-article.tsx +92 -0
- package/src/guides/guide-figure-zoom.tsx +533 -0
- package/src/guides/guide-figure.tsx +68 -0
- package/src/guides/guide-toc.tsx +133 -0
- package/src/guides/guides-landing.tsx +57 -0
- package/src/guides/guides-search.tsx +160 -0
- package/src/guides/guides-shell.tsx +60 -0
- package/src/guides/guides-sidebar.tsx +93 -0
- package/src/guides/index.ts +28 -0
- package/src/guides/library.ts +165 -0
- package/src/health/health.ts +115 -0
- package/src/health/index.ts +11 -0
- package/src/internal/cx.ts +4 -0
- package/src/internal/next-link.ts +23 -0
- package/src/jobs/counts.ts +151 -0
- package/src/jobs/cron-auth.ts +60 -0
- package/src/jobs/health.ts +315 -0
- package/src/jobs/idle.ts +86 -0
- package/src/jobs/index.ts +120 -0
- package/src/jobs/job-health-list.tsx +316 -0
- package/src/jobs/next-index.ts +4 -0
- package/src/jobs/next.ts +65 -0
- package/src/jobs/prisma-index.ts +12 -0
- package/src/jobs/prisma.ts +170 -0
- package/src/jobs/prune.ts +79 -0
- package/src/jobs/react-index.ts +35 -0
- package/src/jobs/registry.ts +189 -0
- package/src/jobs/scheduler.ts +437 -0
- package/src/jobs/store.ts +212 -0
- package/src/jobs/windows.ts +157 -0
- package/src/maintenance/core.ts +270 -0
- package/src/maintenance/gate.ts +171 -0
- package/src/maintenance/index.ts +40 -0
- package/src/maintenance/page.ts +76 -0
- package/src/maintenance/proxy.ts +88 -0
- package/src/maintenance/react-index.ts +7 -0
- package/src/maintenance/react.tsx +190 -0
- package/src/maintenance/route.ts +82 -0
- package/src/maintenance/unavailable.ts +61 -0
- package/src/markdown/figure.tsx +132 -0
- package/src/markdown/figures.ts +237 -0
- package/src/markdown/guide.tsx +536 -0
- package/src/markdown/index.ts +53 -0
- package/src/markdown/table-view.tsx +74 -0
- package/src/markdown/table.ts +90 -0
- package/src/password/common.ts +58 -0
- package/src/password/estimate-index.ts +18 -0
- package/src/password/estimate.ts +715 -0
- package/src/password/index.ts +40 -0
- package/src/password/password-input.tsx +116 -0
- package/src/password/password-strength.tsx +157 -0
- package/src/password/reveal.ts +151 -0
- package/src/rate-limit/better-auth.ts +94 -0
- package/src/rate-limit/index.ts +32 -0
- package/src/rate-limit/keys.ts +110 -0
- package/src/rate-limit/limiter.ts +276 -0
- package/src/rate-limit/prisma-index.ts +12 -0
- package/src/rate-limit/prisma.ts +157 -0
- package/src/rate-limit/store.ts +47 -0
- package/src/release/changelog.ts +364 -0
- package/src/release/index.ts +32 -0
- package/src/release/version.ts +64 -0
- package/src/setup-guide/bits.tsx +420 -0
- package/src/setup-guide/card.tsx +358 -0
- package/src/setup-guide/class-names.ts +144 -0
- package/src/setup-guide/context.tsx +239 -0
- package/src/setup-guide/guide.ts +271 -0
- package/src/setup-guide/index.ts +76 -0
- package/src/setup-guide/launcher.tsx +207 -0
- package/src/setup-guide/strip.tsx +127 -0
- package/src/styles/house.css +403 -0
- package/src/trail/index.ts +12 -0
- package/src/trail/rules.ts +84 -0
- package/src/trail/trail.tsx +367 -0
- package/src/ux/errors.ts +32 -0
- package/src/ux/feedback.tsx +41 -0
- package/src/ux/index.ts +44 -0
- package/src/ux/navigation-index.ts +16 -0
- package/src/ux/navigation-progress.tsx +270 -0
- package/src/ux/optimistic.ts +415 -0
- package/src/ux/use-action.ts +155 -0
- package/src/ux/use-optimistic-list.ts +227 -0
- package/src/ux/use-optimistic-value.ts +136 -0
- package/src/web-search/index.ts +13 -0
- package/src/web-search/providers.ts +177 -0
- package/src/web-search/types.ts +98 -0
- package/src/whats-new/body.tsx +190 -0
- package/src/whats-new/index.ts +43 -0
- package/src/whats-new/seen.ts +40 -0
- package/src/whats-new/select.ts +230 -0
- package/src/whats-new/use-whats-new.ts +109 -0
- package/src/wizard/index.ts +61 -0
- package/src/wizard/invalid.ts +152 -0
- package/src/wizard/react-hook-form-index.ts +11 -0
- package/src/wizard/rhf-adapter.ts +67 -0
- package/src/wizard/rules.ts +137 -0
- package/src/wizard/wizard.tsx +442 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { countLines, describeSpan, type CountFormatOptions, type CountLine } from "./counts.js";
|
|
2
|
+
import { runNowScopes, type AnyJob, type RunNowScope } from "./registry.js";
|
|
3
|
+
import type { JobRunRecord, JobSwitchState } from "./store.js";
|
|
4
|
+
import { isManualKey } from "./windows.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A job's health — derived every time it is shown, never stored.
|
|
8
|
+
*
|
|
9
|
+
* A stored status is a second copy of the truth that drifts from the first.
|
|
10
|
+
* Health here is a function of the ledger, the switch and the clock, so the
|
|
11
|
+
* card cannot say "Healthy" about a job whose last run failed an hour ago.
|
|
12
|
+
*
|
|
13
|
+
* Six states, in the order they win:
|
|
14
|
+
*
|
|
15
|
+
* 1. **paused** — switched off. Whatever the runs say, nothing new will start.
|
|
16
|
+
* 2. **never-run** — no run this viewer can see.
|
|
17
|
+
* 3. **running** — the latest run has not finished, and is inside its timeout.
|
|
18
|
+
* 4. **failed** — the latest run failed, or started and never finished. Both
|
|
19
|
+
* apps showed a run still in progress as "Errored", because an unfinished
|
|
20
|
+
* row has `ok = false`; and a run cut off by the platform's time limit stayed
|
|
21
|
+
* that way for ever with no message at all. Past its timeout it is a failure
|
|
22
|
+
* that says so.
|
|
23
|
+
* 5. **stale** — no successful *whole* run for longer than the threshold (a
|
|
24
|
+
* run exactly at the threshold is still healthy). A tenant's own Run now
|
|
25
|
+
* sweeps one tenant, so it proves nothing about the schedule and does not
|
|
26
|
+
* reset the clock — in Quickie it did, so one owner's click made a job look
|
|
27
|
+
* healthy to everyone. A platform Run now sweeps every tenant, as the
|
|
28
|
+
* schedule does, so it counts: an operator re-running a failed sweep puts
|
|
29
|
+
* the job right for everyone.
|
|
30
|
+
* 6. **healthy**.
|
|
31
|
+
*/
|
|
32
|
+
export type JobHealthState = "paused" | "never-run" | "running" | "failed" | "stale" | "healthy";
|
|
33
|
+
|
|
34
|
+
export type JobHealth =
|
|
35
|
+
| { state: "paused" }
|
|
36
|
+
| { state: "never-run" }
|
|
37
|
+
| { state: "running"; since: string }
|
|
38
|
+
| { state: "failed"; at: string; error: string | null; unfinished: boolean }
|
|
39
|
+
| { state: "stale"; lastSuccessAt: string | null; thresholdMs: number }
|
|
40
|
+
| { state: "healthy"; lastSuccessAt: string };
|
|
41
|
+
|
|
42
|
+
/** The fields of a run that health reads. */
|
|
43
|
+
export interface RunFacts {
|
|
44
|
+
startedAt: Date;
|
|
45
|
+
finishedAt: Date | null;
|
|
46
|
+
ok: boolean;
|
|
47
|
+
error: string | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface HealthInput {
|
|
51
|
+
paused: boolean;
|
|
52
|
+
/** The latest run the viewer can see. */
|
|
53
|
+
latest: RunFacts | null;
|
|
54
|
+
/** The latest successful run over every tenant — the one staleness is measured from. */
|
|
55
|
+
lastWholeSuccess: RunFacts | null;
|
|
56
|
+
staleAfterMs: number | null;
|
|
57
|
+
timeoutMs: number;
|
|
58
|
+
now: Date;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function deriveHealth(input: HealthInput): JobHealth {
|
|
62
|
+
const { latest, lastWholeSuccess, now } = input;
|
|
63
|
+
if (input.paused) return { state: "paused" };
|
|
64
|
+
if (!latest && !lastWholeSuccess) return { state: "never-run" };
|
|
65
|
+
if (latest && !latest.finishedAt) {
|
|
66
|
+
if (now.getTime() - latest.startedAt.getTime() <= input.timeoutMs) {
|
|
67
|
+
return { state: "running", since: latest.startedAt.toISOString() };
|
|
68
|
+
}
|
|
69
|
+
return { state: "failed", at: latest.startedAt.toISOString(), error: null, unfinished: true };
|
|
70
|
+
}
|
|
71
|
+
if (latest && !latest.ok) {
|
|
72
|
+
return { state: "failed", at: latest.startedAt.toISOString(), error: latest.error, unfinished: false };
|
|
73
|
+
}
|
|
74
|
+
if (
|
|
75
|
+
input.staleAfterMs !== null &&
|
|
76
|
+
(!lastWholeSuccess || now.getTime() - lastWholeSuccess.startedAt.getTime() > input.staleAfterMs)
|
|
77
|
+
) {
|
|
78
|
+
return {
|
|
79
|
+
state: "stale",
|
|
80
|
+
lastSuccessAt: lastWholeSuccess?.startedAt.toISOString() ?? null,
|
|
81
|
+
thresholdMs: input.staleAfterMs,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const success = lastWholeSuccess ?? latest!;
|
|
85
|
+
return { state: "healthy", lastSuccessAt: success.startedAt.toISOString() };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Who is looking. A platform viewer (AltEd's administrators, Quickie's
|
|
90
|
+
* operators) may switch jobs and sees every sweep's detail; a tenant viewer
|
|
91
|
+
* (Quickie's owners) sees their own Run now results in full, and of a sweep
|
|
92
|
+
* over every tenant only whether it ran and when.
|
|
93
|
+
*/
|
|
94
|
+
export interface JobAudience {
|
|
95
|
+
platform: boolean;
|
|
96
|
+
tenantId?: string | null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** One run, as the card shows it. Dates are ISO strings, so it crosses from a server component intact. */
|
|
100
|
+
export interface JobRunView {
|
|
101
|
+
startedAt: string;
|
|
102
|
+
finishedAt: string | null;
|
|
103
|
+
ok: boolean;
|
|
104
|
+
trigger: "schedule" | "manual";
|
|
105
|
+
by: string | null;
|
|
106
|
+
/** A tenant's Run now: it swept one tenant, not all. */
|
|
107
|
+
tenantScoped: boolean;
|
|
108
|
+
counts: CountLine[];
|
|
109
|
+
error: string | null;
|
|
110
|
+
/**
|
|
111
|
+
* Counts and error left out: a sweep over every tenant, seen by one tenant.
|
|
112
|
+
* Quickie showed each owner the platform-wide ledger, so "412 emails sent"
|
|
113
|
+
* read as theirs, and an error from another organisation's data was on every
|
|
114
|
+
* owner's settings page.
|
|
115
|
+
*/
|
|
116
|
+
withheld: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A job as the health screen draws it — plain data, every count already in words. */
|
|
120
|
+
export interface JobView {
|
|
121
|
+
id: string;
|
|
122
|
+
label: string;
|
|
123
|
+
cadence: string;
|
|
124
|
+
schedule: "tick" | "own";
|
|
125
|
+
staleAfterMs: number | null;
|
|
126
|
+
health: JobHealth;
|
|
127
|
+
/** Health with the switch ignored, so the badge is right the moment someone switches it back on. */
|
|
128
|
+
healthIfOn: JobHealth;
|
|
129
|
+
paused: boolean;
|
|
130
|
+
switchedAt: string | null;
|
|
131
|
+
switchedBy: string | null;
|
|
132
|
+
/** This viewer may switch it. */
|
|
133
|
+
canSwitch: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* This viewer may press Run now, and what it would sweep — the first of
|
|
136
|
+
* `runNowScopes`, or `null` when there are none.
|
|
137
|
+
*/
|
|
138
|
+
runNow: RunNowScope | null;
|
|
139
|
+
/**
|
|
140
|
+
* Every Run now this viewer may press, narrowest first. One scope for almost
|
|
141
|
+
* everyone: on a job that offers both, an operator (platform, no tenant) gets
|
|
142
|
+
* `["platform"]` and an owner `["tenant"]`; only a viewer holding both gets two.
|
|
143
|
+
*/
|
|
144
|
+
runNowScopes: RunNowScope[];
|
|
145
|
+
lastRun: JobRunView | null;
|
|
146
|
+
lastSuccess: JobRunView | null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** What the view needs from the store for one job. */
|
|
150
|
+
export interface JobFacts {
|
|
151
|
+
switch: JobSwitchState | null;
|
|
152
|
+
latest: JobRunRecord | null;
|
|
153
|
+
lastWholeSuccess: JobRunRecord | null;
|
|
154
|
+
lastVisibleSuccess: JobRunRecord | null;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface JobViewOptions extends CountFormatOptions {
|
|
158
|
+
now: Date;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function effectiveTimeout(job: AnyJob, fallback: number): number {
|
|
162
|
+
return job.timeoutMs ?? fallback;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Every Run now this viewer may press on this job, narrowest first: a tenant run needs a tenant, a platform run platform authority. */
|
|
166
|
+
export function runNowScopesFor(job: AnyJob, audience: JobAudience): RunNowScope[] {
|
|
167
|
+
return runNowScopes(job.runNow).filter((scope) =>
|
|
168
|
+
scope === "tenant" ? Boolean(audience.tenantId) : audience.platform,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Whether this viewer may press Run now on this job, and over whom — the narrowest scope they hold. */
|
|
173
|
+
export function runNowFor(job: AnyJob, audience: JobAudience): RunNowScope | null {
|
|
174
|
+
return runNowScopesFor(job, audience)[0] ?? null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function runView(job: AnyJob, run: JobRunRecord, audience: JobAudience, options: CountFormatOptions): JobRunView {
|
|
178
|
+
const withheld = !audience.platform && run.tenantId === null;
|
|
179
|
+
return {
|
|
180
|
+
startedAt: run.startedAt.toISOString(),
|
|
181
|
+
finishedAt: run.finishedAt?.toISOString() ?? null,
|
|
182
|
+
ok: run.ok,
|
|
183
|
+
trigger: isManualKey(run.windowKey) ? "manual" : "schedule",
|
|
184
|
+
by: run.by,
|
|
185
|
+
tenantScoped: run.tenantId !== null,
|
|
186
|
+
counts: withheld ? [] : countLines(job.counts, run.counts, options),
|
|
187
|
+
error: withheld ? null : run.error,
|
|
188
|
+
withheld,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function buildJobView(
|
|
193
|
+
job: AnyJob,
|
|
194
|
+
facts: JobFacts,
|
|
195
|
+
audience: JobAudience,
|
|
196
|
+
options: JobViewOptions & { timeoutMs: number },
|
|
197
|
+
): JobView {
|
|
198
|
+
const switchable = job.switchable ?? true;
|
|
199
|
+
const paused = switchable && Boolean(facts.switch?.paused);
|
|
200
|
+
const runNowScopes = runNowScopesFor(job, audience);
|
|
201
|
+
const lastRun = facts.latest ? runView(job, facts.latest, audience, options) : null;
|
|
202
|
+
const input = {
|
|
203
|
+
latest: facts.latest,
|
|
204
|
+
lastWholeSuccess: facts.lastWholeSuccess,
|
|
205
|
+
staleAfterMs: job.staleAfterMs,
|
|
206
|
+
timeoutMs: effectiveTimeout(job, options.timeoutMs),
|
|
207
|
+
now: options.now,
|
|
208
|
+
};
|
|
209
|
+
const healthIfOn = deriveHealth({ ...input, paused: false });
|
|
210
|
+
// A withheld failure keeps its state and loses its words.
|
|
211
|
+
const redact = (health: JobHealth): JobHealth =>
|
|
212
|
+
health.state === "failed" && lastRun?.withheld ? { ...health, error: null } : health;
|
|
213
|
+
return {
|
|
214
|
+
id: job.id,
|
|
215
|
+
label: job.label,
|
|
216
|
+
cadence: job.cadence,
|
|
217
|
+
schedule: job.schedule ?? "tick",
|
|
218
|
+
staleAfterMs: job.staleAfterMs,
|
|
219
|
+
health: redact(paused ? { state: "paused" } : healthIfOn),
|
|
220
|
+
healthIfOn: redact(healthIfOn),
|
|
221
|
+
paused,
|
|
222
|
+
switchedAt: switchable ? (facts.switch?.at?.toISOString() ?? null) : null,
|
|
223
|
+
switchedBy: switchable ? (facts.switch?.by ?? null) : null,
|
|
224
|
+
canSwitch: switchable && audience.platform,
|
|
225
|
+
runNow: runNowScopes[0] ?? null,
|
|
226
|
+
runNowScopes,
|
|
227
|
+
lastRun,
|
|
228
|
+
lastSuccess: facts.lastVisibleSuccess ? runView(job, facts.lastVisibleSuccess, audience, options) : null,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export interface WhenOptions {
|
|
233
|
+
locale?: string;
|
|
234
|
+
/**
|
|
235
|
+
* Whose clock. Default `Europe/London`. Never the runtime's own: the card is
|
|
236
|
+
* drawn on a server in UTC and again in a browser in BST, and two different
|
|
237
|
+
* times in one element is a hydration error.
|
|
238
|
+
*/
|
|
239
|
+
timeZone?: string;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** "30 Sept 2026, 14:05". */
|
|
243
|
+
export function formatWhen(iso: string, options: WhenOptions = {}): string {
|
|
244
|
+
return new Intl.DateTimeFormat(options.locale ?? "en-GB", {
|
|
245
|
+
day: "numeric",
|
|
246
|
+
month: "short",
|
|
247
|
+
year: "numeric",
|
|
248
|
+
hour: "2-digit",
|
|
249
|
+
minute: "2-digit",
|
|
250
|
+
timeZone: options.timeZone ?? "Europe/London",
|
|
251
|
+
}).format(new Date(iso));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** "29 Sept 2026". */
|
|
255
|
+
export function formatDay(iso: string, options: WhenOptions = {}): string {
|
|
256
|
+
return new Intl.DateTimeFormat(options.locale ?? "en-GB", {
|
|
257
|
+
day: "numeric",
|
|
258
|
+
month: "short",
|
|
259
|
+
year: "numeric",
|
|
260
|
+
timeZone: options.timeZone ?? "Europe/London",
|
|
261
|
+
}).format(new Date(iso));
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export interface HealthWords {
|
|
265
|
+
label: string;
|
|
266
|
+
/** A sentence under the badge, or `null` when the badge says it all. */
|
|
267
|
+
detail: string | null;
|
|
268
|
+
tone: "good" | "neutral" | "warning" | "bad";
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The badge and the sentence under it. */
|
|
272
|
+
export function describeHealth(health: JobHealth, options: WhenOptions = {}): HealthWords {
|
|
273
|
+
switch (health.state) {
|
|
274
|
+
case "paused":
|
|
275
|
+
return { label: "Paused", detail: null, tone: "neutral" };
|
|
276
|
+
case "never-run":
|
|
277
|
+
return { label: "Never run", detail: "No runs recorded yet.", tone: "neutral" };
|
|
278
|
+
case "running":
|
|
279
|
+
return { label: "Running", detail: `Started ${formatWhen(health.since, options)}.`, tone: "neutral" };
|
|
280
|
+
case "failed":
|
|
281
|
+
return {
|
|
282
|
+
label: "Failed",
|
|
283
|
+
detail: health.unfinished
|
|
284
|
+
? `Started ${formatWhen(health.at, options)} and never finished — it was probably cut off.`
|
|
285
|
+
: (health.error ?? "The last run failed."),
|
|
286
|
+
tone: "bad",
|
|
287
|
+
};
|
|
288
|
+
case "stale":
|
|
289
|
+
return {
|
|
290
|
+
label: "Stale",
|
|
291
|
+
detail: health.lastSuccessAt
|
|
292
|
+
? `No successful run in over ${describeSpan(health.thresholdMs)} — the last was ${formatWhen(health.lastSuccessAt, options)}.`
|
|
293
|
+
: `No successful run on its schedule yet; one is expected every ${describeSpan(health.thresholdMs)}.`,
|
|
294
|
+
tone: "warning",
|
|
295
|
+
};
|
|
296
|
+
case "healthy":
|
|
297
|
+
return { label: "Healthy", detail: null, tone: "good" };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** "Paused 29 Sept 2026 by AltEd Admin", "Turned back on …", or nothing if it was never switched. */
|
|
302
|
+
export function describeSwitch(
|
|
303
|
+
view: Pick<JobView, "paused" | "switchedAt" | "switchedBy">,
|
|
304
|
+
options: WhenOptions = {},
|
|
305
|
+
): string | null {
|
|
306
|
+
if (!view.switchedAt) return view.paused ? "Paused" : null;
|
|
307
|
+
const by = view.switchedBy ? ` by ${view.switchedBy}` : "";
|
|
308
|
+
return `${view.paused ? "Paused" : "Turned back on"} ${formatDay(view.switchedAt, options)}${by}`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** "Last run 30 Sept 2026, 14:05 · Run now by Ann Lee". */
|
|
312
|
+
export function describeRun(run: JobRunView, prefix: string, options: WhenOptions = {}): string {
|
|
313
|
+
const when = `${prefix} ${formatWhen(run.startedAt, options)}`;
|
|
314
|
+
return run.trigger === "manual" ? `${when} · Run now${run.by ? ` by ${run.by}` : ""}` : when;
|
|
315
|
+
}
|
package/src/jobs/idle.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { summariseTick, type Scheduler, type TickResult } from "./scheduler.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The idle tick: a tick route that leaves a serverless database asleep between windows.
|
|
5
|
+
*
|
|
6
|
+
* Vercel Cron calls the tick every few minutes, and most calls have nothing to do: every job's window is one it has
|
|
7
|
+
* already claimed. Asking the database to find that out keeps a serverless database (Neon) awake around the clock;
|
|
8
|
+
* AltEd measured about twenty compute hours a day spent that way. The scheduler already knows every job's window, so
|
|
9
|
+
* it can say, without the database, whether anything could have changed since the last call — the signature, every
|
|
10
|
+
* tick job's window key now — and the tick runs once per signature. The rest answer from a cache.
|
|
11
|
+
*
|
|
12
|
+
* Quickie wrote this first, with a module-level `ranJustNow` flag to tell a cached answer from a fresh run. One
|
|
13
|
+
* instance serves concurrent calls (Vercel's Fluid compute), so the flag was shared between them and a response could
|
|
14
|
+
* say "idle" for a run or "ran" for a cached answer. Here each call has its own answer, `{ status: "ran" | "idle" }`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Runs `run` once per signature and answers later calls with the same signature from a cache. `fresh` says whether
|
|
19
|
+
* this call ran it. If `run` throws, nothing is kept and the error comes back to this call.
|
|
20
|
+
* `@connextar/house/jobs/next` has one over Next's data cache; `memoryTickMemo` is one for tests.
|
|
21
|
+
*/
|
|
22
|
+
export type TickMemo = (
|
|
23
|
+
signature: string,
|
|
24
|
+
run: () => Promise<TickResult[]>,
|
|
25
|
+
) => Promise<{ results: TickResult[]; fresh: boolean }>;
|
|
26
|
+
|
|
27
|
+
export type IdleTickOutcome =
|
|
28
|
+
| { status: "ran"; signature: string; results: TickResult[]; summary: string }
|
|
29
|
+
| { status: "idle"; signature: string; summary: string };
|
|
30
|
+
|
|
31
|
+
/** A tick that could not finish its work: thrown out of the memo so nothing is kept, and the next call runs again. */
|
|
32
|
+
class UnfinishedTick extends Error {
|
|
33
|
+
constructor(readonly results: TickResult[]) {
|
|
34
|
+
super("The tick did not finish: a job failed before it could claim its window.");
|
|
35
|
+
this.name = "UnfinishedTick";
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A job that failed before claiming its window — its switches unreadable, its store down — must be tried again. */
|
|
40
|
+
export const tickUnfinished = (results: readonly TickResult[]) =>
|
|
41
|
+
results.some((result) => result.status === "failed" && !result.windowKey);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The tick route's body, once per signature: `{ status: "ran", results }` when this call ran the scheduler,
|
|
45
|
+
* `{ status: "idle" }` when an earlier call already ran it for the same windows. A tick in which a job failed before
|
|
46
|
+
* claiming its window is not remembered, so the next call runs it again.
|
|
47
|
+
*
|
|
48
|
+
* Expire the memo whenever something the signature cannot see changes — a job switched back on inside its window —
|
|
49
|
+
* or that job waits for its next window (`expireIdleTick` in `/jobs/next`).
|
|
50
|
+
*/
|
|
51
|
+
export async function idleTick(scheduler: Scheduler, memo: TickMemo, now: Date = new Date()): Promise<IdleTickOutcome> {
|
|
52
|
+
const signature = scheduler.signature(now);
|
|
53
|
+
try {
|
|
54
|
+
const { results, fresh } = await memo(signature, async () => {
|
|
55
|
+
const results = await scheduler.tick(now);
|
|
56
|
+
if (tickUnfinished(results)) throw new UnfinishedTick(results);
|
|
57
|
+
return results;
|
|
58
|
+
});
|
|
59
|
+
if (!fresh) return { status: "idle", signature, summary: "Nothing new since the last run in these windows" };
|
|
60
|
+
return { status: "ran", signature, results, summary: summariseTick(results) };
|
|
61
|
+
} catch (error) {
|
|
62
|
+
if (error instanceof UnfinishedTick) {
|
|
63
|
+
return { status: "ran", signature, results: error.results, summary: summariseTick(error.results) };
|
|
64
|
+
}
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A `TickMemo` in memory — for tests, and for a long-lived server with one process. */
|
|
70
|
+
export function memoryTickMemo(): TickMemo & { clear(): void } {
|
|
71
|
+
const kept = new Map<string, Promise<TickResult[]>>();
|
|
72
|
+
const memo = (async (signature, run) => {
|
|
73
|
+
const known = kept.get(signature);
|
|
74
|
+
if (known) return { results: await known, fresh: false };
|
|
75
|
+
const running = run();
|
|
76
|
+
kept.set(signature, running);
|
|
77
|
+
try {
|
|
78
|
+
return { results: await running, fresh: true };
|
|
79
|
+
} catch (error) {
|
|
80
|
+
kept.delete(signature);
|
|
81
|
+
throw error;
|
|
82
|
+
}
|
|
83
|
+
}) as TickMemo & { clear(): void };
|
|
84
|
+
memo.clear = () => kept.clear();
|
|
85
|
+
return memo;
|
|
86
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/jobs` — scheduled jobs: declared once, run once per
|
|
3
|
+
* window, switched off and on by name, and honest about their health.
|
|
4
|
+
*
|
|
5
|
+
* AltEd and Quickie each wrote this, from the same seed, and each went on to
|
|
6
|
+
* prove something the other had not: AltEd that a job must be switchable with a
|
|
7
|
+
* name and a date against it, and that some jobs run on their own clock;
|
|
8
|
+
* Quickie that Run now must be fenced to the caller's own tenant. Both printed
|
|
9
|
+
* a job's result as its variable names. This is the union, with that made
|
|
10
|
+
* impossible: a count reaches the screen only through its declaration.
|
|
11
|
+
*
|
|
12
|
+
* Server code. It owns no schema — the app implements `JobStore` over its own
|
|
13
|
+
* table (the README has the Prisma model and adapter) — and it imports no
|
|
14
|
+
* framework: the only thing it reaches for is `node:crypto`, in the tick
|
|
15
|
+
* route's secret check. The health list is `@connextar/house/jobs/react`,
|
|
16
|
+
* which re-exports the words and types a client component needs, so nothing
|
|
17
|
+
* in the browser ever imports this entry.
|
|
18
|
+
*/
|
|
19
|
+
export { checkCronRequest, type CronAuthOptions, type CronAuthResult } from "./cron-auth.js";
|
|
20
|
+
export {
|
|
21
|
+
countLines,
|
|
22
|
+
countsSummary,
|
|
23
|
+
declaredCounts,
|
|
24
|
+
describeSpan,
|
|
25
|
+
formatCount,
|
|
26
|
+
formatDuration,
|
|
27
|
+
formatMoney,
|
|
28
|
+
type CountDeclaration,
|
|
29
|
+
type CountDeclarations,
|
|
30
|
+
type CountFormatOptions,
|
|
31
|
+
type CountKind,
|
|
32
|
+
type CountLabel,
|
|
33
|
+
type CountLine,
|
|
34
|
+
type CountValues,
|
|
35
|
+
} from "./counts.js";
|
|
36
|
+
export {
|
|
37
|
+
buildJobView,
|
|
38
|
+
deriveHealth,
|
|
39
|
+
describeHealth,
|
|
40
|
+
describeRun,
|
|
41
|
+
describeSwitch,
|
|
42
|
+
formatDay,
|
|
43
|
+
formatWhen,
|
|
44
|
+
runNowFor,
|
|
45
|
+
runNowScopesFor,
|
|
46
|
+
type HealthInput,
|
|
47
|
+
type HealthWords,
|
|
48
|
+
type JobAudience,
|
|
49
|
+
type JobFacts,
|
|
50
|
+
type JobHealth,
|
|
51
|
+
type JobHealthState,
|
|
52
|
+
type JobRunView,
|
|
53
|
+
type JobView,
|
|
54
|
+
type RunFacts,
|
|
55
|
+
type WhenOptions,
|
|
56
|
+
} from "./health.js";
|
|
57
|
+
export {
|
|
58
|
+
DEFAULT_COOLDOWN_MS,
|
|
59
|
+
DEFAULT_TIMEOUT_MS,
|
|
60
|
+
MAX_JOB_ID,
|
|
61
|
+
MAX_WINDOW_KEY,
|
|
62
|
+
defineJob,
|
|
63
|
+
defineJobs,
|
|
64
|
+
jobDefinitionProblems,
|
|
65
|
+
runNowScopes,
|
|
66
|
+
type AnyJob,
|
|
67
|
+
type JobActor,
|
|
68
|
+
type JobContext,
|
|
69
|
+
type JobDefinition,
|
|
70
|
+
type RunNowRule,
|
|
71
|
+
type RunNowScope,
|
|
72
|
+
} from "./registry.js";
|
|
73
|
+
export {
|
|
74
|
+
DEFAULT_JOB_RUN_PRUNE_BATCH,
|
|
75
|
+
DEFAULT_JOB_RUN_RETENTION_DAYS,
|
|
76
|
+
JOB_RUN_PRUNE_COUNTS,
|
|
77
|
+
pruneJobRuns,
|
|
78
|
+
type JobRunPruneCounts,
|
|
79
|
+
type PruneJobRunsOptions,
|
|
80
|
+
} from "./prune.js";
|
|
81
|
+
export { idleTick, memoryTickMemo, tickUnfinished, type IdleTickOutcome, type TickMemo } from "./idle.js";
|
|
82
|
+
export {
|
|
83
|
+
createScheduler,
|
|
84
|
+
refusalStatus,
|
|
85
|
+
summariseTick,
|
|
86
|
+
tickSignature,
|
|
87
|
+
type RunNowCaller,
|
|
88
|
+
type RunNowOutcome,
|
|
89
|
+
type RunNowRefusal,
|
|
90
|
+
type Scheduler,
|
|
91
|
+
type SchedulerOptions,
|
|
92
|
+
type SwitchCaller,
|
|
93
|
+
type SwitchOutcome,
|
|
94
|
+
type SwitchRefusal,
|
|
95
|
+
type TickResult,
|
|
96
|
+
type TickStatus,
|
|
97
|
+
} from "./scheduler.js";
|
|
98
|
+
export {
|
|
99
|
+
createMemoryJobStore,
|
|
100
|
+
pausedFromStored,
|
|
101
|
+
type JobRunClaim,
|
|
102
|
+
type JobRunOutcome,
|
|
103
|
+
type JobRunPrune,
|
|
104
|
+
type JobRunQuery,
|
|
105
|
+
type JobRunRecord,
|
|
106
|
+
type JobStore,
|
|
107
|
+
type JobSwitchChange,
|
|
108
|
+
type JobSwitchState,
|
|
109
|
+
} from "./store.js";
|
|
110
|
+
export {
|
|
111
|
+
MANUAL_KEY_PREFIX,
|
|
112
|
+
daily,
|
|
113
|
+
everyHours,
|
|
114
|
+
hourly,
|
|
115
|
+
isManualKey,
|
|
116
|
+
monthly,
|
|
117
|
+
weekly,
|
|
118
|
+
type CalendarWindowOptions,
|
|
119
|
+
type JobWindow,
|
|
120
|
+
} from "./windows.js";
|