@sixb/sync-worker 0.1.1 → 0.1.2
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.js +554 -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 +11 -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 +128 -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,50 @@
|
|
|
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 class SyncWorker extends QueueWorker<
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
export class SyncWorker extends QueueWorker<
|
|
29
|
+
SyncRunRequestedQueueJob,
|
|
30
|
+
typeof SYNC_RUN_FAILURE_CODES
|
|
31
|
+
> {
|
|
32
|
+
private readonly host: SyncWorkerHost
|
|
43
33
|
|
|
44
|
-
constructor(
|
|
45
|
-
if (
|
|
34
|
+
constructor(host: SyncWorkerHost) {
|
|
35
|
+
if (host.definitions.syncs.list().length === 0) {
|
|
46
36
|
throw new Error("[SixbSyncWorker] No sync definitions are registered.")
|
|
47
37
|
}
|
|
48
38
|
|
|
49
39
|
super({
|
|
50
|
-
projectId:
|
|
51
|
-
queue:
|
|
52
|
-
|
|
40
|
+
projectId: host.id,
|
|
41
|
+
queue: host.queues.syncRuns,
|
|
42
|
+
failureCodes: SYNC_RUN_FAILURE_CODES,
|
|
43
|
+
workerId: `sync-worker-${host.id}`,
|
|
53
44
|
})
|
|
54
45
|
|
|
55
|
-
|
|
56
|
-
this.
|
|
46
|
+
assertSyncStorage(host)
|
|
47
|
+
this.host = host
|
|
57
48
|
}
|
|
58
49
|
|
|
59
50
|
protected async execute(
|
|
@@ -61,30 +52,55 @@ export class SyncWorker extends QueueWorker<SyncRunRequestedQueueJob> {
|
|
|
61
52
|
signal: AbortSignal
|
|
62
53
|
): Promise<void> {
|
|
63
54
|
const { job } = claimed
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
55
|
+
const syncRuns = this.host.storage.syncRuns
|
|
56
|
+
if (!syncRuns) {
|
|
57
|
+
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
58
|
+
}
|
|
59
|
+
const run = await syncRuns.getById({ projectId: this.host.id, id: job.payload.runId })
|
|
60
|
+
if (!run) {
|
|
61
|
+
throw new Error(`[SixbSyncWorker] Sync run '${job.payload.runId}' was not found.`)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const durableExecution = await this.host.storage.executions.getById({
|
|
65
|
+
projectId: this.host.id,
|
|
66
|
+
id: run.executionId,
|
|
67
|
+
})
|
|
68
|
+
if (!durableExecution) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`[SixbSyncWorker] Sync run '${run.id}' references missing execution '${run.executionId}'.`
|
|
71
|
+
)
|
|
69
72
|
}
|
|
70
73
|
|
|
71
|
-
|
|
74
|
+
const execution = bindDurablePrimitiveExecution(this.host, {
|
|
75
|
+
execution: durableExecution,
|
|
76
|
+
primitive: { kind: "sync", id: run.syncId, runId: run.id },
|
|
77
|
+
})
|
|
78
|
+
const context = buildSyncContext(this.host, execution.sixb)
|
|
79
|
+
|
|
72
80
|
try {
|
|
73
|
-
|
|
74
|
-
runtime:
|
|
75
|
-
|
|
81
|
+
await runSyncJob({
|
|
82
|
+
runtime: context,
|
|
83
|
+
run,
|
|
76
84
|
signal,
|
|
77
|
-
onRunStarted: (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
onRunStarted: (started) =>
|
|
86
|
+
emitSyncRunStarted(this.host, started, durableExecution.correlationId),
|
|
87
|
+
onRunFinished: (finished, createdVersion) =>
|
|
88
|
+
emitSyncRunFinishedEvents(
|
|
89
|
+
this.host,
|
|
90
|
+
finished,
|
|
91
|
+
durableExecution.correlationId,
|
|
92
|
+
createdVersion
|
|
93
|
+
),
|
|
94
|
+
onRunFailed: (error, run, failure) => {
|
|
95
|
+
reportRunFailure(this.host, error, {
|
|
96
|
+
projectId: this.host.id,
|
|
82
97
|
attempt: job.attempt,
|
|
98
|
+
runKind: "sync",
|
|
83
99
|
run: {
|
|
84
|
-
kind: "sync",
|
|
85
100
|
runId: run.id,
|
|
86
101
|
syncId: run.syncId,
|
|
87
102
|
},
|
|
103
|
+
failure,
|
|
88
104
|
})
|
|
89
105
|
},
|
|
90
106
|
})
|
|
@@ -92,29 +108,31 @@ export class SyncWorker extends QueueWorker<SyncRunRequestedQueueJob> {
|
|
|
92
108
|
if (error instanceof SyncRunAlreadyStartedError) return
|
|
93
109
|
throw error
|
|
94
110
|
}
|
|
95
|
-
|
|
96
|
-
await emitSyncSucceededEvents(this.sixb, syncJob, result)
|
|
97
111
|
}
|
|
98
112
|
|
|
99
113
|
protected override async onExecutionError(
|
|
100
114
|
claimed: ClaimedQueueJob<SyncRunRequestedQueueJob>,
|
|
101
115
|
_error: unknown
|
|
102
|
-
): Promise<QueueWorkerFailureDecision
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
id: job.payload.runId
|
|
106
|
-
syncId: job.payload.syncId,
|
|
116
|
+
): Promise<QueueWorkerFailureDecision<(typeof SYNC_RUN_FAILURE_CODES)[number]>> {
|
|
117
|
+
const run = await this.host.storage.syncRuns?.getById({
|
|
118
|
+
projectId: this.host.id,
|
|
119
|
+
id: claimed.job.payload.runId,
|
|
107
120
|
})
|
|
108
|
-
|
|
109
|
-
|
|
121
|
+
return run?.status === "failed" && run.error
|
|
122
|
+
? { kind: "fail", failure: run.error }
|
|
123
|
+
: { kind: "fail" }
|
|
110
124
|
}
|
|
111
125
|
}
|
|
112
126
|
|
|
113
127
|
async function emitSyncRunStarted(
|
|
114
|
-
|
|
115
|
-
run: Pick<SyncRunRecord, "id" | "syncId" | "startedAt"
|
|
128
|
+
host: SyncWorkerHost,
|
|
129
|
+
run: Pick<SyncRunRecord, "id" | "syncId" | "startedAt">,
|
|
130
|
+
correlationId: string
|
|
116
131
|
): Promise<void> {
|
|
117
|
-
|
|
132
|
+
if (!run.startedAt) {
|
|
133
|
+
throw new Error(`[SixbSyncWorker] Sync run '${run.id}' started without a timestamp.`)
|
|
134
|
+
}
|
|
135
|
+
await host.events?.emit(
|
|
118
136
|
{
|
|
119
137
|
events: [
|
|
120
138
|
{
|
|
@@ -126,31 +144,33 @@ async function emitSyncRunStarted(
|
|
|
126
144
|
},
|
|
127
145
|
},
|
|
128
146
|
],
|
|
147
|
+
correlationId,
|
|
129
148
|
},
|
|
130
149
|
{ source: SOURCE }
|
|
131
150
|
)
|
|
132
151
|
}
|
|
133
152
|
|
|
134
|
-
async function
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
153
|
+
async function emitSyncRunFinishedEvents(
|
|
154
|
+
host: SyncWorkerHost,
|
|
155
|
+
run: Pick<SyncRunRecord, "id" | "syncId" | "datasetId" | "status" | "output" | "error">,
|
|
156
|
+
correlationId: string,
|
|
157
|
+
createdVersion?: DatasetVersion
|
|
138
158
|
): Promise<void> {
|
|
139
|
-
await
|
|
159
|
+
await host.events?.emit(
|
|
140
160
|
{
|
|
141
161
|
events: [
|
|
142
|
-
...(
|
|
162
|
+
...(createdVersion
|
|
143
163
|
? [
|
|
144
164
|
{
|
|
145
165
|
type: "dataset.version.committed" as const,
|
|
146
166
|
payload: {
|
|
147
|
-
datasetId:
|
|
148
|
-
versionId:
|
|
149
|
-
createdAt:
|
|
167
|
+
datasetId: createdVersion.datasetId,
|
|
168
|
+
versionId: createdVersion.versionId,
|
|
169
|
+
createdAt: createdVersion.createdAt.toISOString(),
|
|
150
170
|
producer: {
|
|
151
171
|
kind: "sync" as const,
|
|
152
|
-
id:
|
|
153
|
-
runId:
|
|
172
|
+
id: run.syncId,
|
|
173
|
+
runId: run.id,
|
|
154
174
|
},
|
|
155
175
|
},
|
|
156
176
|
},
|
|
@@ -159,60 +179,57 @@ async function emitSyncSucceededEvents(
|
|
|
159
179
|
{
|
|
160
180
|
type: "sync.run.finished",
|
|
161
181
|
payload: {
|
|
162
|
-
syncId:
|
|
163
|
-
runId:
|
|
164
|
-
status:
|
|
165
|
-
datasetId:
|
|
166
|
-
...(
|
|
182
|
+
syncId: run.syncId,
|
|
183
|
+
runId: run.id,
|
|
184
|
+
status: requireTerminalStatus(run.status, `Sync run '${run.id}'`),
|
|
185
|
+
datasetId: run.datasetId,
|
|
186
|
+
...(run.output ? { versionId: run.output.versionId } : {}),
|
|
187
|
+
...(run.error ? { error: run.error } : {}),
|
|
167
188
|
},
|
|
168
189
|
},
|
|
169
190
|
],
|
|
191
|
+
correlationId,
|
|
170
192
|
},
|
|
171
193
|
{ source: SOURCE }
|
|
172
194
|
)
|
|
173
195
|
}
|
|
174
196
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
):
|
|
179
|
-
|
|
180
|
-
{
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
],
|
|
191
|
-
},
|
|
192
|
-
{ source: SOURCE }
|
|
193
|
-
)
|
|
197
|
+
function requireTerminalStatus(
|
|
198
|
+
status: SyncRunStatus,
|
|
199
|
+
context: string
|
|
200
|
+
): Exclude<SyncRunStatus, "queued" | "running"> {
|
|
201
|
+
if (status === "queued" || status === "running") {
|
|
202
|
+
throw new Error(`[SixbSyncWorker] ${context} is not terminal.`)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return status
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function assertSyncStorage(host: SyncWorkerHost): void {
|
|
209
|
+
if (!host.storage.syncRuns) {
|
|
210
|
+
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
211
|
+
}
|
|
194
212
|
}
|
|
195
213
|
|
|
196
|
-
function buildSyncContext(
|
|
197
|
-
|
|
214
|
+
function buildSyncContext(
|
|
215
|
+
host: SyncWorkerHost,
|
|
216
|
+
sixb: ReturnType<typeof bindDurablePrimitiveExecution>["sixb"]
|
|
217
|
+
): SyncWorkerContext {
|
|
218
|
+
const syncRunsStorage = host.storage.syncRuns
|
|
198
219
|
if (!syncRunsStorage) {
|
|
199
220
|
throw new Error("[SixbSyncWorker] Sync workers require storage.syncRuns support.")
|
|
200
221
|
}
|
|
201
222
|
|
|
202
223
|
return {
|
|
203
|
-
id:
|
|
224
|
+
id: host.id,
|
|
204
225
|
syncRunsStorage,
|
|
205
|
-
lakeStorage:
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
return sixb.getDatasetById(datasetId)
|
|
213
|
-
},
|
|
214
|
-
connector(definition) {
|
|
215
|
-
return sixb.connector(definition)
|
|
226
|
+
lakeStorage: host.lakeStorage,
|
|
227
|
+
blobs: sixb.blobs,
|
|
228
|
+
logging: host.logging,
|
|
229
|
+
syncs: host.definitions.syncs,
|
|
230
|
+
datasets: host.definitions.datasets,
|
|
231
|
+
connectorSources: {
|
|
232
|
+
list: (definition) => resolveSyncConnectorSources(sixb.connector, definition),
|
|
216
233
|
},
|
|
217
234
|
}
|
|
218
235
|
}
|