fix(worker): break circular import by extracting PgBoss singleton
incremental-sync.ts and api/cw/sync.ts imported getBoss() from workert.ts. When workert.ts (the entry point) dynamically imported incremental-sync.ts, it triggered a circular module re-evaluation that hung indefinitely. Extract the PgBoss singleton and getBoss() factory to a new boss-instance.ts module that neither has top-level async side-effects nor imports from workert.ts. All consumers (workert.ts, index.ts, incremental-sync.ts, cw/sync.ts) now import from boss-instance.ts instead.
This commit is contained in:
+1
-31
@@ -1,29 +1,7 @@
|
||||
import { PgBoss } from "pg-boss";
|
||||
import { io, Socket } from "socket.io-client";
|
||||
import { WorkerQueue } from "./modules/workers/queues";
|
||||
import { setupEventDebugger } from "./modules/logging/eventDebugger";
|
||||
|
||||
// Add statement and connection timeouts to prevent silent hangs
|
||||
function makePgBossUrl(rawUrl: string): string {
|
||||
try {
|
||||
const u = new URL(rawUrl);
|
||||
// 30-second statement timeout and 15-second TCP connect timeout
|
||||
u.searchParams.set("options", "-c statement_timeout=30000");
|
||||
u.searchParams.set("connect_timeout", "15");
|
||||
return u.toString();
|
||||
} catch {
|
||||
return rawUrl;
|
||||
}
|
||||
}
|
||||
|
||||
const boss = new PgBoss({
|
||||
connectionString: makePgBossUrl(process.env.DATABASE_URL!),
|
||||
connectionTimeoutMillis: 15_000,
|
||||
});
|
||||
|
||||
boss.on("error", (err) => {
|
||||
console.error("[worker] PgBoss error", err);
|
||||
});
|
||||
import { boss, getBoss } from "./boss-instance";
|
||||
|
||||
let bossStartPromise: Promise<void> | null = null;
|
||||
let reservationQueueReady = false;
|
||||
@@ -160,14 +138,6 @@ export async function initializeWorkerSystem(): Promise<void> {
|
||||
console.log("[worker] Worker system initialized - ready for job enqueueing");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PgBoss instance for direct job enqueueing.
|
||||
* Must call initializeWorkerSystem() first.
|
||||
*/
|
||||
export function getBoss(): PgBoss {
|
||||
return boss;
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
// if (Bun.env.NODE_ENV === "development") {
|
||||
// setupEventDebugger({ processLabel: "WORKER" });
|
||||
|
||||
Reference in New Issue
Block a user