diff --git a/apps/journal/app/routes/sync.import.komoot.tsx b/apps/journal/app/routes/sync.import.komoot.tsx index 2512942..260c3dd 100644 --- a/apps/journal/app/routes/sync.import.komoot.tsx +++ b/apps/journal/app/routes/sync.import.komoot.tsx @@ -67,22 +67,35 @@ export async function action({ request }: Route.ActionArgs) { status: "pending", }); - // Try to enqueue via pg-boss (available in production / custom server). - // In dev (Vite), boss is never initialized — fall back to running inline - // in a detached promise so the response is still immediate. + const { runKomootBulkImport } = await import("~/lib/komoot-bulk-import.server"); + const creds = service.credentials as Parameters[2]; + + // Try to enqueue via pg-boss (production / custom server.ts). + // In dev (Vite), boss is never initialized — fall back to an inline + // fire-and-forget so the redirect still happens immediately. + let enqueued = false; try { const { getBoss } = await import("~/lib/boss.server"); const boss = getBoss(); - await boss.send("komoot-bulk-import", { - batchId, - userId: user.id, - creds: service.credentials, - }); + await boss.send("komoot-bulk-import", { batchId, userId: user.id, creds }); + enqueued = true; } catch { - const { runKomootBulkImport } = await import("~/lib/komoot-bulk-import.server"); - const creds = service.credentials as Parameters[2]; - // Fire-and-forget — don't await so the redirect happens immediately - void runKomootBulkImport(batchId, user.id, creds).catch(() => {}); + // pg-boss not available + } + + if (!enqueued) { + // Mark the batch running before returning so the UI doesn't stay frozen + // if the detached promise fails to start. + const { eq } = await import("drizzle-orm"); + await db.update(importBatches).set({ status: "running" }).where(eq(importBatches.id, batchId)); + void runKomootBulkImport(batchId, user.id, creds).catch(async (err) => { + const { eq: eq2 } = await import("drizzle-orm"); + await db.update(importBatches).set({ + status: "failed", + errorMessage: err instanceof Error ? err.message : String(err), + completedAt: new Date(), + }).where(eq2(importBatches.id, batchId)); + }); } return redirect("/sync/import/komoot");