@open-mercato/core 0.8.1-develop.7241.1.fad7b2ce2a → 0.8.1-develop.7254.1.ab24f26208
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/.turbo/turbo-build.log +1 -1
- package/dist/modules/attachments/lib/storage.js +15 -1
- package/dist/modules/attachments/lib/storage.js.map +2 -2
- package/dist/modules/auth/api/reset/confirm.js +3 -2
- package/dist/modules/auth/api/reset/confirm.js.map +2 -2
- package/dist/modules/auth/api/reset.js +2 -1
- package/dist/modules/auth/api/reset.js.map +2 -2
- package/dist/modules/auth/di.js +6 -6
- package/dist/modules/auth/di.js.map +2 -2
- package/dist/modules/auth/lib/tenantHint.js +29 -0
- package/dist/modules/auth/lib/tenantHint.js.map +7 -0
- package/dist/modules/customer_accounts/di.js +3 -3
- package/dist/modules/customer_accounts/di.js.map +2 -2
- package/dist/modules/customer_accounts/lib/customerAuth.js +18 -7
- package/dist/modules/customer_accounts/lib/customerAuth.js.map +2 -2
- package/dist/modules/customer_accounts/services/customerSessionService.js +23 -0
- package/dist/modules/customer_accounts/services/customerSessionService.js.map +2 -2
- package/dist/modules/customers/ai-tools/activities-tasks-pack.js +28 -24
- package/dist/modules/customers/ai-tools/activities-tasks-pack.js.map +2 -2
- package/dist/modules/customers/commands/companies.js +5 -0
- package/dist/modules/customers/commands/companies.js.map +2 -2
- package/dist/modules/data_sync/lib/sync-engine.js +3 -7
- package/dist/modules/data_sync/lib/sync-engine.js.map +2 -2
- package/dist/modules/data_sync/lib/sync-schedule-service.js +52 -3
- package/dist/modules/data_sync/lib/sync-schedule-service.js.map +2 -2
- package/dist/modules/progress/lib/progressServiceImpl.js +1 -1
- package/dist/modules/progress/lib/progressServiceImpl.js.map +2 -2
- package/package.json +7 -7
- package/src/modules/attachments/lib/storage.ts +24 -1
- package/src/modules/auth/api/reset/confirm.ts +3 -2
- package/src/modules/auth/api/reset.ts +2 -1
- package/src/modules/auth/di.ts +6 -6
- package/src/modules/auth/lib/tenantHint.ts +49 -0
- package/src/modules/customer_accounts/di.ts +3 -3
- package/src/modules/customer_accounts/lib/customerAuth.ts +36 -7
- package/src/modules/customer_accounts/services/customerSessionService.ts +28 -0
- package/src/modules/customers/ai-tools/activities-tasks-pack.ts +49 -39
- package/src/modules/customers/commands/companies.ts +8 -0
- package/src/modules/data_sync/AGENTS.md +1 -1
- package/src/modules/data_sync/lib/sync-engine.ts +23 -12
- package/src/modules/data_sync/lib/sync-schedule-service.ts +54 -3
- package/src/modules/progress/lib/progressServiceImpl.ts +7 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/data_sync/lib/sync-engine.ts"],
|
|
4
|
-
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport type { CredentialsService } from '../../integrations/lib/credentials-service'\nimport type { IntegrationLogService } from '../../integrations/lib/log-service'\nimport type { IntegrationStateService } from '../../integrations/lib/state-service'\nimport type { ProgressService } from '../../progress/lib/progressService'\nimport { STALE_JOB_TIMEOUT_SECONDS } from '../../progress/lib/progressService'\nimport { refreshCoverageSnapshot } from '../../query_index/lib/coverage'\nimport { emitDataSyncEvent } from '../events'\nimport type { DataSyncAdapter, DataMapping, ExportBatch, ImportBatch, RunParameterValue } from './adapter'\nimport { getDataSyncAdapter, resolveProviderKey } from './adapter-registry'\nimport type { SyncRunService } from './sync-run-service'\nimport { SyncRunOwnershipConflictError } from './sync-run-service'\nimport { forEachBatch } from './batch-stream'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport {\n captureTelemetryTrace,\n getTelemetryRuntime,\n type TelemetrySpanAttributes,\n} from '@open-mercato/shared/lib/telemetry/runtime'\nimport { groupableCode } from '@open-mercato/shared/lib/telemetry/error-code'\nimport type { SyncRun } from '../data/entities'\n\nconst logger = createLogger('data_sync').child({ component: 'sync-engine' })\n\n/**\n * A run that finished with failed items. Raised so a partial success is one\n * reported error with a count, at the granularity an operator acts on \u2014 a\n * different fact from any single item's failure, which the per-item error rows\n * report on their own.\n */\nexport class SyncRunPartialFailureError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'SyncRunPartialFailureError'\n }\n}\n\n/**\n * The fingerprint for a run that ended in a fault.\n *\n * One code for now. PR #5450's `classifySyncError` splits faults into transient\n * and terminal; when it lands, this is the single place that becomes\n * `data_sync.run_transient` / `data_sync.run_terminal`.\n */\nconst RUN_FAILED_CODE = 'data_sync.run_failed'\n\n/** Run identity for a reported error, mirroring `runSpanAttributes` minus the provider key. */\nfunction runEventAttributes(run: SyncRun, scope: SyncScope): TelemetrySpanAttributes {\n return {\n 'data_sync.run_id': run.id,\n 'data_sync.integration_id': run.integrationId,\n 'data_sync.entity_type': run.entityType,\n 'data_sync.direction': run.direction,\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n }\n}\n\n/**\n * The failure fingerprint for a dead-lettered item.\n *\n * An adapter that classifies its own failures sets `errorCode` on the item's data\n * (a stable `module.reason` token, never an interpolated string); anything that is\n * not that shape falls back rather than being trusted, and the fallback is a real\n * code rather than `unknown`, so grouping works even for an adapter that supplies\n * nothing.\n */\nfunction itemErrorCode(data: Record<string, unknown>, fallback: string): string {\n return groupableCode(data.errorCode, fallback)\n}\n\n/**\n * Report a `data_sync` failure that is otherwise only recorded (a dropped\n * promise, a run's own summary). Wrapped: observability may never decide the fate\n * of a batch that is already committed.\n */\nfunction reportSyncError(\n error: unknown,\n code: string,\n attributes: TelemetrySpanAttributes,\n): void {\n try {\n getTelemetryRuntime()?.reportError(error, { module: 'data_sync', code, attributes })\n } catch (telemetryError) {\n logger.warn('Failed to report a data sync error to telemetry', { code, err: telemetryError as Error })\n }\n}\n\ntype RunParameters = Record<string, RunParameterValue>\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n userId?: string | null\n}\n\ntype EngineDeps = {\n em: EntityManager\n syncRunService: SyncRunService\n integrationCredentialsService: CredentialsService\n integrationLogService: IntegrationLogService\n integrationStateService?: IntegrationStateService\n progressService: ProgressService\n}\n\n/** Repeated on every batch span so a rooted batch trace identifies its run on its own. */\nfunction runSpanAttributes(run: SyncRun, providerKey: string, scope: SyncScope): TelemetrySpanAttributes {\n return {\n 'data_sync.run_id': run.id,\n 'data_sync.integration_id': run.integrationId,\n 'data_sync.provider_key': providerKey,\n 'data_sync.entity_type': run.entityType,\n 'data_sync.direction': run.direction,\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n }\n}\n\nfunction applyImportCounters(batch: ImportBatch): Pick<Required<SyncCounterDelta>, 'createdCount' | 'updatedCount' | 'skippedCount' | 'failedCount'> {\n let createdCount = 0\n let updatedCount = 0\n let skippedCount = 0\n let failedCount = 0\n\n for (const item of batch.items) {\n if (item.action === 'create') createdCount += 1\n else if (item.action === 'update') updatedCount += 1\n else if (item.action === 'failed') failedCount += 1\n else skippedCount += 1\n }\n\n return { createdCount, updatedCount, skippedCount, failedCount }\n}\n\ntype SyncCounterDelta = {\n createdCount?: number\n updatedCount?: number\n skippedCount?: number\n failedCount?: number\n processedCount: number\n}\n\nfunction applyExportCounters(batch: ExportBatch): SyncCounterDelta {\n let failedCount = 0\n let skippedCount = 0\n let updatedCount = 0\n\n for (const result of batch.results) {\n if (result.status === 'error') failedCount += 1\n else if (result.status === 'skipped') skippedCount += 1\n else updatedCount += 1\n }\n\n return {\n failedCount,\n skippedCount,\n updatedCount,\n processedCount: batch.results.length,\n }\n}\n\n// Adapter batches can legitimately outlast the stale-job sweep window (slow upstream\n// APIs), so the engine must heartbeat while a batch is still being produced. The same\n// tick also polls cancellation, so a cancel lands within one interval instead of waiting\n// out the batch \u2014 sharing this timer rather than adding a second one that would double\n// the per-interval round-trips for the whole life of a run.\nconst HEARTBEAT_TICK_MS = (STALE_JOB_TIMEOUT_SECONDS * 1000) / 4\n\n// Runs `tick` on an interval only while the source iterator is pending, so heartbeats\n// stop the moment the producer dies and genuinely stale jobs still get swept. The outer\n// finally closes the adapter generator on early exits (cancellation, ownership conflict).\n// Our own abort, as opposed to a failure that merely coincided with one. Adapters are told to\n// return rather than throw, but `signal.throwIfAborted()` and an aborted `fetch` both surface as\n// this, and either is a cancellation rather than a fault.\n//\n// Matched structurally on `name` rather than with `instanceof Error`, because those two throw a\n// `DOMException`, and whether that inherits from `Error` depends on the runtime \u2014 it does under\n// bare Node 24 and does NOT under the jest environment this is tested in. An `instanceof` test\n// therefore passes or fails on where the code runs, which is not something cancellation should\n// depend on.\nfunction isAbortError(error: unknown): boolean {\n return typeof error === 'object' && error !== null && (error as { name?: unknown }).name === 'AbortError'\n}\n\nasync function* withHeartbeat<T>(source: AsyncIterable<T>, tick: () => void, intervalMs: number): AsyncGenerator<T, void, undefined> {\n const iterator = source[Symbol.asyncIterator]()\n try {\n while (true) {\n const timer = setInterval(tick, intervalMs)\n let result: IteratorResult<T>\n try {\n result = await iterator.next()\n } finally {\n clearInterval(timer)\n }\n if (result.done) return\n yield result.value\n }\n } finally {\n await iterator.return?.()\n }\n}\n\nexport function createSyncEngine(deps: EngineDeps) {\n const { syncRunService, integrationCredentialsService, integrationLogService, integrationStateService, progressService } = deps\n\n async function resolveMapping(adapter: DataSyncAdapter, entityType: string, scope: SyncScope): Promise<DataMapping> {\n return adapter.getMapping({\n entityType,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n })\n }\n\n async function updateProgress(progressJobId: string | null | undefined, processedCount: number, totalCount: number | null, scope: SyncScope): Promise<void> {\n if (!progressJobId) return\n\n await progressService.updateProgress(\n progressJobId,\n {\n processedCount,\n totalCount: totalCount ?? undefined,\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n }\n\n // On redelivery the progress counter must resume where the last delivery left off the\n // same way committedBatches does \u2014 updateProgress writes absolute counts, so starting at\n // zero would regress the visible count. The progress job's own processedCount is the only\n // persisted value already in the engine's unit: `batch.processedCount ?? items.length`,\n // i.e. source records. The run's created/updated/skipped/failed counters count emitted\n // items, which adapters may explode several-per-source-record (Akeneo yields a product\n // plus its variants), so seeding from them would overshoot the total and pin the bar.\n async function seedProcessedCount(progressJobId: string | null | undefined, scope: SyncScope): Promise<number> {\n if (!progressJobId) return 0\n const job = await progressService.getJob(progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n return job?.processedCount ?? 0\n }\n\n function makeHeartbeatTick(progressJobId: string | null | undefined, scope: SyncScope): () => void {\n const touchJobHeartbeat = progressService.touchJobHeartbeat?.bind(progressService)\n if (!progressJobId || !touchJobHeartbeat) return () => {}\n let inFlight = false\n return () => {\n if (inFlight) return\n inFlight = true\n touchJobHeartbeat(progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n .catch((error) => {\n logger.warn('Progress heartbeat failed', {\n progressJobId,\n error: error instanceof Error ? error.message : String(error),\n })\n })\n .finally(() => {\n inFlight = false\n })\n }\n }\n\n // Rides the heartbeat timer, which is the only thing that runs while the adapter is still\n // producing a batch \u2014 the engine's own cancellation check sits in the batch handler and is\n // reached only after a yield. Swallows its own errors because it runs on a timer, where an\n // unhandled rejection is fatal, and stops polling once it has aborted.\n function makeCancellationTick(progressJobId: string | null | undefined, scope: SyncScope, controller: AbortController): () => void {\n if (!progressJobId) return () => {}\n let inFlight = false\n return () => {\n if (inFlight || controller.signal.aborted) return\n inFlight = true\n progressService.isCancellationRequested(progressJobId, scope.tenantId, scope.organizationId)\n .then((cancelled) => {\n if (cancelled) controller.abort()\n })\n .catch((error) => {\n logger.warn('Cancellation poll failed', {\n progressJobId,\n error: error instanceof Error ? error.message : String(error),\n })\n })\n .finally(() => {\n inFlight = false\n })\n }\n }\n\n async function refreshCoverageSnapshots(entityTypes: string[] | undefined, scope: SyncScope): Promise<void> {\n if (!entityTypes || entityTypes.length === 0) return\n\n const types = Array.from(\n new Set(entityTypes.filter((value) => typeof value === 'string' && value.trim().length > 0)),\n )\n const outcomes = await Promise.allSettled(\n types.map((entityType) => refreshCoverageSnapshot(deps.em, {\n entityType,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })),\n )\n // `allSettled` keeps a failed refresh from failing a committed batch, which is\n // right \u2014 but on its own it also discards the reason entirely, leaving no row,\n // no log and no signal. Reporting is the whole difference between degrading and\n // going silent.\n outcomes.forEach((outcome, index) => {\n if (outcome.status !== 'rejected') return\n logger.warn('Coverage snapshot refresh failed', { entityType: types[index], err: outcome.reason as Error })\n reportSyncError(outcome.reason, 'data_sync.coverage_refresh_failed', {\n entityType: types[index],\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n })\n })\n }\n\n async function logImportItemFailures(\n runId: string,\n integrationId: string,\n items: ImportBatch['items'],\n scope: SyncScope,\n ): Promise<void> {\n const failedItems = items.filter((item) => item.action === 'failed')\n for (const item of failedItems) {\n const errorMessage = typeof item.data.errorMessage === 'string' && item.data.errorMessage.trim().length > 0\n ? item.data.errorMessage.trim()\n : 'Import item failed'\n const sourceProductUuid = typeof item.data.sourceProductUuid === 'string' && item.data.sourceProductUuid.trim().length > 0\n ? item.data.sourceProductUuid.trim()\n : null\n const sourceIdentifier = typeof item.data.sourceIdentifier === 'string' && item.data.sourceIdentifier.trim().length > 0\n ? item.data.sourceIdentifier.trim()\n : null\n const message = [\n `Failed to import item ${item.externalId}`,\n sourceProductUuid ? `(uuid: ${sourceProductUuid})` : null,\n sourceIdentifier ? `(identifier: ${sourceIdentifier})` : null,\n `: ${errorMessage}`,\n ].filter((part) => part !== null).join(' ')\n\n await integrationLogService.write(\n {\n integrationId,\n runId,\n level: 'error',\n message,\n code: itemErrorCode(item.data, 'data_sync.item_failed'),\n payload: item.data,\n },\n scope,\n )\n }\n }\n\n async function logExportItemFailures(\n runId: string,\n integrationId: string,\n results: ExportBatch['results'],\n scope: SyncScope,\n ): Promise<void> {\n const failedResults = results.filter((result) => result.status === 'error' && result.error)\n for (const result of failedResults) {\n const label = result.externalId ? `${result.externalId} (id: ${result.localId})` : result.localId\n const errorMessage = result.error!.split('\\n')[0]\n const message = `Failed to export item ${label}: ${errorMessage}`\n\n await integrationLogService.write(\n {\n integrationId,\n runId,\n level: 'error',\n message,\n code: 'data_sync.export_item_failed',\n payload: { kind: 'export-item-failure', summary: result.error },\n },\n scope,\n )\n }\n }\n\n /**\n * The adapter-gated operational log. Deliberately carries no `code`: these rows\n * are run status records, and every fault they narrate was already written \u2014 and\n * therefore already reported \u2014 by a direct `level: 'error'` write that owns the\n * fingerprint. A code here would double-report every fault for adapters that\n * have `operationalTelemetry` on.\n */\n async function writeOperationalLog(params: {\n integrationId: string\n runId: string\n level: 'info' | 'warn' | 'error'\n message: string\n scope: SyncScope\n enabled: boolean\n payload?: Record<string, unknown>\n }): Promise<void> {\n if (!params.enabled) return\n\n await integrationLogService.write(\n {\n integrationId: params.integrationId,\n runId: params.runId,\n level: params.level,\n message: params.message,\n payload: params.payload,\n },\n params.scope,\n )\n }\n\n async function updateOperationalState(params: {\n integrationId: string\n status: 'healthy' | 'degraded' | 'unhealthy'\n scope: SyncScope\n enabled: boolean\n }): Promise<void> {\n if (!params.enabled || !integrationStateService) return\n\n await integrationStateService.upsert(\n params.integrationId,\n {\n lastHealthStatus: params.status,\n lastHealthCheckedAt: new Date(),\n },\n params.scope,\n )\n }\n\n async function finalizeRun(\n runId: string,\n status: 'completed' | 'failed' | 'cancelled',\n scope: SyncScope,\n error?: string,\n operationalTelemetry = false,\n ): Promise<void> {\n const existingRun = await syncRunService.getRun(runId, scope)\n const alreadyFinalizedWithSameStatus = existingRun?.status === status\n && (status === 'completed' || status === 'failed' || status === 'cancelled')\n\n const run = await syncRunService.markStatus(runId, status, scope, error)\n if (!run) return\n\n if (alreadyFinalizedWithSameStatus) {\n return\n }\n\n if (run.status !== status) {\n // `markStatus` refuses a terminal -> different-terminal transition and\n // returns the row unchanged, so the run is already finished under another\n // delivery of this job. Everything below \u2014 the progress job, the\n // operational log and the lifecycle event \u2014 would describe the wrong\n // outcome, and `data_sync.run.failed` is dispatched to tenant webhooks.\n // A displaced worker stays silent instead.\n logger.warn('Skipping finalization of a sync run another worker already finalized', {\n runId,\n requestedStatus: status,\n actualStatus: run.status,\n })\n return\n }\n\n if (run.progressJobId) {\n if (status === 'completed') {\n await progressService.completeJob(\n run.progressJobId,\n {\n resultSummary: {\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n batchesCompleted: run.batchesCompleted,\n },\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n } else if (status === 'failed') {\n await progressService.failJob(\n run.progressJobId,\n {\n errorMessage: error ?? 'Sync run failed',\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n } else if (status === 'cancelled') {\n await progressService.markCancelled(\n run.progressJobId,\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n }\n }\n\n if (status === 'completed') {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'healthy',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run completed',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'completed',\n summary: `Sync completed with ${run.createdCount} created, ${run.updatedCount} updated, ${run.failedCount} failed.`,\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n batchesCompleted: run.batchesCompleted,\n },\n })\n } else if (status === 'cancelled') {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'warn',\n message: 'Sync run cancelled',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'cancelled',\n summary: 'The sync run was cancelled before completion.',\n },\n })\n } else {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'unhealthy',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message: error ?? 'Sync run failed',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'failed',\n summary: error ?? 'The sync run failed.',\n },\n })\n }\n\n if (status === 'completed') {\n // A run that finished with failures is a partial success, and the operator\n // finds out here or not at all: the per-item rows carry the reasons but no\n // count, and this is the only place that knows the run is over. Reported\n // outside `writeOperationalLog` on purpose \u2014 that path is gated on an adapter\n // opt-in, and an adapter flag may decide how chatty the operational log is,\n // never whether a failure is observable.\n if (run.failedCount > 0) {\n reportSyncError(\n new SyncRunPartialFailureError(`Sync run completed with ${run.failedCount} failed item(s)`),\n 'data_sync.run_partial_failure',\n {\n ...runEventAttributes(run, scope),\n 'data_sync.failed_count': run.failedCount,\n 'data_sync.created_count': run.createdCount,\n 'data_sync.updated_count': run.updatedCount,\n 'data_sync.skipped_count': run.skippedCount,\n },\n )\n }\n await emitDataSyncEvent('data_sync.run.completed', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n })\n return\n }\n\n if (status === 'cancelled') {\n await emitDataSyncEvent('data_sync.run.cancelled', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n return\n }\n\n await emitDataSyncEvent('data_sync.run.failed', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n error: error ?? null,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n }\n\n return {\n async runImport(runId: string, batchSize: number, scope: SyncScope): Promise<void> {\n const run = await syncRunService.getRun(runId, scope)\n if (!run) {\n logger.warn('Skipping stale import job for missing run', { runId })\n return\n }\n if (run.status === 'cancelled') {\n if (run.progressJobId) {\n await progressService.markCancelled(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n return\n }\n\n const providerKey = resolveProviderKey(run.integrationId)\n const adapter = getDataSyncAdapter(providerKey)\n if (!adapter?.streamImport) {\n throw new Error(`No import adapter registered for provider ${providerKey}`)\n }\n const operationalTelemetry = adapter.operationalTelemetry === true\n const persistSharedCursor = adapter.persistsSharedCursor?.(run.entityType) ?? true\n\n const credentials = await integrationCredentialsService.resolve(run.integrationId, scope)\n if (!credentials) {\n throw new Error(`Integration ${run.integrationId} is missing credentials`)\n }\n\n // A run already `running` means a stalled job was redelivered, so this is\n // a resume rather than a first start. Consumers see one `started` event per\n // delivery either way; the flag is what lets them tell the two apart.\n const resumed = run.status === 'running'\n const activeRun = await syncRunService.markStatus(run.id, 'running', scope)\n if (!activeRun || activeRun.status !== 'running') {\n return\n }\n await emitDataSyncEvent('data_sync.run.started', {\n runId: run.id,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n resumed,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run started',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Import run started for ${run.entityType}.`,\n entityType: run.entityType,\n direction: run.direction,\n },\n })\n\n if (run.progressJobId) {\n await progressService.startJob(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n\n const mapping = await resolveMapping(adapter, run.entityType, scope)\n let processedCount = await seedProcessedCount(run.progressJobId, scope)\n let totalCount: number | null = null\n let committedBatches = activeRun.batchesCompleted ?? 0\n // Whether the last committed batch said the source was exhausted. Distinguishes a stream that\n // drained from one the adapter stopped early \u2014 see the post-stream finalize below.\n let streamReportedDone = false\n // Captured while the triggering job's span is still the active one, so\n // every rooted batch trace can link back to it.\n const runTrace = captureTelemetryTrace()\n const spanAttributes = runSpanAttributes(run, providerKey, scope)\n // Declared outside the try because both the catch and the completion path below read it.\n const cancellation = new AbortController()\n const heartbeat = makeHeartbeatTick(run.progressJobId, scope)\n const pollCancellation = makeCancellationTick(run.progressJobId, scope, cancellation)\n\n try {\n const streamResult = await forEachBatch(\n withHeartbeat(\n adapter.streamImport({\n entityType: run.entityType,\n cursor: run.cursor ?? undefined,\n batchSize,\n credentials,\n mapping,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n runId: run.id,\n parameters: (run.parameters ?? {}) as RunParameters,\n signal: cancellation.signal,\n }),\n // `finally` so a synchronous throw from the heartbeat cannot also stop cancellation\n // from being observed for the rest of the run.\n () => { try { heartbeat() } finally { pollCancellation() } },\n HEARTBEAT_TICK_MS,\n ),\n {\n spanName: 'data_sync.import.batch',\n drainSpanName: 'data_sync.import.drain',\n attributes: spanAttributes,\n linkTo: runTrace,\n },\n async (batch, span) => {\n span.setAttributes({\n 'data_sync.batch_index': batch.batchIndex,\n 'data_sync.batch_size': batch.items.length,\n })\n\n if (cancellation.signal.aborted || (run.progressJobId && await progressService.isCancellationRequested(run.progressJobId, scope.tenantId, scope.organizationId))) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return 'stop'\n }\n\n const delta = applyImportCounters(batch)\n const processedBatchCount = batch.processedCount ?? batch.items.length\n processedCount += processedBatchCount\n totalCount = batch.totalEstimate ?? totalCount\n\n span.setAttributes({\n 'data_sync.processed_count': processedBatchCount,\n 'data_sync.created_count': delta.createdCount,\n 'data_sync.updated_count': delta.updatedCount,\n 'data_sync.skipped_count': delta.skippedCount,\n 'data_sync.failed_count': delta.failedCount,\n })\n\n await syncRunService.commitBatchProgress(\n run.id,\n {\n ...delta,\n batchesCompleted: 1,\n },\n batch.cursor,\n scope,\n { expectedBatchesCompleted: committedBatches, persistSharedCursor },\n )\n committedBatches += 1\n streamReportedDone = batch.hasMore === false\n\n await updateProgress(run.progressJobId, processedCount, totalCount, scope)\n await refreshCoverageSnapshots(batch.refreshCoverageEntityTypes, scope)\n await logImportItemFailures(run.id, run.integrationId, batch.items, scope)\n\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: batch.message?.trim().length\n ? batch.message.trim()\n : `Processed import batch ${batch.batchIndex}`,\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Processed ${processedCount}${totalCount ? ` of ${totalCount}` : ''} rows so far.`,\n processedCount,\n batchSize: batch.items.length,\n processedBatchCount,\n cursor: batch.cursor,\n },\n })\n\n return 'continue'\n },\n )\n if (streamResult === 'stopped') return\n } catch (error) {\n if (error instanceof SyncRunOwnershipConflictError) {\n logger.warn('Yielding import run to a concurrent worker that already advanced it', {\n runId: run.id,\n expectedBatchesCompleted: error.expectedBatchesCompleted,\n })\n return\n }\n // An adapter that honours the signal may reject instead of returning, so our own abort is\n // a cancellation rather than a fault \u2014 and must not leave an `error` entry in the\n // integration log for a run the operator cancelled on purpose. Anything else that merely\n // coincided with the cancel \u2014 a rejecting commit, an upstream 500 \u2014 is a genuine failure\n // and keeps its log entry, its message, its `failed` status and its failed event.\n if (cancellation.signal.aborted && isAbortError(error)) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n const message = error instanceof Error ? error.message : 'Sync import failed'\n await integrationLogService.write(\n {\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message,\n code: RUN_FAILED_CODE,\n },\n scope,\n )\n await finalizeRun(run.id, 'failed', scope, message, operationalTelemetry)\n return\n }\n\n // An adapter that honours the signal stops mid-batch and returns WITHOUT yielding, so the\n // batch handler \u2014 which owns the only other `cancelled` transition \u2014 never runs and the\n // stream reports `completed`.\n //\n // `streamReportedDone` keeps that from swallowing a run that genuinely finished: an adapter\n // that ignores the signal and drains after reporting `hasMore: false` delivered everything it\n // had, even when the cancel landed during the final read. Calling that cancelled would tell\n // the operator a complete sync was partial and leave a finished run resumable.\n if (cancellation.signal.aborted && !streamReportedDone) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n\n await finalizeRun(run.id, 'completed', scope, undefined, operationalTelemetry)\n },\n\n async runExport(runId: string, batchSize: number, scope: SyncScope): Promise<void> {\n const run = await syncRunService.getRun(runId, scope)\n if (!run) {\n logger.warn('Skipping stale export job for missing run', { runId })\n return\n }\n if (run.status === 'cancelled') {\n if (run.progressJobId) {\n await progressService.markCancelled(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n return\n }\n\n const providerKey = resolveProviderKey(run.integrationId)\n const adapter = getDataSyncAdapter(providerKey)\n if (!adapter?.streamExport) {\n throw new Error(`No export adapter registered for provider ${providerKey}`)\n }\n const operationalTelemetry = adapter.operationalTelemetry === true\n const persistSharedCursor = adapter.persistsSharedCursor?.(run.entityType) ?? true\n\n const credentials = await integrationCredentialsService.resolve(run.integrationId, scope)\n if (!credentials) {\n throw new Error(`Integration ${run.integrationId} is missing credentials`)\n }\n\n // A run already `running` means a stalled job was redelivered, so this is\n // a resume rather than a first start. Consumers see one `started` event per\n // delivery either way; the flag is what lets them tell the two apart.\n const resumed = run.status === 'running'\n const activeRun = await syncRunService.markStatus(run.id, 'running', scope)\n if (!activeRun || activeRun.status !== 'running') {\n return\n }\n await emitDataSyncEvent('data_sync.run.started', {\n runId: run.id,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n resumed,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run started',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Export run started for ${run.entityType}.`,\n entityType: run.entityType,\n direction: run.direction,\n },\n })\n\n if (run.progressJobId) {\n await progressService.startJob(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n\n const mapping = await resolveMapping(adapter, run.entityType, scope)\n let processedCount = await seedProcessedCount(run.progressJobId, scope)\n let committedBatches = activeRun.batchesCompleted ?? 0\n // Whether the last committed batch said the source was exhausted. Distinguishes a stream that\n // drained from one the adapter stopped early \u2014 see the post-stream finalize below.\n let streamReportedDone = false\n // Captured while the triggering job's span is still the active one, so\n // every rooted batch trace can link back to it.\n const runTrace = captureTelemetryTrace()\n const spanAttributes = runSpanAttributes(run, providerKey, scope)\n // Declared outside the try because both the catch and the completion path below read it.\n const cancellation = new AbortController()\n const heartbeat = makeHeartbeatTick(run.progressJobId, scope)\n const pollCancellation = makeCancellationTick(run.progressJobId, scope, cancellation)\n\n try {\n const streamResult = await forEachBatch(\n withHeartbeat(\n adapter.streamExport({\n entityType: run.entityType,\n cursor: run.cursor ?? undefined,\n batchSize,\n credentials,\n mapping,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n runId: run.id,\n parameters: (run.parameters ?? {}) as RunParameters,\n signal: cancellation.signal,\n }),\n // `finally` so a synchronous throw from the heartbeat cannot also stop cancellation\n // from being observed for the rest of the run.\n () => { try { heartbeat() } finally { pollCancellation() } },\n HEARTBEAT_TICK_MS,\n ),\n {\n spanName: 'data_sync.export.batch',\n drainSpanName: 'data_sync.export.drain',\n attributes: spanAttributes,\n linkTo: runTrace,\n },\n async (batch, span) => {\n span.setAttributes({\n 'data_sync.batch_index': batch.batchIndex,\n 'data_sync.batch_size': batch.results.length,\n })\n\n if (cancellation.signal.aborted || (run.progressJobId && await progressService.isCancellationRequested(run.progressJobId, scope.tenantId, scope.organizationId))) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return 'stop'\n }\n\n const delta = applyExportCounters(batch)\n processedCount += delta.processedCount\n\n span.setAttributes({\n 'data_sync.processed_count': delta.processedCount,\n 'data_sync.updated_count': delta.updatedCount,\n 'data_sync.skipped_count': delta.skippedCount,\n 'data_sync.failed_count': delta.failedCount,\n })\n\n await syncRunService.commitBatchProgress(\n run.id,\n {\n createdCount: 0,\n updatedCount: delta.updatedCount,\n skippedCount: delta.skippedCount,\n failedCount: delta.failedCount,\n batchesCompleted: 1,\n },\n batch.cursor,\n scope,\n { expectedBatchesCompleted: committedBatches, persistSharedCursor },\n )\n committedBatches += 1\n streamReportedDone = batch.hasMore === false\n await updateProgress(run.progressJobId, processedCount, null, scope)\n await logExportItemFailures(run.id, run.integrationId, batch.results, scope)\n\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: `Processed export batch ${batch.batchIndex}`,\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Processed ${processedCount} export items so far.`,\n processedCount,\n batchSize: batch.results.length,\n cursor: batch.cursor,\n },\n })\n\n return 'continue'\n },\n )\n if (streamResult === 'stopped') return\n } catch (error) {\n if (error instanceof SyncRunOwnershipConflictError) {\n logger.warn('Yielding export run to a concurrent worker that already advanced it', {\n runId: run.id,\n expectedBatchesCompleted: error.expectedBatchesCompleted,\n })\n return\n }\n // An adapter that honours the signal may reject instead of returning, so our own abort is\n // a cancellation rather than a fault \u2014 and must not leave an `error` entry in the\n // integration log for a run the operator cancelled on purpose. Anything else that merely\n // coincided with the cancel \u2014 a rejecting commit, an upstream 500 \u2014 is a genuine failure\n // and keeps its log entry, its message, its `failed` status and its failed event.\n if (cancellation.signal.aborted && isAbortError(error)) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n const message = error instanceof Error ? error.message : 'Sync export failed'\n await integrationLogService.write(\n {\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message,\n code: RUN_FAILED_CODE,\n },\n scope,\n )\n await finalizeRun(run.id, 'failed', scope, message, operationalTelemetry)\n return\n }\n\n // An adapter that honours the signal stops mid-batch and returns WITHOUT yielding, so the\n // batch handler \u2014 which owns the only other `cancelled` transition \u2014 never runs and the\n // stream reports `completed`.\n //\n // `streamReportedDone` keeps that from swallowing a run that genuinely finished: an adapter\n // that ignores the signal and drains after reporting `hasMore: false` delivered everything it\n // had, even when the cancel landed during the final read. Calling that cancelled would tell\n // the operator a complete sync was partial and leave a finished run resumable.\n if (cancellation.signal.aborted && !streamReportedDone) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n\n await finalizeRun(run.id, 'completed', scope, undefined, operationalTelemetry)\n },\n }\n}\n\nexport type SyncEngine = ReturnType<typeof createSyncEngine>\n"],
|
|
5
|
-
"mappings": "AAKA,SAAS,iCAAiC;AAC1C,SAAS,+BAA+B;AACxC,SAAS,yBAAyB;AAElC,SAAS,oBAAoB,0BAA0B;AAEvD,SAAS,qCAAqC;AAC9C,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,qBAAqB;AAG9B,MAAM,SAAS,aAAa,WAAW,EAAE,MAAM,EAAE,WAAW,cAAc,CAAC;AAQpE,MAAM,mCAAmC,MAAM;AAAA,EACpD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AASA,MAAM,kBAAkB;AAGxB,SAAS,mBAAmB,KAAc,OAA2C;AACnF,SAAO;AAAA,IACL,oBAAoB,IAAI;AAAA,IACxB,4BAA4B,IAAI;AAAA,IAChC,yBAAyB,IAAI;AAAA,IAC7B,uBAAuB,IAAI;AAAA,IAC3B,gBAAgB,MAAM;AAAA,IACtB,sBAAsB,MAAM;AAAA,EAC9B;AACF;AAWA,SAAS,cAAc,MAA+B,UAA0B;AAC9E,SAAO,cAAc,KAAK,WAAW,QAAQ;AAC/C;AA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AW;AAAA,MAClC,SAAS,OAAO;AACd,YAAI,iBAAiB,+BAA+B;AAClD,iBAAO,KAAK,uEAAuE;AAAA,YACjF,OAAO,IAAI;AAAA,YACX,0BAA0B,MAAM;AAAA,UAClC,CAAC;AACD;AAAA,QACF;AAMA,YAAI,aAAa,OAAO,WAAW,aAAa,KAAK,GAAG;AACtD,gBAAM,YAAY,IAAI,IAAI,aAAa,OAAO,QAAW,oBAAoB;AAC7E;AAAA,QACF;AACA,cAAM,UAAU,iBAAiB,QAAQ,MAAM,UAAU;AACzD,cAAM,sBAAsB;AAAA,UAC1B;AAAA,YACE,eAAe,IAAI;AAAA,YACnB,OAAO,IAAI;AAAA,YACX,OAAO;AAAA,YACP;AAAA,YACA,MAAM;AAAA,UACR;AAAA,UACA;AAAA,QACF;AACA,cAAM,YAAY,IAAI,IAAI,UAAU,OAAO,SAAS,oBAAoB;AACxE;AAAA,MACF;AAUA,UAAI,aAAa,OAAO,WAAW,CAAC,oBAAoB;AACtD,cAAM,YAAY,IAAI,IAAI,aAAa,OAAO,QAAW,oBAAoB;AAC7E;AAAA,MACF;AAEA,YAAM,YAAY,IAAI,IAAI,aAAa,OAAO,QAAW,oBAAoB;AAAA,IAC/E;AAAA,EACF;AACF;",
|
|
4
|
+
"sourcesContent": ["import type { EntityManager } from '@mikro-orm/postgresql'\nimport type { CredentialsService } from '../../integrations/lib/credentials-service'\nimport type { IntegrationLogService } from '../../integrations/lib/log-service'\nimport type { IntegrationStateService } from '../../integrations/lib/state-service'\nimport type { ProgressService } from '../../progress/lib/progressService'\nimport { STALE_JOB_TIMEOUT_SECONDS } from '../../progress/lib/progressService'\nimport { refreshCoverageSnapshot } from '../../query_index/lib/coverage'\nimport { emitDataSyncEvent } from '../events'\nimport type { DataSyncAdapter, DataMapping, ExportBatch, ImportBatch, RunParameterValue } from './adapter'\nimport { getDataSyncAdapter, resolveProviderKey } from './adapter-registry'\nimport type { SyncRunService } from './sync-run-service'\nimport { SyncRunOwnershipConflictError } from './sync-run-service'\nimport { forEachBatch } from './batch-stream'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport {\n captureTelemetryTrace,\n getTelemetryRuntime,\n type TelemetrySpanAttributes,\n} from '@open-mercato/shared/lib/telemetry/runtime'\nimport { groupableCode } from '@open-mercato/shared/lib/telemetry/error-code'\nimport type { SyncRun } from '../data/entities'\n\nconst logger = createLogger('data_sync').child({ component: 'sync-engine' })\n\n/**\n * A run that finished with failed items. Raised so a partial success is one\n * reported error with a count, at the granularity an operator acts on \u2014 a\n * different fact from any single item's failure, which the per-item error rows\n * report on their own.\n */\nexport class SyncRunPartialFailureError extends Error {\n constructor(message: string) {\n super(message)\n this.name = 'SyncRunPartialFailureError'\n }\n}\n\n/**\n * The fingerprint for a run that ended in a fault.\n *\n * One code for now. PR #5450's `classifySyncError` splits faults into transient\n * and terminal; when it lands, this is the single place that becomes\n * `data_sync.run_transient` / `data_sync.run_terminal`.\n */\nconst RUN_FAILED_CODE = 'data_sync.run_failed'\n\n/** Run identity for a reported error, mirroring `runSpanAttributes` minus the provider key. */\nfunction runEventAttributes(run: SyncRun, scope: SyncScope): TelemetrySpanAttributes {\n return {\n 'data_sync.run_id': run.id,\n 'data_sync.integration_id': run.integrationId,\n 'data_sync.entity_type': run.entityType,\n 'data_sync.direction': run.direction,\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n }\n}\n\n/**\n * The failure fingerprint for a dead-lettered item.\n *\n * An adapter that classifies its own failures sets `errorCode` on the item's data\n * (a stable `module.reason` token, never an interpolated string); anything that is\n * not that shape falls back rather than being trusted, and the fallback is a real\n * code rather than `unknown`, so grouping works even for an adapter that supplies\n * nothing.\n */\nfunction itemErrorCode(data: Record<string, unknown>, fallback: string): string {\n return groupableCode(data.errorCode, fallback)\n}\n\n/**\n * Report a `data_sync` failure that is otherwise only recorded (a dropped\n * promise, a run's own summary). Wrapped: observability may never decide the fate\n * of a batch that is already committed.\n */\nfunction reportSyncError(\n error: unknown,\n code: string,\n attributes: TelemetrySpanAttributes,\n): void {\n try {\n getTelemetryRuntime()?.reportError(error, { module: 'data_sync', code, attributes })\n } catch (telemetryError) {\n logger.warn('Failed to report a data sync error to telemetry', { code, err: telemetryError as Error })\n }\n}\n\ntype RunParameters = Record<string, RunParameterValue>\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n userId?: string | null\n}\n\ntype EngineDeps = {\n em: EntityManager\n syncRunService: SyncRunService\n integrationCredentialsService: CredentialsService\n integrationLogService: IntegrationLogService\n integrationStateService?: IntegrationStateService\n progressService: ProgressService\n}\n\n/** Repeated on every batch span so a rooted batch trace identifies its run on its own. */\nfunction runSpanAttributes(run: SyncRun, providerKey: string, scope: SyncScope): TelemetrySpanAttributes {\n return {\n 'data_sync.run_id': run.id,\n 'data_sync.integration_id': run.integrationId,\n 'data_sync.provider_key': providerKey,\n 'data_sync.entity_type': run.entityType,\n 'data_sync.direction': run.direction,\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n }\n}\n\nfunction applyImportCounters(batch: ImportBatch): Pick<Required<SyncCounterDelta>, 'createdCount' | 'updatedCount' | 'skippedCount' | 'failedCount'> {\n let createdCount = 0\n let updatedCount = 0\n let skippedCount = 0\n let failedCount = 0\n\n for (const item of batch.items) {\n if (item.action === 'create') createdCount += 1\n else if (item.action === 'update') updatedCount += 1\n else if (item.action === 'failed') failedCount += 1\n else skippedCount += 1\n }\n\n return { createdCount, updatedCount, skippedCount, failedCount }\n}\n\ntype SyncCounterDelta = {\n createdCount?: number\n updatedCount?: number\n skippedCount?: number\n failedCount?: number\n processedCount: number\n}\n\nfunction applyExportCounters(batch: ExportBatch): SyncCounterDelta {\n let failedCount = 0\n let skippedCount = 0\n let updatedCount = 0\n\n for (const result of batch.results) {\n if (result.status === 'error') failedCount += 1\n else if (result.status === 'skipped') skippedCount += 1\n else updatedCount += 1\n }\n\n return {\n failedCount,\n skippedCount,\n updatedCount,\n processedCount: batch.results.length,\n }\n}\n\n// Adapter batches can legitimately outlast the stale-job sweep window (slow upstream\n// APIs), so the engine must heartbeat while a batch is still being produced. The same\n// tick also polls cancellation, so a cancel lands within one interval instead of waiting\n// out the batch \u2014 sharing this timer rather than adding a second one that would double\n// the per-interval round-trips for the whole life of a run.\nconst HEARTBEAT_TICK_MS = (STALE_JOB_TIMEOUT_SECONDS * 1000) / 4\n\n// Our own abort, as opposed to a failure that merely coincided with one. Adapters are told to\n// return rather than throw, but `signal.throwIfAborted()` and an aborted `fetch` both surface as\n// this, and either is a cancellation rather than a fault.\n//\n// Matched structurally on `name` rather than with `instanceof Error`, because those two throw a\n// `DOMException`, and whether that inherits from `Error` depends on the runtime \u2014 it does under\n// bare Node 24 and does NOT under the jest environment this is tested in. An `instanceof` test\n// therefore passes or fails on where the code runs, which is not something cancellation should\n// depend on.\nfunction isAbortError(error: unknown): boolean {\n return typeof error === 'object' && error !== null && (error as { name?: unknown }).name === 'AbortError'\n}\n\n// The interval is armed once and spans the whole iteration, the `yield` included, rather than being\n// restarted around each `next()`. Producing a batch is not the only thing that can outlast the stale\n// sweep: the consumer body between `yield` and the next `next()` commits batch progress, refreshes\n// coverage snapshots, logs item failures and writes the operational log, and a coverage refresh over\n// a large query index is slow enough that a per-read timer left that window unheartbeated (#5370).\n// The generator body does not start until the first `next()`, so a stream nobody consumes still arms\n// nothing. Every way out of `forEachBatch` \u2014 drain, early close, a throwing read, a throwing consumer \u2014\n// either completes this generator or calls `return()` on it, so the `finally` below always clears the\n// timer. That last part is a property of the consumer rather than of this helper: a consumer that\n// abandoned the generator mid-`yield` without closing it would leak the interval, which the old\n// per-read arming could not do. Any new consumer must honour `IteratorClose`.\n//\n// Every callback on this timer MUST use a forked EntityManager. The window now covers the consumer\n// body, where `commitBatchProgress` holds the shared EM inside `em.begin()`/`em.commit()`, so a tick\n// that read on the shared EM would issue a query into that open transaction and interleave with its\n// UnitOfWork \u2014 the shape `packages/core/AGENTS.md` bans and `withAtomicFlush` cannot guard against.\nasync function* withHeartbeat<T>(source: AsyncIterable<T>, tick: () => void, intervalMs: number): AsyncGenerator<T, void, undefined> {\n const iterator = source[Symbol.asyncIterator]()\n const timer = setInterval(tick, intervalMs)\n try {\n while (true) {\n const result = await iterator.next()\n if (result.done) return\n yield result.value\n }\n } finally {\n clearInterval(timer)\n await iterator.return?.()\n }\n}\n\nexport function createSyncEngine(deps: EngineDeps) {\n const { syncRunService, integrationCredentialsService, integrationLogService, integrationStateService, progressService } = deps\n\n async function resolveMapping(adapter: DataSyncAdapter, entityType: string, scope: SyncScope): Promise<DataMapping> {\n return adapter.getMapping({\n entityType,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n })\n }\n\n async function updateProgress(progressJobId: string | null | undefined, processedCount: number, totalCount: number | null, scope: SyncScope): Promise<void> {\n if (!progressJobId) return\n\n await progressService.updateProgress(\n progressJobId,\n {\n processedCount,\n totalCount: totalCount ?? undefined,\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n }\n\n // On redelivery the progress counter must resume where the last delivery left off the\n // same way committedBatches does \u2014 updateProgress writes absolute counts, so starting at\n // zero would regress the visible count. The progress job's own processedCount is the only\n // persisted value already in the engine's unit: `batch.processedCount ?? items.length`,\n // i.e. source records. The run's created/updated/skipped/failed counters count emitted\n // items, which adapters may explode several-per-source-record (Akeneo yields a product\n // plus its variants), so seeding from them would overshoot the total and pin the bar.\n async function seedProcessedCount(progressJobId: string | null | undefined, scope: SyncScope): Promise<number> {\n if (!progressJobId) return 0\n const job = await progressService.getJob(progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n return job?.processedCount ?? 0\n }\n\n function makeHeartbeatTick(progressJobId: string | null | undefined, scope: SyncScope): () => void {\n const touchJobHeartbeat = progressService.touchJobHeartbeat?.bind(progressService)\n if (!progressJobId || !touchJobHeartbeat) return () => {}\n let inFlight = false\n return () => {\n if (inFlight) return\n inFlight = true\n touchJobHeartbeat(progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n .catch((error) => {\n logger.warn('Progress heartbeat failed', {\n progressJobId,\n error: error instanceof Error ? error.message : String(error),\n })\n })\n .finally(() => {\n inFlight = false\n })\n }\n }\n\n // Rides the heartbeat timer, which is the only thing that runs while the adapter is still\n // producing a batch \u2014 the engine's own cancellation check sits in the batch handler and is\n // reached only after a yield. Since that timer now spans the consumer body too, a cancel is\n // also observed during the per-batch bookkeeping rather than waiting out the next read.\n // Swallows its own errors because it runs on a timer, where an unhandled rejection is fatal,\n // and stops polling once it has aborted.\n function makeCancellationTick(progressJobId: string | null | undefined, scope: SyncScope, controller: AbortController): () => void {\n if (!progressJobId) return () => {}\n let inFlight = false\n return () => {\n if (inFlight || controller.signal.aborted) return\n inFlight = true\n progressService.isCancellationRequested(progressJobId, scope.tenantId, scope.organizationId)\n .then((cancelled) => {\n if (cancelled) controller.abort()\n })\n .catch((error) => {\n logger.warn('Cancellation poll failed', {\n progressJobId,\n error: error instanceof Error ? error.message : String(error),\n })\n })\n .finally(() => {\n inFlight = false\n })\n }\n }\n\n async function refreshCoverageSnapshots(entityTypes: string[] | undefined, scope: SyncScope): Promise<void> {\n if (!entityTypes || entityTypes.length === 0) return\n\n const types = Array.from(\n new Set(entityTypes.filter((value) => typeof value === 'string' && value.trim().length > 0)),\n )\n const outcomes = await Promise.allSettled(\n types.map((entityType) => refreshCoverageSnapshot(deps.em, {\n entityType,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })),\n )\n // `allSettled` keeps a failed refresh from failing a committed batch, which is\n // right \u2014 but on its own it also discards the reason entirely, leaving no row,\n // no log and no signal. Reporting is the whole difference between degrading and\n // going silent.\n outcomes.forEach((outcome, index) => {\n if (outcome.status !== 'rejected') return\n logger.warn('Coverage snapshot refresh failed', { entityType: types[index], err: outcome.reason as Error })\n reportSyncError(outcome.reason, 'data_sync.coverage_refresh_failed', {\n entityType: types[index],\n 'om.tenant_id': scope.tenantId,\n 'om.organization_id': scope.organizationId,\n })\n })\n }\n\n async function logImportItemFailures(\n runId: string,\n integrationId: string,\n items: ImportBatch['items'],\n scope: SyncScope,\n ): Promise<void> {\n const failedItems = items.filter((item) => item.action === 'failed')\n for (const item of failedItems) {\n const errorMessage = typeof item.data.errorMessage === 'string' && item.data.errorMessage.trim().length > 0\n ? item.data.errorMessage.trim()\n : 'Import item failed'\n const sourceProductUuid = typeof item.data.sourceProductUuid === 'string' && item.data.sourceProductUuid.trim().length > 0\n ? item.data.sourceProductUuid.trim()\n : null\n const sourceIdentifier = typeof item.data.sourceIdentifier === 'string' && item.data.sourceIdentifier.trim().length > 0\n ? item.data.sourceIdentifier.trim()\n : null\n const message = [\n `Failed to import item ${item.externalId}`,\n sourceProductUuid ? `(uuid: ${sourceProductUuid})` : null,\n sourceIdentifier ? `(identifier: ${sourceIdentifier})` : null,\n `: ${errorMessage}`,\n ].filter((part) => part !== null).join(' ')\n\n await integrationLogService.write(\n {\n integrationId,\n runId,\n level: 'error',\n message,\n code: itemErrorCode(item.data, 'data_sync.item_failed'),\n payload: item.data,\n },\n scope,\n )\n }\n }\n\n async function logExportItemFailures(\n runId: string,\n integrationId: string,\n results: ExportBatch['results'],\n scope: SyncScope,\n ): Promise<void> {\n const failedResults = results.filter((result) => result.status === 'error' && result.error)\n for (const result of failedResults) {\n const label = result.externalId ? `${result.externalId} (id: ${result.localId})` : result.localId\n const errorMessage = result.error!.split('\\n')[0]\n const message = `Failed to export item ${label}: ${errorMessage}`\n\n await integrationLogService.write(\n {\n integrationId,\n runId,\n level: 'error',\n message,\n code: 'data_sync.export_item_failed',\n payload: { kind: 'export-item-failure', summary: result.error },\n },\n scope,\n )\n }\n }\n\n /**\n * The adapter-gated operational log. Deliberately carries no `code`: these rows\n * are run status records, and every fault they narrate was already written \u2014 and\n * therefore already reported \u2014 by a direct `level: 'error'` write that owns the\n * fingerprint. A code here would double-report every fault for adapters that\n * have `operationalTelemetry` on.\n */\n async function writeOperationalLog(params: {\n integrationId: string\n runId: string\n level: 'info' | 'warn' | 'error'\n message: string\n scope: SyncScope\n enabled: boolean\n payload?: Record<string, unknown>\n }): Promise<void> {\n if (!params.enabled) return\n\n await integrationLogService.write(\n {\n integrationId: params.integrationId,\n runId: params.runId,\n level: params.level,\n message: params.message,\n payload: params.payload,\n },\n params.scope,\n )\n }\n\n async function updateOperationalState(params: {\n integrationId: string\n status: 'healthy' | 'degraded' | 'unhealthy'\n scope: SyncScope\n enabled: boolean\n }): Promise<void> {\n if (!params.enabled || !integrationStateService) return\n\n await integrationStateService.upsert(\n params.integrationId,\n {\n lastHealthStatus: params.status,\n lastHealthCheckedAt: new Date(),\n },\n params.scope,\n )\n }\n\n async function finalizeRun(\n runId: string,\n status: 'completed' | 'failed' | 'cancelled',\n scope: SyncScope,\n error?: string,\n operationalTelemetry = false,\n ): Promise<void> {\n const existingRun = await syncRunService.getRun(runId, scope)\n const alreadyFinalizedWithSameStatus = existingRun?.status === status\n && (status === 'completed' || status === 'failed' || status === 'cancelled')\n\n const run = await syncRunService.markStatus(runId, status, scope, error)\n if (!run) return\n\n if (alreadyFinalizedWithSameStatus) {\n return\n }\n\n if (run.status !== status) {\n // `markStatus` refuses a terminal -> different-terminal transition and\n // returns the row unchanged, so the run is already finished under another\n // delivery of this job. Everything below \u2014 the progress job, the\n // operational log and the lifecycle event \u2014 would describe the wrong\n // outcome, and `data_sync.run.failed` is dispatched to tenant webhooks.\n // A displaced worker stays silent instead.\n logger.warn('Skipping finalization of a sync run another worker already finalized', {\n runId,\n requestedStatus: status,\n actualStatus: run.status,\n })\n return\n }\n\n if (run.progressJobId) {\n if (status === 'completed') {\n await progressService.completeJob(\n run.progressJobId,\n {\n resultSummary: {\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n batchesCompleted: run.batchesCompleted,\n },\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n } else if (status === 'failed') {\n await progressService.failJob(\n run.progressJobId,\n {\n errorMessage: error ?? 'Sync run failed',\n },\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n } else if (status === 'cancelled') {\n await progressService.markCancelled(\n run.progressJobId,\n {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n },\n )\n }\n }\n\n if (status === 'completed') {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'healthy',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run completed',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'completed',\n summary: `Sync completed with ${run.createdCount} created, ${run.updatedCount} updated, ${run.failedCount} failed.`,\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n batchesCompleted: run.batchesCompleted,\n },\n })\n } else if (status === 'cancelled') {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'warn',\n message: 'Sync run cancelled',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'cancelled',\n summary: 'The sync run was cancelled before completion.',\n },\n })\n } else {\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'unhealthy',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message: error ?? 'Sync run failed',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'failed',\n summary: error ?? 'The sync run failed.',\n },\n })\n }\n\n if (status === 'completed') {\n // A run that finished with failures is a partial success, and the operator\n // finds out here or not at all: the per-item rows carry the reasons but no\n // count, and this is the only place that knows the run is over. Reported\n // outside `writeOperationalLog` on purpose \u2014 that path is gated on an adapter\n // opt-in, and an adapter flag may decide how chatty the operational log is,\n // never whether a failure is observable.\n if (run.failedCount > 0) {\n reportSyncError(\n new SyncRunPartialFailureError(`Sync run completed with ${run.failedCount} failed item(s)`),\n 'data_sync.run_partial_failure',\n {\n ...runEventAttributes(run, scope),\n 'data_sync.failed_count': run.failedCount,\n 'data_sync.created_count': run.createdCount,\n 'data_sync.updated_count': run.updatedCount,\n 'data_sync.skipped_count': run.skippedCount,\n },\n )\n }\n await emitDataSyncEvent('data_sync.run.completed', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n createdCount: run.createdCount,\n updatedCount: run.updatedCount,\n skippedCount: run.skippedCount,\n failedCount: run.failedCount,\n })\n return\n }\n\n if (status === 'cancelled') {\n await emitDataSyncEvent('data_sync.run.cancelled', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n return\n }\n\n await emitDataSyncEvent('data_sync.run.failed', {\n runId,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n error: error ?? null,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n }\n\n return {\n async runImport(runId: string, batchSize: number, scope: SyncScope): Promise<void> {\n const run = await syncRunService.getRun(runId, scope)\n if (!run) {\n logger.warn('Skipping stale import job for missing run', { runId })\n return\n }\n if (run.status === 'cancelled') {\n if (run.progressJobId) {\n await progressService.markCancelled(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n return\n }\n\n const providerKey = resolveProviderKey(run.integrationId)\n const adapter = getDataSyncAdapter(providerKey)\n if (!adapter?.streamImport) {\n throw new Error(`No import adapter registered for provider ${providerKey}`)\n }\n const operationalTelemetry = adapter.operationalTelemetry === true\n const persistSharedCursor = adapter.persistsSharedCursor?.(run.entityType) ?? true\n\n const credentials = await integrationCredentialsService.resolve(run.integrationId, scope)\n if (!credentials) {\n throw new Error(`Integration ${run.integrationId} is missing credentials`)\n }\n\n // A run already `running` means a stalled job was redelivered, so this is\n // a resume rather than a first start. Consumers see one `started` event per\n // delivery either way; the flag is what lets them tell the two apart.\n const resumed = run.status === 'running'\n const activeRun = await syncRunService.markStatus(run.id, 'running', scope)\n if (!activeRun || activeRun.status !== 'running') {\n return\n }\n await emitDataSyncEvent('data_sync.run.started', {\n runId: run.id,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n resumed,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run started',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Import run started for ${run.entityType}.`,\n entityType: run.entityType,\n direction: run.direction,\n },\n })\n\n if (run.progressJobId) {\n await progressService.startJob(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n\n const mapping = await resolveMapping(adapter, run.entityType, scope)\n let processedCount = await seedProcessedCount(run.progressJobId, scope)\n let totalCount: number | null = null\n let committedBatches = activeRun.batchesCompleted ?? 0\n // Whether the last committed batch said the source was exhausted. Distinguishes a stream that\n // drained from one the adapter stopped early \u2014 see the post-stream finalize below.\n let streamReportedDone = false\n // Captured while the triggering job's span is still the active one, so\n // every rooted batch trace can link back to it.\n const runTrace = captureTelemetryTrace()\n const spanAttributes = runSpanAttributes(run, providerKey, scope)\n // Declared outside the try because both the catch and the completion path below read it.\n const cancellation = new AbortController()\n const heartbeat = makeHeartbeatTick(run.progressJobId, scope)\n const pollCancellation = makeCancellationTick(run.progressJobId, scope, cancellation)\n\n try {\n const streamResult = await forEachBatch(\n withHeartbeat(\n adapter.streamImport({\n entityType: run.entityType,\n cursor: run.cursor ?? undefined,\n batchSize,\n credentials,\n mapping,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n runId: run.id,\n parameters: (run.parameters ?? {}) as RunParameters,\n signal: cancellation.signal,\n }),\n // `finally` so a synchronous throw from the heartbeat cannot also stop cancellation\n // from being observed for the rest of the run.\n () => { try { heartbeat() } finally { pollCancellation() } },\n HEARTBEAT_TICK_MS,\n ),\n {\n spanName: 'data_sync.import.batch',\n drainSpanName: 'data_sync.import.drain',\n attributes: spanAttributes,\n linkTo: runTrace,\n },\n async (batch, span) => {\n span.setAttributes({\n 'data_sync.batch_index': batch.batchIndex,\n 'data_sync.batch_size': batch.items.length,\n })\n\n if (cancellation.signal.aborted || (run.progressJobId && await progressService.isCancellationRequested(run.progressJobId, scope.tenantId, scope.organizationId))) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return 'stop'\n }\n\n const delta = applyImportCounters(batch)\n const processedBatchCount = batch.processedCount ?? batch.items.length\n processedCount += processedBatchCount\n totalCount = batch.totalEstimate ?? totalCount\n\n span.setAttributes({\n 'data_sync.processed_count': processedBatchCount,\n 'data_sync.created_count': delta.createdCount,\n 'data_sync.updated_count': delta.updatedCount,\n 'data_sync.skipped_count': delta.skippedCount,\n 'data_sync.failed_count': delta.failedCount,\n })\n\n await syncRunService.commitBatchProgress(\n run.id,\n {\n ...delta,\n batchesCompleted: 1,\n },\n batch.cursor,\n scope,\n { expectedBatchesCompleted: committedBatches, persistSharedCursor },\n )\n committedBatches += 1\n streamReportedDone = batch.hasMore === false\n\n await updateProgress(run.progressJobId, processedCount, totalCount, scope)\n await refreshCoverageSnapshots(batch.refreshCoverageEntityTypes, scope)\n await logImportItemFailures(run.id, run.integrationId, batch.items, scope)\n\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: batch.message?.trim().length\n ? batch.message.trim()\n : `Processed import batch ${batch.batchIndex}`,\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Processed ${processedCount}${totalCount ? ` of ${totalCount}` : ''} rows so far.`,\n processedCount,\n batchSize: batch.items.length,\n processedBatchCount,\n cursor: batch.cursor,\n },\n })\n\n return 'continue'\n },\n )\n if (streamResult === 'stopped') return\n } catch (error) {\n if (error instanceof SyncRunOwnershipConflictError) {\n logger.warn('Yielding import run to a concurrent worker that already advanced it', {\n runId: run.id,\n expectedBatchesCompleted: error.expectedBatchesCompleted,\n })\n return\n }\n // An adapter that honours the signal may reject instead of returning, so our own abort is\n // a cancellation rather than a fault \u2014 and must not leave an `error` entry in the\n // integration log for a run the operator cancelled on purpose. Anything else that merely\n // coincided with the cancel \u2014 a rejecting commit, an upstream 500 \u2014 is a genuine failure\n // and keeps its log entry, its message, its `failed` status and its failed event.\n if (cancellation.signal.aborted && isAbortError(error)) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n const message = error instanceof Error ? error.message : 'Sync import failed'\n await integrationLogService.write(\n {\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message,\n code: RUN_FAILED_CODE,\n },\n scope,\n )\n await finalizeRun(run.id, 'failed', scope, message, operationalTelemetry)\n return\n }\n\n // An adapter that honours the signal stops mid-batch and returns WITHOUT yielding, so the\n // batch handler \u2014 which owns the only other `cancelled` transition \u2014 never runs and the\n // stream reports `completed`.\n //\n // `streamReportedDone` keeps that from swallowing a run that genuinely finished: an adapter\n // that ignores the signal and drains after reporting `hasMore: false` delivered everything it\n // had, even when the cancel landed during the final read. Calling that cancelled would tell\n // the operator a complete sync was partial and leave a finished run resumable.\n if (cancellation.signal.aborted && !streamReportedDone) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n\n await finalizeRun(run.id, 'completed', scope, undefined, operationalTelemetry)\n },\n\n async runExport(runId: string, batchSize: number, scope: SyncScope): Promise<void> {\n const run = await syncRunService.getRun(runId, scope)\n if (!run) {\n logger.warn('Skipping stale export job for missing run', { runId })\n return\n }\n if (run.status === 'cancelled') {\n if (run.progressJobId) {\n await progressService.markCancelled(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n return\n }\n\n const providerKey = resolveProviderKey(run.integrationId)\n const adapter = getDataSyncAdapter(providerKey)\n if (!adapter?.streamExport) {\n throw new Error(`No export adapter registered for provider ${providerKey}`)\n }\n const operationalTelemetry = adapter.operationalTelemetry === true\n const persistSharedCursor = adapter.persistsSharedCursor?.(run.entityType) ?? true\n\n const credentials = await integrationCredentialsService.resolve(run.integrationId, scope)\n if (!credentials) {\n throw new Error(`Integration ${run.integrationId} is missing credentials`)\n }\n\n // A run already `running` means a stalled job was redelivered, so this is\n // a resume rather than a first start. Consumers see one `started` event per\n // delivery either way; the flag is what lets them tell the two apart.\n const resumed = run.status === 'running'\n const activeRun = await syncRunService.markStatus(run.id, 'running', scope)\n if (!activeRun || activeRun.status !== 'running') {\n return\n }\n await emitDataSyncEvent('data_sync.run.started', {\n runId: run.id,\n integrationId: run.integrationId,\n entityType: run.entityType,\n direction: run.direction,\n resumed,\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n })\n await updateOperationalState({\n integrationId: run.integrationId,\n status: 'degraded',\n scope,\n enabled: operationalTelemetry,\n })\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: 'Sync run started',\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Export run started for ${run.entityType}.`,\n entityType: run.entityType,\n direction: run.direction,\n },\n })\n\n if (run.progressJobId) {\n await progressService.startJob(run.progressJobId, {\n tenantId: scope.tenantId,\n organizationId: scope.organizationId,\n userId: scope.userId,\n })\n }\n\n const mapping = await resolveMapping(adapter, run.entityType, scope)\n let processedCount = await seedProcessedCount(run.progressJobId, scope)\n let committedBatches = activeRun.batchesCompleted ?? 0\n // Whether the last committed batch said the source was exhausted. Distinguishes a stream that\n // drained from one the adapter stopped early \u2014 see the post-stream finalize below.\n let streamReportedDone = false\n // Captured while the triggering job's span is still the active one, so\n // every rooted batch trace can link back to it.\n const runTrace = captureTelemetryTrace()\n const spanAttributes = runSpanAttributes(run, providerKey, scope)\n // Declared outside the try because both the catch and the completion path below read it.\n const cancellation = new AbortController()\n const heartbeat = makeHeartbeatTick(run.progressJobId, scope)\n const pollCancellation = makeCancellationTick(run.progressJobId, scope, cancellation)\n\n try {\n const streamResult = await forEachBatch(\n withHeartbeat(\n adapter.streamExport({\n entityType: run.entityType,\n cursor: run.cursor ?? undefined,\n batchSize,\n credentials,\n mapping,\n scope: { organizationId: scope.organizationId, tenantId: scope.tenantId },\n runId: run.id,\n parameters: (run.parameters ?? {}) as RunParameters,\n signal: cancellation.signal,\n }),\n // `finally` so a synchronous throw from the heartbeat cannot also stop cancellation\n // from being observed for the rest of the run.\n () => { try { heartbeat() } finally { pollCancellation() } },\n HEARTBEAT_TICK_MS,\n ),\n {\n spanName: 'data_sync.export.batch',\n drainSpanName: 'data_sync.export.drain',\n attributes: spanAttributes,\n linkTo: runTrace,\n },\n async (batch, span) => {\n span.setAttributes({\n 'data_sync.batch_index': batch.batchIndex,\n 'data_sync.batch_size': batch.results.length,\n })\n\n if (cancellation.signal.aborted || (run.progressJobId && await progressService.isCancellationRequested(run.progressJobId, scope.tenantId, scope.organizationId))) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return 'stop'\n }\n\n const delta = applyExportCounters(batch)\n processedCount += delta.processedCount\n\n span.setAttributes({\n 'data_sync.processed_count': delta.processedCount,\n 'data_sync.updated_count': delta.updatedCount,\n 'data_sync.skipped_count': delta.skippedCount,\n 'data_sync.failed_count': delta.failedCount,\n })\n\n await syncRunService.commitBatchProgress(\n run.id,\n {\n createdCount: 0,\n updatedCount: delta.updatedCount,\n skippedCount: delta.skippedCount,\n failedCount: delta.failedCount,\n batchesCompleted: 1,\n },\n batch.cursor,\n scope,\n { expectedBatchesCompleted: committedBatches, persistSharedCursor },\n )\n committedBatches += 1\n streamReportedDone = batch.hasMore === false\n await updateProgress(run.progressJobId, processedCount, null, scope)\n await logExportItemFailures(run.id, run.integrationId, batch.results, scope)\n\n await writeOperationalLog({\n integrationId: run.integrationId,\n runId: run.id,\n level: 'info',\n message: `Processed export batch ${batch.batchIndex}`,\n scope,\n enabled: operationalTelemetry,\n payload: {\n operationalStatus: 'running',\n summary: `Processed ${processedCount} export items so far.`,\n processedCount,\n batchSize: batch.results.length,\n cursor: batch.cursor,\n },\n })\n\n return 'continue'\n },\n )\n if (streamResult === 'stopped') return\n } catch (error) {\n if (error instanceof SyncRunOwnershipConflictError) {\n logger.warn('Yielding export run to a concurrent worker that already advanced it', {\n runId: run.id,\n expectedBatchesCompleted: error.expectedBatchesCompleted,\n })\n return\n }\n // An adapter that honours the signal may reject instead of returning, so our own abort is\n // a cancellation rather than a fault \u2014 and must not leave an `error` entry in the\n // integration log for a run the operator cancelled on purpose. Anything else that merely\n // coincided with the cancel \u2014 a rejecting commit, an upstream 500 \u2014 is a genuine failure\n // and keeps its log entry, its message, its `failed` status and its failed event.\n if (cancellation.signal.aborted && isAbortError(error)) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n const message = error instanceof Error ? error.message : 'Sync export failed'\n await integrationLogService.write(\n {\n integrationId: run.integrationId,\n runId: run.id,\n level: 'error',\n message,\n code: RUN_FAILED_CODE,\n },\n scope,\n )\n await finalizeRun(run.id, 'failed', scope, message, operationalTelemetry)\n return\n }\n\n // An adapter that honours the signal stops mid-batch and returns WITHOUT yielding, so the\n // batch handler \u2014 which owns the only other `cancelled` transition \u2014 never runs and the\n // stream reports `completed`.\n //\n // `streamReportedDone` keeps that from swallowing a run that genuinely finished: an adapter\n // that ignores the signal and drains after reporting `hasMore: false` delivered everything it\n // had, even when the cancel landed during the final read. Calling that cancelled would tell\n // the operator a complete sync was partial and leave a finished run resumable.\n if (cancellation.signal.aborted && !streamReportedDone) {\n await finalizeRun(run.id, 'cancelled', scope, undefined, operationalTelemetry)\n return\n }\n\n await finalizeRun(run.id, 'completed', scope, undefined, operationalTelemetry)\n },\n }\n}\n\nexport type SyncEngine = ReturnType<typeof createSyncEngine>\n"],
|
|
5
|
+
"mappings": "AAKA,SAAS,iCAAiC;AAC1C,SAAS,+BAA+B;AACxC,SAAS,yBAAyB;AAElC,SAAS,oBAAoB,0BAA0B;AAEvD,SAAS,qCAAqC;AAC9C,SAAS,oBAAoB;AAC7B,SAAS,oBAAoB;AAC7B;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP,SAAS,qBAAqB;AAG9B,MAAM,SAAS,aAAa,WAAW,EAAE,MAAM,EAAE,WAAW,cAAc,CAAC;AAQpE,MAAM,mCAAmC,MAAM;AAAA,EACpD,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AASA,MAAM,kBAAkB;AAGxB,SAAS,mBAAmB,KAAc,OAA2C;AACnF,SAAO;AAAA,IACL,oBAAoB,IAAI;AAAA,IACxB,4BAA4B,IAAI;AAAA,IAChC,yBAAyB,IAAI;AAAA,IAC7B,uBAAuB,IAAI;AAAA,IAC3B,gBAAgB,MAAM;AAAA,IACtB,sBAAsB,MAAM;AAAA,EAC9B;AACF;AAWA,SAAS,cAAc,MAA+B,UAA0B;AAC9E,SAAO,cAAc,KAAK,WAAW,QAAQ;AAC/C;AAOA,SAAS,gBACP,OACA,MACA,YACM;AACN,MAAI;AACF,wBAAoB,GAAG,YAAY,OAAO,EAAE,QAAQ,aAAa,MAAM,WAAW,CAAC;AAAA,EACrF,SAAS,gBAAgB;AACvB,WAAO,KAAK,mDAAmD,EAAE,MAAM,KAAK,eAAwB,CAAC;AAAA,EACvG;AACF;AAoBA,SAAS,kBAAkB,KAAc,aAAqB,OAA2C;AACvG,SAAO;AAAA,IACL,oBAAoB,IAAI;AAAA,IACxB,4BAA4B,IAAI;AAAA,IAChC,0BAA0B;AAAA,IAC1B,yBAAyB,IAAI;AAAA,IAC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,YACX,OAAO;AAAA,YACP;AAAA,YACA,MAAM;AAAA,UACR;AAAA,UACA;AAAA,QACF;AACA,cAAM,YAAY,IAAI,IAAI,UAAU,OAAO,SAAS,oBAAoB;AACxE;AAAA,MACF;AAUA,UAAI,aAAa,OAAO,WAAW,CAAC,oBAAoB;AACtD,cAAM,YAAY,IAAI,IAAI,aAAa,OAAO,QAAW,oBAAoB;AAC7E;AAAA,MACF;AAEA,YAAM,YAAY,IAAI,IAAI,aAAa,OAAO,QAAW,oBAAoB;AAAA,IAC/E;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { findAndCountWithDecryption, findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
|
|
3
3
|
import { enforceCommandOptimisticLockWithGuards, enforceRecordGoneIsConflict } from "@open-mercato/shared/lib/crud/optimistic-lock-command";
|
|
4
|
+
import { createLogger } from "@open-mercato/shared/lib/logger";
|
|
5
|
+
import { getTelemetryRuntime } from "@open-mercato/shared/lib/telemetry/runtime";
|
|
4
6
|
import { SyncSchedule } from "../data/entities.js";
|
|
7
|
+
const logger = createLogger("data_sync").child({ component: "sync-schedule-service" });
|
|
5
8
|
function createSyncScheduleService(em, schedulerService) {
|
|
6
9
|
function requireScheduler() {
|
|
7
10
|
if (!schedulerService) {
|
|
@@ -149,10 +152,56 @@ function createSyncScheduleService(em, schedulerService) {
|
|
|
149
152
|
});
|
|
150
153
|
}
|
|
151
154
|
const scheduledJobId = row.scheduledJobId ?? row.id;
|
|
155
|
+
const wasEnabled = row.isEnabled;
|
|
152
156
|
await requireScheduler().unregister(scheduledJobId);
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
157
|
+
try {
|
|
158
|
+
row.deletedAt = /* @__PURE__ */ new Date();
|
|
159
|
+
row.isEnabled = false;
|
|
160
|
+
await em.flush();
|
|
161
|
+
} catch (error) {
|
|
162
|
+
try {
|
|
163
|
+
await requireScheduler().register({
|
|
164
|
+
id: scheduledJobId,
|
|
165
|
+
name: buildScheduleName(row),
|
|
166
|
+
description: buildScheduleDescription(row),
|
|
167
|
+
scopeType: "organization",
|
|
168
|
+
organizationId: scope.organizationId,
|
|
169
|
+
tenantId: scope.tenantId,
|
|
170
|
+
scheduleType: row.scheduleType,
|
|
171
|
+
scheduleValue: row.scheduleValue,
|
|
172
|
+
timezone: row.timezone,
|
|
173
|
+
targetType: "queue",
|
|
174
|
+
targetQueue: "data-sync-scheduled",
|
|
175
|
+
targetPayload: {
|
|
176
|
+
scheduleId: row.id,
|
|
177
|
+
scope
|
|
178
|
+
},
|
|
179
|
+
requireFeature: "data_sync.run",
|
|
180
|
+
sourceType: "module",
|
|
181
|
+
sourceModule: "data_sync",
|
|
182
|
+
isEnabled: wasEnabled
|
|
183
|
+
});
|
|
184
|
+
} catch (compensationError) {
|
|
185
|
+
logger.error("Failed to restore scheduled job after a failed schedule delete", {
|
|
186
|
+
scheduledJobId,
|
|
187
|
+
scheduleId: row.id,
|
|
188
|
+
organizationId: scope.organizationId,
|
|
189
|
+
tenantId: scope.tenantId,
|
|
190
|
+
error: compensationError instanceof Error ? compensationError.message : String(compensationError)
|
|
191
|
+
});
|
|
192
|
+
getTelemetryRuntime()?.reportError(compensationError, {
|
|
193
|
+
module: "data_sync",
|
|
194
|
+
code: "data_sync.schedule_delete_compensation_failed",
|
|
195
|
+
attributes: {
|
|
196
|
+
scheduledJobId,
|
|
197
|
+
scheduleId: row.id,
|
|
198
|
+
organizationId: scope.organizationId,
|
|
199
|
+
tenantId: scope.tenantId
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
throw error;
|
|
204
|
+
}
|
|
156
205
|
return true;
|
|
157
206
|
}
|
|
158
207
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../src/modules/data_sync/lib/sync-schedule-service.ts"],
|
|
4
|
-
"sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { AwilixContainer } from 'awilix'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { findAndCountWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { enforceCommandOptimisticLockWithGuards, enforceRecordGoneIsConflict } from '@open-mercato/shared/lib/crud/optimistic-lock-command'\nimport { SyncSchedule } from '../data/entities'\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n}\n\ntype SchedulerServiceLike = {\n register: (registration: {\n id: string\n name: string\n description?: string\n scopeType: 'organization'\n organizationId: string\n tenantId: string\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone?: string\n targetType: 'queue'\n targetQueue: string\n targetPayload: Record<string, unknown>\n requireFeature?: string\n sourceType: 'module'\n sourceModule: string\n isEnabled?: boolean\n }) => Promise<void>\n unregister: (scheduleId: string) => Promise<void>\n}\n\nexport function createSyncScheduleService(em: EntityManager, schedulerService?: SchedulerServiceLike) {\n function requireScheduler(): SchedulerServiceLike {\n if (!schedulerService) {\n throw new Error('Scheduler module is not available')\n }\n return schedulerService\n }\n\n function buildScheduleName(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Data sync: ${row.integrationId} ${row.entityType} ${row.direction}`\n }\n\n function buildScheduleDescription(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Scheduled ${row.direction} for ${row.integrationId} (${row.entityType})`\n }\n\n async function getById(id: string, scope: SyncScope): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n async function getByKey(\n integrationId: string,\n entityType: string,\n direction: 'import' | 'export',\n scope: SyncScope,\n ): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n integrationId,\n entityType,\n direction,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n return {\n getById,\n getByKey,\n\n async listSchedules(query: {\n integrationId?: string\n entityType?: string\n direction?: 'import' | 'export'\n page: number\n pageSize: number\n }, scope: SyncScope): Promise<{ items: SyncSchedule[]; total: number }> {\n const where: FilterQuery<SyncSchedule> = {\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n }\n\n if (query.integrationId) where.integrationId = query.integrationId\n if (query.entityType) where.entityType = query.entityType\n if (query.direction) where.direction = query.direction\n\n const [items, total] = await findAndCountWithDecryption(\n em,\n SyncSchedule,\n where,\n {\n orderBy: { createdAt: 'DESC' },\n limit: query.pageSize,\n offset: (query.page - 1) * query.pageSize,\n },\n scope,\n )\n\n return { items, total }\n },\n\n async saveSchedule(input: {\n id?: string\n integrationId: string\n entityType: string\n direction: 'import' | 'export'\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone: string\n fullSync: boolean\n isEnabled: boolean\n expectedUpdatedAt?: string | null\n }, scope: SyncScope, container?: AwilixContainer): Promise<SyncSchedule> {\n const existing = input.id\n ? await getById(input.id, scope)\n : await getByKey(input.integrationId, input.entityType, input.direction, scope)\n\n if (existing && container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: existing.id,\n current: existing.updatedAt ?? null,\n expected: input.expectedUpdatedAt ?? null,\n })\n } else if (!existing && input.expectedUpdatedAt) {\n // Concurrent delete-then-edit race: the client edited a schedule that was\n // removed before this keyed upsert ran. Surface the unified conflict\n // instead of silently re-creating it. No-op when no expected version was\n // sent (a genuine create) or when OM_OPTIMISTIC_LOCK is off.\n enforceRecordGoneIsConflict({\n resourceKind: 'data_sync.schedule',\n resourceId: input.id ?? `${input.integrationId}:${input.entityType}:${input.direction}`,\n expected: input.expectedUpdatedAt,\n })\n }\n\n const id = existing?.id ?? randomUUID()\n const scheduledJobId = existing?.scheduledJobId ?? id\n\n // Validate the schedule (and register it with the scheduler) before writing\n // the SyncSchedule row \u2014 an unparseable scheduleValue must not leave a\n // persisted row with no working schedule behind it.\n await requireScheduler().register({\n id: scheduledJobId,\n name: buildScheduleName(input),\n description: buildScheduleDescription(input),\n scopeType: 'organization',\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n targetType: 'queue',\n targetQueue: 'data-sync-scheduled',\n targetPayload: {\n scheduleId: id,\n scope,\n },\n requireFeature: 'data_sync.run',\n sourceType: 'module',\n sourceModule: 'data_sync',\n isEnabled: input.isEnabled,\n })\n\n const row = existing ?? em.create(SyncSchedule, {\n id,\n integrationId: input.integrationId,\n entityType: input.entityType,\n direction: input.direction,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n fullSync: input.fullSync,\n isEnabled: input.isEnabled,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n })\n\n row.integrationId = input.integrationId\n row.entityType = input.entityType\n row.direction = input.direction\n row.scheduleType = input.scheduleType\n row.scheduleValue = input.scheduleValue\n row.timezone = input.timezone\n row.fullSync = input.fullSync\n row.isEnabled = input.isEnabled\n row.scheduledJobId = scheduledJobId\n\n if (!existing) {\n em.persist(row)\n }\n\n await em.flush()\n\n return row\n },\n\n async deleteSchedule(\n id: string,\n scope: SyncScope,\n container?: AwilixContainer,\n expectedUpdatedAt?: string | null,\n ): Promise<boolean> {\n const row = await getById(id, scope)\n if (!row) return false\n\n if (container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: row.id,\n current: row.updatedAt ?? null,\n expected: expectedUpdatedAt ?? null,\n })\n }\n\n const scheduledJobId = row.scheduledJobId ?? row.id\n await requireScheduler().unregister(scheduledJobId)\n\n row.deletedAt = new Date()\n row.isEnabled = false\n await em.flush()\n return true\n },\n }\n}\n\nexport type SyncScheduleService = ReturnType<typeof createSyncScheduleService>\n"],
|
|
5
|
-
"mappings": "AAAA,SAAS,kBAAkB;AAG3B,SAAS,4BAA4B,6BAA6B;AAClE,SAAS,wCAAwC,mCAAmC;AACpF,SAAS,oBAAoB;
|
|
4
|
+
"sourcesContent": ["import { randomUUID } from 'node:crypto'\nimport type { AwilixContainer } from 'awilix'\nimport type { EntityManager, FilterQuery } from '@mikro-orm/postgresql'\nimport { findAndCountWithDecryption, findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'\nimport { enforceCommandOptimisticLockWithGuards, enforceRecordGoneIsConflict } from '@open-mercato/shared/lib/crud/optimistic-lock-command'\nimport { createLogger } from '@open-mercato/shared/lib/logger'\nimport { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'\nimport { SyncSchedule } from '../data/entities'\n\nconst logger = createLogger('data_sync').child({ component: 'sync-schedule-service' })\n\ntype SyncScope = {\n organizationId: string\n tenantId: string\n}\n\ntype SchedulerServiceLike = {\n register: (registration: {\n id: string\n name: string\n description?: string\n scopeType: 'organization'\n organizationId: string\n tenantId: string\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone?: string\n targetType: 'queue'\n targetQueue: string\n targetPayload: Record<string, unknown>\n requireFeature?: string\n sourceType: 'module'\n sourceModule: string\n isEnabled?: boolean\n }) => Promise<void>\n unregister: (scheduleId: string) => Promise<void>\n}\n\nexport function createSyncScheduleService(em: EntityManager, schedulerService?: SchedulerServiceLike) {\n function requireScheduler(): SchedulerServiceLike {\n if (!schedulerService) {\n throw new Error('Scheduler module is not available')\n }\n return schedulerService\n }\n\n function buildScheduleName(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Data sync: ${row.integrationId} ${row.entityType} ${row.direction}`\n }\n\n function buildScheduleDescription(row: { integrationId: string; entityType: string; direction: 'import' | 'export' }): string {\n return `Scheduled ${row.direction} for ${row.integrationId} (${row.entityType})`\n }\n\n async function getById(id: string, scope: SyncScope): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n async function getByKey(\n integrationId: string,\n entityType: string,\n direction: 'import' | 'export',\n scope: SyncScope,\n ): Promise<SyncSchedule | null> {\n return findOneWithDecryption(\n em,\n SyncSchedule,\n {\n integrationId,\n entityType,\n direction,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n },\n undefined,\n scope,\n )\n }\n\n return {\n getById,\n getByKey,\n\n async listSchedules(query: {\n integrationId?: string\n entityType?: string\n direction?: 'import' | 'export'\n page: number\n pageSize: number\n }, scope: SyncScope): Promise<{ items: SyncSchedule[]; total: number }> {\n const where: FilterQuery<SyncSchedule> = {\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n deletedAt: null,\n }\n\n if (query.integrationId) where.integrationId = query.integrationId\n if (query.entityType) where.entityType = query.entityType\n if (query.direction) where.direction = query.direction\n\n const [items, total] = await findAndCountWithDecryption(\n em,\n SyncSchedule,\n where,\n {\n orderBy: { createdAt: 'DESC' },\n limit: query.pageSize,\n offset: (query.page - 1) * query.pageSize,\n },\n scope,\n )\n\n return { items, total }\n },\n\n async saveSchedule(input: {\n id?: string\n integrationId: string\n entityType: string\n direction: 'import' | 'export'\n scheduleType: 'cron' | 'interval'\n scheduleValue: string\n timezone: string\n fullSync: boolean\n isEnabled: boolean\n expectedUpdatedAt?: string | null\n }, scope: SyncScope, container?: AwilixContainer): Promise<SyncSchedule> {\n const existing = input.id\n ? await getById(input.id, scope)\n : await getByKey(input.integrationId, input.entityType, input.direction, scope)\n\n if (existing && container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: existing.id,\n current: existing.updatedAt ?? null,\n expected: input.expectedUpdatedAt ?? null,\n })\n } else if (!existing && input.expectedUpdatedAt) {\n // Concurrent delete-then-edit race: the client edited a schedule that was\n // removed before this keyed upsert ran. Surface the unified conflict\n // instead of silently re-creating it. No-op when no expected version was\n // sent (a genuine create) or when OM_OPTIMISTIC_LOCK is off.\n enforceRecordGoneIsConflict({\n resourceKind: 'data_sync.schedule',\n resourceId: input.id ?? `${input.integrationId}:${input.entityType}:${input.direction}`,\n expected: input.expectedUpdatedAt,\n })\n }\n\n const id = existing?.id ?? randomUUID()\n const scheduledJobId = existing?.scheduledJobId ?? id\n\n // Validate the schedule (and register it with the scheduler) before writing\n // the SyncSchedule row \u2014 an unparseable scheduleValue must not leave a\n // persisted row with no working schedule behind it.\n await requireScheduler().register({\n id: scheduledJobId,\n name: buildScheduleName(input),\n description: buildScheduleDescription(input),\n scopeType: 'organization',\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n targetType: 'queue',\n targetQueue: 'data-sync-scheduled',\n targetPayload: {\n scheduleId: id,\n scope,\n },\n requireFeature: 'data_sync.run',\n sourceType: 'module',\n sourceModule: 'data_sync',\n isEnabled: input.isEnabled,\n })\n\n const row = existing ?? em.create(SyncSchedule, {\n id,\n integrationId: input.integrationId,\n entityType: input.entityType,\n direction: input.direction,\n scheduleType: input.scheduleType,\n scheduleValue: input.scheduleValue,\n timezone: input.timezone,\n fullSync: input.fullSync,\n isEnabled: input.isEnabled,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n })\n\n row.integrationId = input.integrationId\n row.entityType = input.entityType\n row.direction = input.direction\n row.scheduleType = input.scheduleType\n row.scheduleValue = input.scheduleValue\n row.timezone = input.timezone\n row.fullSync = input.fullSync\n row.isEnabled = input.isEnabled\n row.scheduledJobId = scheduledJobId\n\n if (!existing) {\n em.persist(row)\n }\n\n await em.flush()\n\n return row\n },\n\n async deleteSchedule(\n id: string,\n scope: SyncScope,\n container?: AwilixContainer,\n expectedUpdatedAt?: string | null,\n ): Promise<boolean> {\n const row = await getById(id, scope)\n if (!row) return false\n\n if (container) {\n await enforceCommandOptimisticLockWithGuards(container, {\n resourceKind: 'data_sync.schedule',\n resourceId: row.id,\n current: row.updatedAt ?? null,\n expected: expectedUpdatedAt ?? null,\n })\n }\n\n const scheduledJobId = row.scheduledJobId ?? row.id\n const wasEnabled = row.isEnabled\n await requireScheduler().unregister(scheduledJobId)\n\n try {\n row.deletedAt = new Date()\n row.isEnabled = false\n await em.flush()\n } catch (error) {\n try {\n await requireScheduler().register({\n id: scheduledJobId,\n name: buildScheduleName(row),\n description: buildScheduleDescription(row),\n scopeType: 'organization',\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n scheduleType: row.scheduleType,\n scheduleValue: row.scheduleValue,\n timezone: row.timezone,\n targetType: 'queue',\n targetQueue: 'data-sync-scheduled',\n targetPayload: {\n scheduleId: row.id,\n scope,\n },\n requireFeature: 'data_sync.run',\n sourceType: 'module',\n sourceModule: 'data_sync',\n isEnabled: wasEnabled,\n })\n } catch (compensationError) {\n logger.error('Failed to restore scheduled job after a failed schedule delete', {\n scheduledJobId,\n scheduleId: row.id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n error: compensationError instanceof Error ? compensationError.message : String(compensationError),\n })\n getTelemetryRuntime()?.reportError(compensationError, {\n module: 'data_sync',\n code: 'data_sync.schedule_delete_compensation_failed',\n attributes: {\n scheduledJobId,\n scheduleId: row.id,\n organizationId: scope.organizationId,\n tenantId: scope.tenantId,\n },\n })\n }\n throw error\n }\n\n return true\n },\n }\n}\n\nexport type SyncScheduleService = ReturnType<typeof createSyncScheduleService>\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,kBAAkB;AAG3B,SAAS,4BAA4B,6BAA6B;AAClE,SAAS,wCAAwC,mCAAmC;AACpF,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AACpC,SAAS,oBAAoB;AAE7B,MAAM,SAAS,aAAa,WAAW,EAAE,MAAM,EAAE,WAAW,wBAAwB,CAAC;AA6B9E,SAAS,0BAA0B,IAAmB,kBAAyC;AACpG,WAA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AAW;AAAA,YACX,gBAAgB,MAAM;AAAA,YACtB,UAAU,MAAM;AAAA,YAChB,cAAc,IAAI;AAAA,YAClB,eAAe,IAAI;AAAA,YACnB,UAAU,IAAI;AAAA,YACd,YAAY;AAAA,YACZ,aAAa;AAAA,YACb,eAAe;AAAA,cACb,YAAY,IAAI;AAAA,cAChB;AAAA,YACF;AAAA,YACA,gBAAgB;AAAA,YAChB,YAAY;AAAA,YACZ,cAAc;AAAA,YACd,WAAW;AAAA,UACb,CAAC;AAAA,QACH,SAAS,mBAAmB;AAC1B,iBAAO,MAAM,kEAAkE;AAAA,YAC7E;AAAA,YACA,YAAY,IAAI;AAAA,YAChB,gBAAgB,MAAM;AAAA,YACtB,UAAU,MAAM;AAAA,YAChB,OAAO,6BAA6B,QAAQ,kBAAkB,UAAU,OAAO,iBAAiB;AAAA,UAClG,CAAC;AACD,8BAAoB,GAAG,YAAY,mBAAmB;AAAA,YACpD,QAAQ;AAAA,YACR,MAAM;AAAA,YACN,YAAY;AAAA,cACV;AAAA,cACA,YAAY,IAAI;AAAA,cAChB,gBAAgB,MAAM;AAAA,cACtB,UAAU,MAAM;AAAA,YAClB;AAAA,UACF,CAAC;AAAA,QACH;AACA,cAAM;AAAA,MACR;AAEA,aAAO;AAAA,IACT;AAAA,EACF;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -483,7 +483,7 @@ function createProgressService(em, eventBus) {
|
|
|
483
483
|
return job;
|
|
484
484
|
},
|
|
485
485
|
async isCancellationRequested(jobId, tenantId, organizationId) {
|
|
486
|
-
const job = await findOneWithDecryption(em, ProgressJob, {
|
|
486
|
+
const job = await findOneWithDecryption(em.fork(), ProgressJob, {
|
|
487
487
|
id: jobId,
|
|
488
488
|
tenantId,
|
|
489
489
|
...organizationId ? { organizationId } : {}
|