import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("data_sync").child({ component: "sync-export" });
const metadata = {
  queue: "data-sync-export",
  id: "data-sync:export",
  concurrency: 5
};
async function handle(job, ctx) {
  try {
    const engine = ctx.resolve("dataSyncEngine");
    await engine.runExport(job.payload.runId, job.payload.batchSize, job.payload.scope);
  } catch (error) {
    const message = error instanceof Error ? error.message : "Data sync export worker failed";
    const errorStack = error instanceof Error ? error.stack : void 0;
    try {
      const syncRunService = ctx.resolve("dataSyncRunService");
      const progressService = ctx.resolve("progressService");
      const run = await syncRunService.getRun(job.payload.runId, job.payload.scope);
      if (run && run.status !== "completed" && run.status !== "failed" && run.status !== "cancelled") {
        await syncRunService.markStatus(run.id, "failed", job.payload.scope, message);
        if (run.progressJobId) {
          await progressService.failJob(
            run.progressJobId,
            {
              errorMessage: message,
              errorStack
            },
            job.payload.scope
          );
        }
      }
    } catch (finalizeError) {
      logger.error("Failed to finalize crashed export worker job", { err: finalizeError });
    }
    throw error;
  }
}
export {
  handle as default,
  metadata
};
//# sourceMappingURL=sync-export.js.map
