trails/apps/journal/app/lib/boss.server.ts
Ullrich Schäfer 9c6407423a
fix(journal): remove ineffective dynamic imports
Rollup was warning on 5 modules that were both dynamically and statically
imported. With static importers in the same chunk, the dynamic forms
buy no chunking benefit — they were leftovers from earlier
cycle-avoidance workarounds that no longer apply.

Converted to static:
- @trails-cool/gpx (routes.\$id.server.ts, sync.import.\$provider.server.ts)
- logger.server (boss.server.ts — comment claimed test cycles, but tests pass)
- boss.server (activities.server.ts at two sites)
- connected-services/manager (komoot/importer.ts, wahoo/importer.ts)
- notifications.server (root.tsx)

Kept dynamic: fit-file-parser in sync.import.\$provider.server.ts — it's
heavy and only the FIT ingestion path needs it, no other static
importers exist, so the dynamic actually does chunk-split it.

Build is now warning-free.

Full repo: pnpm typecheck, pnpm lint, pnpm test, pnpm build all green.
192 tests passed (up from 181 — rate-limit test from #424 + others).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 12:05:08 +02:00

50 lines
1.5 KiB
TypeScript

// Module-level pg-boss singleton. server.ts initializes the boss + starts
// the worker; feature code (e.g., activities.server.ts) calls `getBoss()`
// to enqueue jobs against the same instance. The singleton's lifecycle
// is bound to the Node process — startWorker calls boss.start(); the
// SIGTERM handler stops it.
import { logger } from "./logger.server.ts";
// Structurally typed (we only need `send`) so we don't have to pull
// pg-boss into the journal app's dep graph just for the typedef.
interface BossLike {
send(queueName: string, data: unknown): Promise<string | null>;
}
let _boss: BossLike | null = null;
/** Set by server.ts once the boss is created + started. */
export function setBoss(boss: BossLike): void {
_boss = boss;
}
/**
* Get the started pg-boss instance. Throws if called before
* server.ts has initialized it (i.e., outside a running Journal
* server context).
*/
export function getBoss(): BossLike {
if (!_boss) {
throw new Error("pg-boss not initialized — getBoss called before server bootstrap");
}
return _boss;
}
/**
* Best-effort enqueue: log + swallow errors so a downstream queue
* outage doesn't fail the user-visible request that triggered the
* fan-out. Use this for "fire and forget" notifications work.
*/
export async function enqueueOptional(
queue: string,
data: unknown,
ctx: Record<string, unknown> = {},
): Promise<void> {
try {
const boss = getBoss();
await boss.send(queue, data);
} catch (err) {
logger.warn({ err, queue, ...ctx }, "boss.send failed; continuing");
}
}