@sixb/sync-worker 0.2.5 → 0.2.7

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.
@@ -10,11 +10,8 @@ import {
10
10
  import { isOAuthConnectorDefinition } from "@sixb/core/internal/connector-connections"
11
11
  import { createSixbError, isSixbError, summarizeErrorMessage } from "@sixb/core/internal/errors"
12
12
  import type { SyncConnectorSource } from "@sixb/core/internal/syncs"
13
- import {
14
- ConnectionCheckpoints,
15
- createSyncCheckpointCodec,
16
- type SyncCheckpointCodec,
17
- } from "./checkpoints"
13
+ import type { SyncRunRecord } from "@sixb/core/storage"
14
+ import { connectionCheckpoint, singleCheckpoint } from "./checkpoints"
18
15
  import { normalizeReadResult, runAbortable, throwIfAborted } from "./normalize"
19
16
  import type { SyncWorkerContext } from "./types"
20
17
 
@@ -23,9 +20,68 @@ export interface SyncSourceValue {
23
20
  readonly connection?: SyncConnectorConnection
24
21
  }
25
22
 
23
+ /** What one run reads: a static connector's client, or one connection of an OAuth connector. */
24
+ export type SyncRunSource =
25
+ | { readonly kind: "static"; readonly source: SyncConnectorSource }
26
+ | {
27
+ readonly kind: "connection"
28
+ readonly source: SyncConnectorSource
29
+ readonly connection: SyncConnectorConnection
30
+ }
31
+
32
+ /** The run has nothing left to read, so it ends `cancelled` without reading. */
33
+ export class SyncRunSourceGoneError extends Error {
34
+ override readonly name = "SyncRunSourceGoneError"
35
+ }
36
+
37
+ /**
38
+ * Resolve what this run reads. A sync of an OAuth connector runs once per connection, so a run
39
+ * reads the connection it was queued for, and ends without reading if that connection is no longer
40
+ * connected or its owner was suspended since.
41
+ */
42
+ export async function resolveSyncRunSource(
43
+ runtime: SyncWorkerContext,
44
+ sync: SyncDefinition,
45
+ run: Pick<SyncRunRecord, "id" | "connectionId">
46
+ ): Promise<SyncRunSource> {
47
+ const sources = await runtime.connectorSources.list(sync.connector)
48
+
49
+ if (!isOAuthConnectorDefinition(sync.connector)) {
50
+ if (run.connectionId !== undefined) {
51
+ throw sourceInvariant(sync.id, `Static connector run '${run.id}' names a connection.`)
52
+ }
53
+ const [source] = sources
54
+ if (sources.length !== 1 || !source || source.connection !== undefined) {
55
+ throw sourceInvariant(sync.id, "Static connectors must resolve exactly one source.")
56
+ }
57
+ return { kind: "static", source }
58
+ }
59
+
60
+ if (run.connectionId === undefined) {
61
+ throw new SyncRunSourceGoneError(
62
+ `[SixbSyncWorker] Sync run '${run.id}' was queued before a sync of an OAuth connector ran once per connection. The sync's next trigger runs each connection.`
63
+ )
64
+ }
65
+
66
+ const source = sources.find((candidate) => candidate.connection?.id === run.connectionId)
67
+ if (!source?.connection) {
68
+ throw new SyncRunSourceGoneError(
69
+ `[SixbSyncWorker] Connection '${run.connectionId}' of sync '${sync.id}' is no longer connected, or its owner is suspended.`
70
+ )
71
+ }
72
+ if (source.connection.connectorId !== sync.connector.id) {
73
+ throw sourceInvariant(
74
+ sync.id,
75
+ `Connection '${source.connection.id}' belongs to connector '${source.connection.connectorId}', not '${sync.connector.id}'.`
76
+ )
77
+ }
78
+ return { kind: "connection", source, connection: source.connection }
79
+ }
80
+
26
81
  interface ReadSyncValuesInput {
27
82
  readonly runtime: SyncWorkerContext
28
83
  readonly sync: SyncDefinition
84
+ readonly source: SyncRunSource
29
85
  readonly runId: string
30
86
  readonly datasetId: string
31
87
  readonly signal: AbortSignal
@@ -35,74 +91,25 @@ interface ReadSyncValuesInput {
35
91
  setCheckpoint(next: JsonValue | undefined): void
36
92
  }
37
93
 
38
- /** Resolve and invoke every source contributing to one atomic Sync run. */
94
+ /** Invoke the source of one atomic Sync run. */
39
95
  export async function readSyncValues(
40
96
  options: ReadSyncValuesInput
41
97
  ): 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)
98
+ const { source } = options
99
+ return source.kind === "static"
100
+ ? readStaticSource(options, source.source)
101
+ : readConnectionSource(options, source.source, source.connection)
95
102
  }
96
103
 
97
104
  async function readStaticSource(
98
105
  options: ReadSyncValuesInput,
99
- source: SyncConnectorSource,
100
- checkpointCodec: SyncCheckpointCodec
106
+ source: SyncConnectorSource
101
107
  ): Promise<AsyncIterable<SyncSourceValue>> {
102
- const previousCheckpoint = checkpointCodec.read(options.previousCheckpoint)
108
+ const identity = checkpointIdentity(options.sync)
109
+ const previousCheckpoint = singleCheckpoint.read(identity, options.previousCheckpoint)
103
110
  if (previousCheckpoint !== undefined) {
104
111
  // Migrate legacy raw checkpoints only after the complete run succeeds.
105
- options.setCheckpoint(checkpointCodec.serialize(previousCheckpoint))
112
+ options.setCheckpoint(singleCheckpoint.serialize(identity, previousCheckpoint))
106
113
  }
107
114
  const client = await runAbortable(options.signal, () => source.connect(options.signal))
108
115
  throwIfAborted(options.signal)
@@ -110,16 +117,12 @@ async function readStaticSource(
110
117
  options.sync.read(
111
118
  client as never,
112
119
  {
113
- projectId: options.runtime.id,
114
- syncId: options.sync.id,
115
- signal: options.signal,
116
- blobs: options.blobStorage,
117
- logger: options.logger,
120
+ ...readContext(options),
118
121
  checkpoint:
119
122
  previousCheckpoint === undefined ? undefined : cloneJsonValue(previousCheckpoint),
120
123
  setCheckpoint(next: unknown) {
121
124
  assertJsonValue(next, `Sync '${options.sync.id}' checkpoint`)
122
- options.setCheckpoint(checkpointCodec.serialize(cloneJsonValue(next)))
125
+ options.setCheckpoint(singleCheckpoint.serialize(identity, cloneJsonValue(next)))
123
126
  },
124
127
  } as never
125
128
  )
@@ -127,50 +130,34 @@ async function readStaticSource(
127
130
  return sourceValues(normalizeReadResult(readResult, options.sync.id, options.signal))
128
131
  }
129
132
 
130
- async function* readManagedSources(
133
+ async function* readConnectionSource(
131
134
  options: ReadSyncValuesInput,
132
- sources: readonly (SyncConnectorSource & {
133
- readonly connection: SyncConnectorConnection
134
- })[],
135
- checkpoints: ConnectionCheckpoints,
136
- storeCheckpoint: () => void
135
+ source: SyncConnectorSource,
136
+ connection: SyncConnectorConnection
137
137
  ): AsyncIterable<SyncSourceValue> {
138
- for (const source of sources) {
139
- yield* readManagedSource(options, source, checkpoints, storeCheckpoint)
140
- }
141
- }
138
+ const identity = checkpointIdentity(options.sync)
139
+ const previous = connectionCheckpoint.read(identity, connection, options.previousCheckpoint)
140
+ // A checkpoint carried from an older format becomes this connection's own on success.
141
+ options.setCheckpoint(connectionCheckpoint.serialize(identity, connection, previous))
142
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
143
  try {
151
144
  throwIfAborted(options.signal)
152
145
  let client: unknown
153
146
  try {
154
147
  client = await runAbortable(options.signal, () => source.connect(options.signal))
155
148
  } catch (error) {
156
- if (!skipsUnauthorizedConnection(options.sync, connection, error)) throw error
157
- // The connection keeps its checkpoint and its rows; its owner sees it needs reconnecting.
158
- console.warn(
159
- `[SixbSyncWorker] Sync '${options.sync.id}' skipped connection '${connection.id}' (${connection.account.label}): it needs reconnecting.`
160
- )
161
- return
149
+ if (isSixbError(error) && error.code === "connector.authorization_required") {
150
+ throw needsReconnecting(options, connection, error)
151
+ }
152
+ throw error
162
153
  }
163
154
  throwIfAborted(options.signal)
164
- const previous = checkpoints.get(connection)
155
+
165
156
  const readResult = await runAbortable(options.signal, () =>
166
157
  options.sync.read(
167
158
  client as never,
168
159
  {
169
- projectId: options.runtime.id,
170
- syncId: options.sync.id,
171
- signal: options.signal,
172
- blobs: options.blobStorage,
173
- logger: options.logger,
160
+ ...readContext(options),
174
161
  connection,
175
162
  checkpoint: previous === undefined ? undefined : cloneJsonValue(previous),
176
163
  setCheckpoint(next: unknown) {
@@ -178,8 +165,9 @@ async function* readManagedSource(
178
165
  next,
179
166
  `Sync '${options.sync.id}' connection '${connection.id}' checkpoint`
180
167
  )
181
- checkpoints.set(connection, cloneJsonValue(next))
182
- storeCheckpoint()
168
+ options.setCheckpoint(
169
+ connectionCheckpoint.serialize(identity, connection, cloneJsonValue(next))
170
+ )
183
171
  },
184
172
  } as never
185
173
  )
@@ -189,41 +177,63 @@ async function* readManagedSource(
189
177
  }
190
178
  } catch (error) {
191
179
  if (options.signal.aborted) throw error
180
+ if (isSixbError(error) && error.code === "connector.authorization_required") throw error
192
181
  throw createSixbError(
193
182
  "sync.execution_failed",
194
- summarizeErrorMessage(error, "Managed connector Sync source failed."),
195
- {
196
- cause: error,
197
- details: {
198
- syncId: options.sync.id,
199
- runId: options.runId,
200
- datasetId: options.datasetId,
201
- connectionId: connection.id,
202
- accountId: connection.account.id,
203
- },
204
- }
183
+ summarizeErrorMessage(error, "Connector connection Sync source failed."),
184
+ { cause: error, details: connectionDetails(options, connection) }
205
185
  )
206
186
  }
207
187
  }
208
188
 
209
189
  /**
210
- * One person's account that needs reconnecting must not fail everyone's run. A snapshot run still
211
- * fails: leaving the account out would drop its rows from the dataset. A project connection still
212
- * fails the run, so the project's own expired grant reaches `onError` as it always has.
190
+ * The connection's grant can no longer give a token. Its owner reconnects it, and the next run
191
+ * resumes from its checkpoint. A person's own connection is theirs to fix, so it is not reported.
213
192
  */
214
- function skipsUnauthorizedConnection(
215
- sync: SyncDefinition,
193
+ function needsReconnecting(
194
+ options: ReadSyncValuesInput,
216
195
  connection: SyncConnectorConnection,
217
- error: unknown
218
- ): boolean {
219
- return (
220
- sync.config.mode !== "snapshot" &&
221
- connection.owner.type === "user" &&
222
- isSixbError(error) &&
223
- error.code === "connector.authorization_required"
196
+ cause: unknown
197
+ ): Error {
198
+ if (connection.owner.type === "user") {
199
+ console.warn(
200
+ `[SixbSyncWorker] Sync '${options.sync.id}' could not read connection '${connection.id}': it needs reconnecting.`
201
+ )
202
+ }
203
+ return createSixbError(
204
+ "connector.authorization_required",
205
+ `[SixbSyncWorker] Connection '${connection.id}' of sync '${options.sync.id}' needs reconnecting.`,
206
+ {
207
+ cause,
208
+ details: { ...connectionDetails(options, connection), ownerType: connection.owner.type },
209
+ }
224
210
  )
225
211
  }
226
212
 
213
+ function readContext(options: ReadSyncValuesInput) {
214
+ return {
215
+ projectId: options.runtime.id,
216
+ syncId: options.sync.id,
217
+ signal: options.signal,
218
+ blobs: options.blobStorage,
219
+ logger: options.logger,
220
+ }
221
+ }
222
+
223
+ function checkpointIdentity(sync: SyncDefinition) {
224
+ return { syncId: sync.id, connectorId: sync.connector.id }
225
+ }
226
+
227
+ function connectionDetails(options: ReadSyncValuesInput, connection: SyncConnectorConnection) {
228
+ return {
229
+ syncId: options.sync.id,
230
+ runId: options.runId,
231
+ datasetId: options.datasetId,
232
+ connectionId: connection.id,
233
+ accountId: connection.account.id,
234
+ }
235
+ }
236
+
227
237
  function sourceInvariant(syncId: string, message: string): Error {
228
238
  return createSixbError("internal.unexpected", `[SixbSyncWorker] ${message}`, {
229
239
  details: { syncId },
package/src/worker.ts CHANGED
@@ -1,4 +1,11 @@
1
- import type { DomainEventLog, LakeStorage, Queues, SixbDefinitions, Storage } from "@sixb/core"
1
+ import type {
2
+ DomainEventLog,
3
+ LakeStorage,
4
+ Queues,
5
+ SixbDefinitions,
6
+ SixbFailure,
7
+ Storage,
8
+ } from "@sixb/core"
2
9
  import { reportRunFailure } from "@sixb/core/internal/error-reporting"
3
10
  import type { LoggingService } from "@sixb/core/internal/logging"
4
11
  import {
@@ -13,6 +20,7 @@ import {
13
20
  type RunClaim,
14
21
  type RunExecution,
15
22
  SYNC_RUN_FAILURE_CODES,
23
+ type SyncRunFailureCode,
16
24
  type SyncRunRecord,
17
25
  type SyncRunStatus,
18
26
  type SyncRunStorage,
@@ -102,6 +110,7 @@ export class SyncWorker extends RunWorker<
102
110
  await this.wakeNextRun(finished)
103
111
  },
104
112
  onRunFailed: (error, run, failure) => {
113
+ if (isPersonalReconnect(failure)) return
105
114
  reportRunFailure(this.host, error, {
106
115
  projectId: this.host.id,
107
116
  attempt: run.attempt,
@@ -149,7 +158,7 @@ export class SyncWorker extends RunWorker<
149
158
 
150
159
  async function emitSyncRunStarted(
151
160
  host: SyncWorkerHost,
152
- run: Pick<SyncRunRecord, "id" | "syncId" | "startedAt">,
161
+ run: Pick<SyncRunRecord, "id" | "syncId" | "connectionId" | "startedAt">,
153
162
  correlationId: string
154
163
  ): Promise<void> {
155
164
  if (!run.startedAt) {
@@ -163,6 +172,7 @@ async function emitSyncRunStarted(
163
172
  payload: {
164
173
  syncId: run.syncId,
165
174
  runId: run.id,
175
+ ...(run.connectionId === undefined ? {} : { connectionId: run.connectionId }),
166
176
  startedAt: run.startedAt.toISOString(),
167
177
  },
168
178
  },
@@ -175,7 +185,10 @@ async function emitSyncRunStarted(
175
185
 
176
186
  async function emitSyncRunFinishedEvents(
177
187
  host: SyncWorkerHost,
178
- run: Pick<SyncRunRecord, "id" | "syncId" | "datasetId" | "status" | "output" | "error">,
188
+ run: Pick<
189
+ SyncRunRecord,
190
+ "id" | "syncId" | "connectionId" | "datasetId" | "status" | "output" | "error"
191
+ >,
179
192
  correlationId: string,
180
193
  createdVersion?: DatasetVersion
181
194
  ): Promise<void> {
@@ -209,6 +222,7 @@ async function emitSyncRunFinishedEvents(
209
222
  payload: {
210
223
  syncId: run.syncId,
211
224
  runId: run.id,
225
+ ...(run.connectionId === undefined ? {} : { connectionId: run.connectionId }),
212
226
  status: requireTerminalStatus(run.status, `Sync run '${run.id}'`),
213
227
  datasetId: run.datasetId,
214
228
  ...(run.output ? { versionId: run.output.versionId } : {}),
@@ -222,6 +236,16 @@ async function emitSyncRunFinishedEvents(
222
236
  )
223
237
  }
224
238
 
239
+ /** A person's own connection needs reconnecting: theirs to fix, so not the project's error. */
240
+ function isPersonalReconnect(failure: SixbFailure<SyncRunFailureCode>): boolean {
241
+ const details = failure.details
242
+ return (
243
+ failure.code === "connector.authorization_required" &&
244
+ isRecord(details) &&
245
+ details.ownerType === "user"
246
+ )
247
+ }
248
+
225
249
  function requireTerminalStatus(
226
250
  status: SyncRunStatus,
227
251
  context: string
@@ -257,3 +281,7 @@ function buildSyncContext(
257
281
  },
258
282
  }
259
283
  }
284
+
285
+ function isRecord(value: unknown): value is Record<string, unknown> {
286
+ return typeof value === "object" && value !== null && !Array.isArray(value)
287
+ }