Files
Brandon LiandClaude Opus 5.5 b932452e98 Self-healing broker clients, per-firm scheduler isolation, eager boot
Why the autotrader sometimes broke after a weekend until `pm2 restart`:
`next start` only logs uncaught errors, so nothing ever crashed, and a few
paths could leave a client dead for the life of the process.

Tradovate: a closed socket scheduled exactly one re-login, with no catch —
one failure during weekend maintenance (or a rate-limit penalty returned as
a 200 without a token) and the client never came back. connectAndAuth is now
a single-flight loop that never throws: jittered backoff capped at 5 min,
penalty tickets waited out, captcha demands backed off. A 15s watchdog
abandons silent, never-authorized or stuck-closing sockets and stale syncs;
renewal falls back to a fresh login; orders fail fast (OrderNotPlacedError)
on a dead socket and time out instead of hanging; a bad sync answer no
longer wipes accounts and positions.

TopstepX: hub start/stop/invoke and every REST call are bounded; a wedged
hub is discarded and rebuilt; a stuck sync is superseded after 3 min;
re-login no longer tears down the sync loop; a watchdog re-logs when syncs
go stale; a no-fill timeout cancels the order.

Scheduler: one unready client used to hold every firm forever. It now holds
the tick for a 10-minute grace, then trades the other firms without it.
Per-firm try/catch in batch collection, one batch at a time, accounts marked
in flight, legs un-counted only when the entry never reached the broker, and
the scheduler starts before the first batch fires.

Boot and supervision: instrumentation.ts boots the process at server start
(it used to wait for the first dashboard request); a supervisor rebuilds a
client that has been unhealthy for 20 min (not a TopstepX one holding a
position); broker HTTP calls default to a 60s timeout; GET /api/health shows
each client's liveness and the scheduler's last decision. Shared state the
eager boot would otherwise split across bundles lives on globalThis. Logs no
longer carry access tokens or whole AxiosErrors (which include the encoded
Tradovate password).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 20:18:33 -05:00

41 lines
2.1 KiB
TypeScript

/**
* Server start hook — Next runs register() once per server process, before
* it serves the first request (a root instrumentation.ts is picked up with no
* config in Next 15+).
*
* It boots the trading background work eagerly. getClients() in lib/clients.ts
* starts the broker clients, the contract resolver, the reporter, the master
* link, the client supervisor and the persisted-schedule resume — and it used
* to run only on the first HTTP request. So a restarted instance (a crash, an
* update, pm2 after a reboot) sat idle, not trading and invisible to the
* master, until someone opened the dashboard or start-all's warm-up curl
* happened to reach it.
*
* The boot must never take the server down with it: a failure is logged, and
* if the boot never got going the first request still starts it through the
* same lazy getClients() path as before.
*
* This module is its own server bundle, so it can hold its own instances of
* lib/ modules, separate from the route handlers'. Anything the two must share
* (the client pool, the scheduler, timers) lives on globalThis for that reason.
*/
export async function register(): Promise<void> {
// The nodejs check is the documented shape for a Node-only import, kept
// exactly: the edge compile (Turbopack always builds one) inlines
// NEXT_RUNTIME and drops the block, so lib/clients — sqlite, sockets —
// never reaches the edge bundle, where it would fail the build. The build
// check is defense in depth (Next itself doesn't call register() during
// `next build`) — a build must never dial brokers or resume the scheduler.
if (process.env.NEXT_RUNTIME === 'nodejs') {
if (process.env.NEXT_PHASE === 'phase-production-build') return;
try {
const { getClients } = await import('./lib/clients');
getClients();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.error(`[instrumentation] eager boot failed — clients will start on the first request: ${message}`);
}
}
}