@sixb/sync-worker 0.1.1 → 0.1.3
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 +4 -6
- package/dist/checkpoints.d.ts +24 -0
- package/dist/checkpoints.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +555 -166
- package/dist/index.js.map +8 -6
- package/dist/normalize.d.ts +3 -1
- package/dist/normalize.d.ts.map +1 -1
- package/dist/run-sync-job.d.ts +4 -5
- package/dist/run-sync-job.d.ts.map +1 -1
- package/dist/source-read.d.ts +21 -0
- package/dist/source-read.d.ts.map +1 -0
- package/dist/types.d.ts +16 -16
- package/dist/types.d.ts.map +1 -1
- package/dist/worker.d.ts +15 -15
- package/dist/worker.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/checkpoints.ts +200 -0
- package/src/index.ts +1 -1
- package/src/normalize.ts +44 -2
- package/src/run-sync-job.ts +232 -115
- package/src/source-read.ts +207 -0
- package/src/types.ts +23 -29
- package/src/worker.ts +134 -111
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertJsonValue,
|
|
3
|
+
type BlobStorage,
|
|
4
|
+
cloneJsonValue,
|
|
5
|
+
type JsonValue,
|
|
6
|
+
type Logger,
|
|
7
|
+
type SyncConnectorConnection,
|
|
8
|
+
type SyncDefinition,
|
|
9
|
+
} from "@sixb/core"
|
|
10
|
+
import { isOAuthConnectorDefinition } from "@sixb/core/internal/connector-connections"
|
|
11
|
+
import { createSixbError, summarizeErrorMessage } from "@sixb/core/internal/errors"
|
|
12
|
+
import type { SyncConnectorSource } from "@sixb/core/internal/syncs"
|
|
13
|
+
import {
|
|
14
|
+
ConnectionCheckpoints,
|
|
15
|
+
createSyncCheckpointCodec,
|
|
16
|
+
type SyncCheckpointCodec,
|
|
17
|
+
} from "./checkpoints"
|
|
18
|
+
import { normalizeReadResult, runAbortable, throwIfAborted } from "./normalize"
|
|
19
|
+
import type { SyncWorkerContext } from "./types"
|
|
20
|
+
|
|
21
|
+
export interface SyncSourceValue {
|
|
22
|
+
readonly value: unknown
|
|
23
|
+
readonly connection?: SyncConnectorConnection
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ReadSyncValuesInput {
|
|
27
|
+
readonly runtime: SyncWorkerContext
|
|
28
|
+
readonly sync: SyncDefinition
|
|
29
|
+
readonly runId: string
|
|
30
|
+
readonly datasetId: string
|
|
31
|
+
readonly signal: AbortSignal
|
|
32
|
+
readonly blobStorage: BlobStorage
|
|
33
|
+
readonly logger: Logger
|
|
34
|
+
readonly previousCheckpoint: JsonValue | undefined
|
|
35
|
+
setCheckpoint(next: JsonValue | undefined): void
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Resolve and invoke every source contributing to one atomic Sync run. */
|
|
39
|
+
export async function readSyncValues(
|
|
40
|
+
options: ReadSyncValuesInput
|
|
41
|
+
): Promise<AsyncIterable<SyncSourceValue>> {
|
|
42
|
+
const sources = await options.runtime.connectorSources.list(options.sync.connector)
|
|
43
|
+
if (!isOAuthConnectorDefinition(options.sync.connector)) {
|
|
44
|
+
if (sources.length !== 1 || sources[0]?.connection !== undefined) {
|
|
45
|
+
throw sourceInvariant(options.sync.id, "Static connectors must resolve exactly one source.")
|
|
46
|
+
}
|
|
47
|
+
const checkpointCodec = createSyncCheckpointCodec({
|
|
48
|
+
syncId: options.sync.id,
|
|
49
|
+
connectorId: options.sync.connector.id,
|
|
50
|
+
strategy: "single",
|
|
51
|
+
})
|
|
52
|
+
return readStaticSource(options, sources[0], checkpointCodec)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const connectionIds = new Set<string>()
|
|
56
|
+
const managedSources = sources.map((source) => {
|
|
57
|
+
if (!source.connection) {
|
|
58
|
+
throw sourceInvariant(
|
|
59
|
+
options.sync.id,
|
|
60
|
+
"OAuth connector sources must include connection metadata."
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
const connection = source.connection
|
|
64
|
+
if (connection.connectorId !== options.sync.connector.id) {
|
|
65
|
+
throw sourceInvariant(
|
|
66
|
+
options.sync.id,
|
|
67
|
+
`Connection '${connection.id}' belongs to connector '${connection.connectorId}', not '${options.sync.connector.id}'.`
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
if (connectionIds.has(connection.id)) {
|
|
71
|
+
throw sourceInvariant(options.sync.id, `Connection '${connection.id}' was resolved twice.`)
|
|
72
|
+
}
|
|
73
|
+
connectionIds.add(connection.id)
|
|
74
|
+
return {
|
|
75
|
+
connection,
|
|
76
|
+
connect: (signal: AbortSignal) => source.connect(signal),
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
managedSources.sort((left, right) => left.connection.id.localeCompare(right.connection.id))
|
|
80
|
+
const checkpointCodec = createSyncCheckpointCodec({
|
|
81
|
+
syncId: options.sync.id,
|
|
82
|
+
connectorId: options.sync.connector.id,
|
|
83
|
+
strategy: "connections",
|
|
84
|
+
})
|
|
85
|
+
const checkpoints = ConnectionCheckpoints.from(
|
|
86
|
+
options.sync.id,
|
|
87
|
+
checkpointCodec.read(options.previousCheckpoint)
|
|
88
|
+
)
|
|
89
|
+
checkpoints.reconcile(managedSources.map((source) => source.connection))
|
|
90
|
+
const storeCheckpoint = () =>
|
|
91
|
+
options.setCheckpoint(checkpointCodec.serialize(checkpoints.serialize()))
|
|
92
|
+
storeCheckpoint()
|
|
93
|
+
|
|
94
|
+
return readManagedSources(options, managedSources, checkpoints, storeCheckpoint)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function readStaticSource(
|
|
98
|
+
options: ReadSyncValuesInput,
|
|
99
|
+
source: SyncConnectorSource,
|
|
100
|
+
checkpointCodec: SyncCheckpointCodec
|
|
101
|
+
): Promise<AsyncIterable<SyncSourceValue>> {
|
|
102
|
+
const previousCheckpoint = checkpointCodec.read(options.previousCheckpoint)
|
|
103
|
+
if (previousCheckpoint !== undefined) {
|
|
104
|
+
// Migrate legacy raw checkpoints only after the complete run succeeds.
|
|
105
|
+
options.setCheckpoint(checkpointCodec.serialize(previousCheckpoint))
|
|
106
|
+
}
|
|
107
|
+
const client = await runAbortable(options.signal, () => source.connect(options.signal))
|
|
108
|
+
throwIfAborted(options.signal)
|
|
109
|
+
const readResult = await runAbortable(options.signal, () =>
|
|
110
|
+
options.sync.read(
|
|
111
|
+
client as never,
|
|
112
|
+
{
|
|
113
|
+
projectId: options.runtime.id,
|
|
114
|
+
syncId: options.sync.id,
|
|
115
|
+
signal: options.signal,
|
|
116
|
+
blobs: options.blobStorage,
|
|
117
|
+
logger: options.logger,
|
|
118
|
+
checkpoint:
|
|
119
|
+
previousCheckpoint === undefined ? undefined : cloneJsonValue(previousCheckpoint),
|
|
120
|
+
setCheckpoint(next: unknown) {
|
|
121
|
+
assertJsonValue(next, `Sync '${options.sync.id}' checkpoint`)
|
|
122
|
+
options.setCheckpoint(checkpointCodec.serialize(cloneJsonValue(next)))
|
|
123
|
+
},
|
|
124
|
+
} as never
|
|
125
|
+
)
|
|
126
|
+
)
|
|
127
|
+
return sourceValues(normalizeReadResult(readResult, options.sync.id, options.signal))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async function* readManagedSources(
|
|
131
|
+
options: ReadSyncValuesInput,
|
|
132
|
+
sources: readonly (SyncConnectorSource & {
|
|
133
|
+
readonly connection: SyncConnectorConnection
|
|
134
|
+
})[],
|
|
135
|
+
checkpoints: ConnectionCheckpoints,
|
|
136
|
+
storeCheckpoint: () => void
|
|
137
|
+
): AsyncIterable<SyncSourceValue> {
|
|
138
|
+
for (const source of sources) {
|
|
139
|
+
yield* readManagedSource(options, source, checkpoints, storeCheckpoint)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function* readManagedSource(
|
|
144
|
+
options: ReadSyncValuesInput,
|
|
145
|
+
source: SyncConnectorSource & { readonly connection: SyncConnectorConnection },
|
|
146
|
+
checkpoints: ConnectionCheckpoints,
|
|
147
|
+
storeCheckpoint: () => void
|
|
148
|
+
): AsyncIterable<SyncSourceValue> {
|
|
149
|
+
const { connection } = source
|
|
150
|
+
try {
|
|
151
|
+
throwIfAborted(options.signal)
|
|
152
|
+
const client = await runAbortable(options.signal, () => source.connect(options.signal))
|
|
153
|
+
throwIfAborted(options.signal)
|
|
154
|
+
const previous = checkpoints.get(connection)
|
|
155
|
+
const readResult = await runAbortable(options.signal, () =>
|
|
156
|
+
options.sync.read(
|
|
157
|
+
client as never,
|
|
158
|
+
{
|
|
159
|
+
projectId: options.runtime.id,
|
|
160
|
+
syncId: options.sync.id,
|
|
161
|
+
signal: options.signal,
|
|
162
|
+
blobs: options.blobStorage,
|
|
163
|
+
logger: options.logger,
|
|
164
|
+
connection,
|
|
165
|
+
checkpoint: previous === undefined ? undefined : cloneJsonValue(previous),
|
|
166
|
+
setCheckpoint(next: unknown) {
|
|
167
|
+
assertJsonValue(
|
|
168
|
+
next,
|
|
169
|
+
`Sync '${options.sync.id}' connection '${connection.id}' checkpoint`
|
|
170
|
+
)
|
|
171
|
+
checkpoints.set(connection, cloneJsonValue(next))
|
|
172
|
+
storeCheckpoint()
|
|
173
|
+
},
|
|
174
|
+
} as never
|
|
175
|
+
)
|
|
176
|
+
)
|
|
177
|
+
for await (const value of normalizeReadResult(readResult, options.sync.id, options.signal)) {
|
|
178
|
+
yield { value, connection }
|
|
179
|
+
}
|
|
180
|
+
} catch (error) {
|
|
181
|
+
if (options.signal.aborted) throw error
|
|
182
|
+
throw createSixbError(
|
|
183
|
+
"sync.execution_failed",
|
|
184
|
+
summarizeErrorMessage(error, "Managed connector Sync source failed."),
|
|
185
|
+
{
|
|
186
|
+
cause: error,
|
|
187
|
+
details: {
|
|
188
|
+
syncId: options.sync.id,
|
|
189
|
+
runId: options.runId,
|
|
190
|
+
datasetId: options.datasetId,
|
|
191
|
+
connectionId: connection.id,
|
|
192
|
+
accountId: connection.account.id,
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function sourceInvariant(syncId: string, message: string): Error {
|
|
200
|
+
return createSixbError("internal.unexpected", `[SixbSyncWorker] ${message}`, {
|
|
201
|
+
details: { syncId },
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function* sourceValues(values: AsyncIterable<unknown>): AsyncIterable<SyncSourceValue> {
|
|
206
|
+
for await (const value of values) yield { value }
|
|
207
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,50 +1,44 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
ConnectorClient,
|
|
5
|
-
ConnectorDefinition,
|
|
6
|
-
DatasetDefinition,
|
|
7
|
-
LakeStorage,
|
|
8
|
-
SyncDefinition,
|
|
9
|
-
SyncMode,
|
|
10
|
-
} from "@sixb/core"
|
|
11
|
-
import type { LogsRuntime } from "@sixb/core/internal/logging"
|
|
1
|
+
import type { BlobStorage, LakeStorage, SixbDefinitions, SixbFailure, SyncMode } from "@sixb/core"
|
|
2
|
+
import type { LoggingService } from "@sixb/core/internal/logging"
|
|
3
|
+
import type { SyncConnectorSourceResolver } from "@sixb/core/internal/syncs"
|
|
12
4
|
import type { DatasetVersion } from "@sixb/core/lake-storage"
|
|
13
|
-
import type { SyncRunRecord, SyncRunStorage } from "@sixb/core/storage"
|
|
5
|
+
import type { SyncRunFailureCode, SyncRunRecord, SyncRunStorage } from "@sixb/core/storage"
|
|
14
6
|
|
|
15
7
|
export interface SyncWorkerContext {
|
|
16
8
|
readonly id: string
|
|
17
9
|
readonly syncRunsStorage: SyncRunStorage
|
|
18
10
|
readonly lakeStorage: LakeStorage
|
|
19
|
-
readonly
|
|
20
|
-
readonly
|
|
11
|
+
readonly blobs: BlobStorage
|
|
12
|
+
readonly logging?: LoggingService
|
|
21
13
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
connector<TAdapter extends ConnectorAdapter>(
|
|
26
|
-
definition: ConnectorDefinition<string, TAdapter>
|
|
27
|
-
): Promise<ConnectorClient<TAdapter>>
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface SyncJob {
|
|
31
|
-
readonly id: string
|
|
32
|
-
readonly syncId: string
|
|
33
|
-
readonly expectedLatestVersionId?: string
|
|
34
|
-
readonly commitMessage?: string
|
|
14
|
+
readonly datasets: Pick<SixbDefinitions["datasets"], "getById">
|
|
15
|
+
readonly syncs: Pick<SixbDefinitions["syncs"], "getById">
|
|
16
|
+
readonly connectorSources: SyncConnectorSourceResolver
|
|
35
17
|
}
|
|
36
18
|
|
|
37
19
|
export interface RunSyncJobInput {
|
|
38
20
|
readonly runtime: SyncWorkerContext
|
|
39
|
-
|
|
21
|
+
/** Durable run loaded before the execution scope is restored. */
|
|
22
|
+
readonly run: SyncRunRecord
|
|
40
23
|
readonly signal?: AbortSignal
|
|
41
24
|
readonly onRunStarted?: SyncRunStartedHandler
|
|
25
|
+
readonly onRunFinished?: SyncRunFinishedHandler
|
|
42
26
|
readonly onRunFailed?: SyncRunFailedHandler
|
|
43
27
|
}
|
|
44
28
|
|
|
45
29
|
export type SyncRunStartedHandler = (run: SyncRunRecord) => Promise<void> | void
|
|
46
30
|
|
|
47
|
-
export type
|
|
31
|
+
export type SyncRunFinishedHandler = (
|
|
32
|
+
run: SyncRunRecord,
|
|
33
|
+
/** Dataset version created by this run, when the durable output is new. */
|
|
34
|
+
createdVersion?: DatasetVersion
|
|
35
|
+
) => Promise<void> | void
|
|
36
|
+
|
|
37
|
+
export type SyncRunFailedHandler = (
|
|
38
|
+
error: unknown,
|
|
39
|
+
run: SyncRunRecord,
|
|
40
|
+
failure: SixbFailure<SyncRunFailureCode>
|
|
41
|
+
) => void
|
|
48
42
|
|
|
49
43
|
interface SyncRunResultBase {
|
|
50
44
|
readonly id: string
|
package/src/worker.ts
CHANGED
|
@@ -1,59 +1,56 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
BlobStorage,
|
|
3
|
-
ConnectorAdapter,
|
|
4
|
-
ConnectorClient,
|
|
5
|
-
ConnectorDefinition,
|
|
6
|
-
DatasetDefinition,
|
|
7
|
-
DomainEventLog,
|
|
8
|
-
LakeStorage,
|
|
9
|
-
Queues,
|
|
10
|
-
Storage,
|
|
11
|
-
SyncDefinition,
|
|
12
|
-
} from "@sixb/core"
|
|
1
|
+
import type { DomainEventLog, LakeStorage, Queues, SixbDefinitions, Storage } from "@sixb/core"
|
|
13
2
|
import { reportRunFailure } from "@sixb/core/internal/error-reporting"
|
|
14
|
-
import type {
|
|
3
|
+
import type { LoggingService } from "@sixb/core/internal/logging"
|
|
4
|
+
import {
|
|
5
|
+
bindDurablePrimitiveExecution,
|
|
6
|
+
type PrimitiveExecutionHost,
|
|
7
|
+
} from "@sixb/core/internal/primitive-execution"
|
|
8
|
+
import { resolveSyncConnectorSources } from "@sixb/core/internal/syncs"
|
|
15
9
|
import type { QueueWorkerFailureDecision } from "@sixb/core/internal/workers"
|
|
16
10
|
import { QueueWorker } from "@sixb/core/internal/workers"
|
|
11
|
+
import type { DatasetVersion } from "@sixb/core/lake-storage"
|
|
17
12
|
import type { ClaimedQueueJob, SyncRunRequestedQueueJob } from "@sixb/core/queues"
|
|
18
|
-
import type
|
|
13
|
+
import { SYNC_RUN_FAILURE_CODES, type SyncRunRecord, type SyncRunStatus } from "@sixb/core/storage"
|
|
19
14
|
import { runSyncJob, SyncRunAlreadyStartedError } from "./run-sync-job"
|
|
20
|
-
import type {
|
|
15
|
+
import type { SyncWorkerContext } from "./types"
|
|
21
16
|
|
|
22
17
|
const SOURCE = "SixbSyncWorker"
|
|
23
18
|
|
|
24
|
-
export interface
|
|
25
|
-
readonly id: string
|
|
19
|
+
export interface SyncWorkerHost extends PrimitiveExecutionHost {
|
|
26
20
|
readonly events?: DomainEventLog
|
|
27
|
-
readonly
|
|
21
|
+
readonly logging?: LoggingService
|
|
28
22
|
readonly lakeStorage: LakeStorage
|
|
29
|
-
readonly blobStorage: BlobStorage
|
|
30
23
|
readonly queues: Queues
|
|
31
24
|
readonly storage: Storage
|
|
32
|
-
|
|
33
|
-
getSyncById(syncId: string): SyncDefinition | null
|
|
34
|
-
getDatasetById(datasetId: string): DatasetDefinition | null
|
|
35
|
-
connector<TAdapter extends ConnectorAdapter>(
|
|
36
|
-
definition: ConnectorDefinition<string, TAdapter>
|
|
37
|
-
): Promise<ConnectorClient<TAdapter>>
|
|
25
|
+
readonly definitions: Pick<SixbDefinitions, "syncs" | "datasets">
|
|
38
26
|
}
|
|
39
27
|
|
|
40
|
-
export
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
export interface SyncWorkerOptions {
|
|
29
|
+
/** Maximum sync run jobs this worker claims and executes at once. Defaults to 1. */
|
|
30
|
+
readonly concurrency?: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class SyncWorker extends QueueWorker<
|
|
34
|
+
SyncRunRequestedQueueJob,
|
|
35
|
+
typeof SYNC_RUN_FAILURE_CODES
|
|
36
|
+
> {
|
|
37
|
+
private readonly host: SyncWorkerHost
|
|
43
38
|
|
|
44
|
-
constructor(
|
|
45
|
-
if (
|
|
39
|
+
constructor(host: SyncWorkerHost, options: SyncWorkerOptions = {}) {
|
|
40
|
+
if (host.definitions.syncs.list().length === 0) {
|
|
46
41
|
throw new Error("[SixbSyncWorker] No sync definitions are registered.")
|
|
47
42
|
}
|
|
48
43
|
|
|
49
44
|
super({
|
|
50
|
-
projectId:
|
|
51
|
-
queue:
|
|
52
|
-
|
|
45
|
+
projectId: host.id,
|
|
46
|
+
queue: host.queues.syncRuns,
|
|
47
|
+
failureCodes: SYNC_RUN_FAILURE_CODES,
|
|
48
|
+
workerId: `sync-worker-${host.id}`,
|
|
49
|
+
claimLimit: options.concurrency,
|
|
53
50
|
})
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
this.
|
|
52
|
+
assertSyncStorage(host)
|
|
53
|
+
this.host = host
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
protected async execute(
|
|
@@ -61,30 +58,55 @@ export class SyncWorker extends QueueWorker<SyncRunRequestedQueueJob> {
|
|
|
61
58
|
signal: AbortSignal
|
|
62
59
|
): Promise<void> {
|
|
63
60
|
const { job } = claimed
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
61
|
+
const syncRuns = this.host.storage.syncRuns
|
|
62
|
+
if (!syncRuns) {
|
|
63
|
+
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
64
|
+
}
|
|
65
|
+
const run = await syncRuns.getById({ projectId: this.host.id, id: job.payload.runId })
|
|
66
|
+
if (!run) {
|
|
67
|
+
throw new Error(`[SixbSyncWorker] Sync run '${job.payload.runId}' was not found.`)
|
|
69
68
|
}
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
const durableExecution = await this.host.storage.executions.getById({
|
|
71
|
+
projectId: this.host.id,
|
|
72
|
+
id: run.executionId,
|
|
73
|
+
})
|
|
74
|
+
if (!durableExecution) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`[SixbSyncWorker] Sync run '${run.id}' references missing execution '${run.executionId}'.`
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const execution = bindDurablePrimitiveExecution(this.host, {
|
|
81
|
+
execution: durableExecution,
|
|
82
|
+
primitive: { kind: "sync", id: run.syncId, runId: run.id },
|
|
83
|
+
})
|
|
84
|
+
const context = buildSyncContext(this.host, execution.sixb)
|
|
85
|
+
|
|
72
86
|
try {
|
|
73
|
-
|
|
74
|
-
runtime:
|
|
75
|
-
|
|
87
|
+
await runSyncJob({
|
|
88
|
+
runtime: context,
|
|
89
|
+
run,
|
|
76
90
|
signal,
|
|
77
|
-
onRunStarted: (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
91
|
+
onRunStarted: (started) =>
|
|
92
|
+
emitSyncRunStarted(this.host, started, durableExecution.correlationId),
|
|
93
|
+
onRunFinished: (finished, createdVersion) =>
|
|
94
|
+
emitSyncRunFinishedEvents(
|
|
95
|
+
this.host,
|
|
96
|
+
finished,
|
|
97
|
+
durableExecution.correlationId,
|
|
98
|
+
createdVersion
|
|
99
|
+
),
|
|
100
|
+
onRunFailed: (error, run, failure) => {
|
|
101
|
+
reportRunFailure(this.host, error, {
|
|
102
|
+
projectId: this.host.id,
|
|
82
103
|
attempt: job.attempt,
|
|
104
|
+
runKind: "sync",
|
|
83
105
|
run: {
|
|
84
|
-
kind: "sync",
|
|
85
106
|
runId: run.id,
|
|
86
107
|
syncId: run.syncId,
|
|
87
108
|
},
|
|
109
|
+
failure,
|
|
88
110
|
})
|
|
89
111
|
},
|
|
90
112
|
})
|
|
@@ -92,29 +114,31 @@ export class SyncWorker extends QueueWorker<SyncRunRequestedQueueJob> {
|
|
|
92
114
|
if (error instanceof SyncRunAlreadyStartedError) return
|
|
93
115
|
throw error
|
|
94
116
|
}
|
|
95
|
-
|
|
96
|
-
await emitSyncSucceededEvents(this.sixb, syncJob, result)
|
|
97
117
|
}
|
|
98
118
|
|
|
99
119
|
protected override async onExecutionError(
|
|
100
120
|
claimed: ClaimedQueueJob<SyncRunRequestedQueueJob>,
|
|
101
121
|
_error: unknown
|
|
102
|
-
): Promise<QueueWorkerFailureDecision
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
id: job.payload.runId
|
|
106
|
-
syncId: job.payload.syncId,
|
|
122
|
+
): Promise<QueueWorkerFailureDecision<(typeof SYNC_RUN_FAILURE_CODES)[number]>> {
|
|
123
|
+
const run = await this.host.storage.syncRuns?.getById({
|
|
124
|
+
projectId: this.host.id,
|
|
125
|
+
id: claimed.job.payload.runId,
|
|
107
126
|
})
|
|
108
|
-
|
|
109
|
-
|
|
127
|
+
return run?.status === "failed" && run.error
|
|
128
|
+
? { kind: "fail", failure: run.error }
|
|
129
|
+
: { kind: "fail" }
|
|
110
130
|
}
|
|
111
131
|
}
|
|
112
132
|
|
|
113
133
|
async function emitSyncRunStarted(
|
|
114
|
-
|
|
115
|
-
run: Pick<SyncRunRecord, "id" | "syncId" | "startedAt"
|
|
134
|
+
host: SyncWorkerHost,
|
|
135
|
+
run: Pick<SyncRunRecord, "id" | "syncId" | "startedAt">,
|
|
136
|
+
correlationId: string
|
|
116
137
|
): Promise<void> {
|
|
117
|
-
|
|
138
|
+
if (!run.startedAt) {
|
|
139
|
+
throw new Error(`[SixbSyncWorker] Sync run '${run.id}' started without a timestamp.`)
|
|
140
|
+
}
|
|
141
|
+
await host.events?.emit(
|
|
118
142
|
{
|
|
119
143
|
events: [
|
|
120
144
|
{
|
|
@@ -126,31 +150,33 @@ async function emitSyncRunStarted(
|
|
|
126
150
|
},
|
|
127
151
|
},
|
|
128
152
|
],
|
|
153
|
+
correlationId,
|
|
129
154
|
},
|
|
130
155
|
{ source: SOURCE }
|
|
131
156
|
)
|
|
132
157
|
}
|
|
133
158
|
|
|
134
|
-
async function
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
159
|
+
async function emitSyncRunFinishedEvents(
|
|
160
|
+
host: SyncWorkerHost,
|
|
161
|
+
run: Pick<SyncRunRecord, "id" | "syncId" | "datasetId" | "status" | "output" | "error">,
|
|
162
|
+
correlationId: string,
|
|
163
|
+
createdVersion?: DatasetVersion
|
|
138
164
|
): Promise<void> {
|
|
139
|
-
await
|
|
165
|
+
await host.events?.emit(
|
|
140
166
|
{
|
|
141
167
|
events: [
|
|
142
|
-
...(
|
|
168
|
+
...(createdVersion
|
|
143
169
|
? [
|
|
144
170
|
{
|
|
145
171
|
type: "dataset.version.committed" as const,
|
|
146
172
|
payload: {
|
|
147
|
-
datasetId:
|
|
148
|
-
versionId:
|
|
149
|
-
createdAt:
|
|
173
|
+
datasetId: createdVersion.datasetId,
|
|
174
|
+
versionId: createdVersion.versionId,
|
|
175
|
+
createdAt: createdVersion.createdAt.toISOString(),
|
|
150
176
|
producer: {
|
|
151
177
|
kind: "sync" as const,
|
|
152
|
-
id:
|
|
153
|
-
runId:
|
|
178
|
+
id: run.syncId,
|
|
179
|
+
runId: run.id,
|
|
154
180
|
},
|
|
155
181
|
},
|
|
156
182
|
},
|
|
@@ -159,60 +185,57 @@ async function emitSyncSucceededEvents(
|
|
|
159
185
|
{
|
|
160
186
|
type: "sync.run.finished",
|
|
161
187
|
payload: {
|
|
162
|
-
syncId:
|
|
163
|
-
runId:
|
|
164
|
-
status:
|
|
165
|
-
datasetId:
|
|
166
|
-
...(
|
|
188
|
+
syncId: run.syncId,
|
|
189
|
+
runId: run.id,
|
|
190
|
+
status: requireTerminalStatus(run.status, `Sync run '${run.id}'`),
|
|
191
|
+
datasetId: run.datasetId,
|
|
192
|
+
...(run.output ? { versionId: run.output.versionId } : {}),
|
|
193
|
+
...(run.error ? { error: run.error } : {}),
|
|
167
194
|
},
|
|
168
195
|
},
|
|
169
196
|
],
|
|
197
|
+
correlationId,
|
|
170
198
|
},
|
|
171
199
|
{ source: SOURCE }
|
|
172
200
|
)
|
|
173
201
|
}
|
|
174
202
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
):
|
|
179
|
-
|
|
180
|
-
{
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
],
|
|
191
|
-
},
|
|
192
|
-
{ source: SOURCE }
|
|
193
|
-
)
|
|
203
|
+
function requireTerminalStatus(
|
|
204
|
+
status: SyncRunStatus,
|
|
205
|
+
context: string
|
|
206
|
+
): Exclude<SyncRunStatus, "queued" | "running"> {
|
|
207
|
+
if (status === "queued" || status === "running") {
|
|
208
|
+
throw new Error(`[SixbSyncWorker] ${context} is not terminal.`)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return status
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function assertSyncStorage(host: SyncWorkerHost): void {
|
|
215
|
+
if (!host.storage.syncRuns) {
|
|
216
|
+
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
217
|
+
}
|
|
194
218
|
}
|
|
195
219
|
|
|
196
|
-
function buildSyncContext(
|
|
197
|
-
|
|
220
|
+
function buildSyncContext(
|
|
221
|
+
host: SyncWorkerHost,
|
|
222
|
+
sixb: ReturnType<typeof bindDurablePrimitiveExecution>["sixb"]
|
|
223
|
+
): SyncWorkerContext {
|
|
224
|
+
const syncRunsStorage = host.storage.syncRuns
|
|
198
225
|
if (!syncRunsStorage) {
|
|
199
226
|
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
200
227
|
}
|
|
201
228
|
|
|
202
229
|
return {
|
|
203
|
-
id:
|
|
230
|
+
id: host.id,
|
|
204
231
|
syncRunsStorage,
|
|
205
|
-
lakeStorage:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return sixb.getDatasetById(datasetId)
|
|
213
|
-
},
|
|
214
|
-
connector(definition) {
|
|
215
|
-
return sixb.connector(definition)
|
|
232
|
+
lakeStorage: host.lakeStorage,
|
|
233
|
+
blobs: sixb.blobs,
|
|
234
|
+
logging: host.logging,
|
|
235
|
+
syncs: host.definitions.syncs,
|
|
236
|
+
datasets: host.definitions.datasets,
|
|
237
|
+
connectorSources: {
|
|
238
|
+
list: (definition) => resolveSyncConnectorSources(sixb.connector, definition),
|
|
216
239
|
},
|
|
217
240
|
}
|
|
218
241
|
}
|