Repository navigation
Expand file tree
/
Copy pathworkflow.ts
More file actions
74 lines (70 loc) · 2.82 KB
/
Copy pathworkflow.ts
File metadata and controls
74 lines (70 loc) · 2.82 KB
1
2
3
4
5
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { WorkflowEntrypoint, type WorkflowEvent, type WorkflowStep, type WorkflowStepConfig } from "cloudflare:workers"
import { R2Sql } from "@opencode/data-core/r2-sql"
import { layer } from "@opencode/data-core/runtime"
import {
deterministicFailureAsResult,
planDataSync,
syncRetentionCohort,
syncStatPeriod,
type DataSyncError,
} from "@opencode/data-core/stat-sync"
import { Effect, Layer } from "effect"
import type { DataSyncParams } from "./schedule"
const runtimeLayer = Layer.mergeAll(layer, R2Sql.layer)
// Each step runs one period's R2 SQL queries (10-minute request timeout, with
// page retries) and its PlanetScale writes.
const STEP: WorkflowStepConfig = {
timeout: "30 minutes",
retries: { limit: 2, delay: "1 minute", backoff: "exponential" },
}
export class DataSyncWorkflow extends WorkflowEntrypoint<unknown, DataSyncParams> {
async run(event: WorkflowEvent<DataSyncParams | undefined>, step: WorkflowStep) {
// A dashboard trigger can start an instance without params.
const plan = await step.do("plan", STEP, () =>
run(
planDataSync(new Date(event.payload?.startedAt ?? event.timestamp), {
history: event.payload?.history ?? false,
}),
),
)
// Steps run one at a time to keep one period's rows in memory. A failed
// period does not stop the others; the run still ends as errored.
const failed: string[] = []
const fail = (name: string, error: unknown) => {
failed.push(name)
console.error(
`data sync step failed ${JSON.stringify({
step: name,
error: error instanceof Error ? error.message : String(error),
cause: error instanceof Error && error.cause !== undefined ? String(error.cause) : undefined,
})}`,
)
}
const settle = (name: string, attempt: Promise<object>) =>
attempt.then(
(result) => {
if ("failed" in result) fail(name, result.failed)
},
(error) => fail(name, error),
)
for (const period of plan.periods) {
const name = `${period.grain} ${period.key}`
await settle(
name,
step.do(name, STEP, () => run(deterministicFailureAsResult(syncStatPeriod(period, plan.catalog)))),
)
}
for (const period of plan.retentionPeriods) {
const name = `retention ${period.start.slice(0, 10)}`
await settle(
name,
step.do(name, STEP, () => run(deterministicFailureAsResult(syncRetentionCohort(period, plan.catalog)))),
)
}
if (failed.length > 0) throw new Error(`data sync failed for ${failed.join(", ")}`)
return { periods: plan.periods.length, retentionPeriods: plan.retentionPeriods.length }
}
}
function run<A>(effect: Effect.Effect<A, DataSyncError, Layer.Success<typeof runtimeLayer>>) {
return Effect.runPromise(effect.pipe(Effect.provide(runtimeLayer)))
}