Auto-start both processes at logon and self-update from master

Instances were started by hand and updated by hand, so they drifted behind
master silently. Now: PM2 supervises the dashboard and the clicker, a logon
task brings them up, and a 5-minute task pulls, rebuilds and restarts when
master moves.

Restarting on every push is only safe because the scheduler now survives it.
It was pure in-memory state (_global.__autoTrader), so any restart silently
stopped automated trading with the dashboard simply showing it as off. It now
mirrors running/action/symbol/stopAfterAll to the settings table, and
resumeSchedulerIfPersisted() picks it back up from the getClients() bootstrap.
No sync-wait was needed there: tick() already skips while a client reports
!syncComplete and while any account holds a position.

A failed build is never deployed — the build runs before anything restarts, so
a broken push leaves the previous build serving.

start-all and update-check both warm the app with a request afterwards. That is
load-bearing: getClients() is lazily bootstrapped, so until something makes an
HTTP request the Tradovate clients, the reporter and the resumed schedule never
start. That was already true of manual restarts.

Logic lives in Node so a macOS or Linux port only needs an equivalent of
install-autostart.ps1. Python deps are hash-guarded, so the common path is one
hash and one import with no network, and failure is non-fatal since only the
clicker needs them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-08-30 17:36:25 -05:00
co-authored by Claude Opus 5
parent 7e7bd985c2
commit 4288d8c298
11 changed files with 593 additions and 44 deletions
+27 -1
View File
@@ -10,7 +10,7 @@
import { getFirms, isSymbolBanned, getInstruments, getBannedSymbols } from './db';
import { getClients } from './clients';
import { computeDailyTarget, resolveEffectiveConfig, POINT_VALUES } from './trading-logic';
import { getSetting } from './db';
import { getSetting, setSetting } from './db';
import type { FirmConfig, AccountConfig } from '@/types';
import type { FirmWithAccounts } from './db';
@@ -623,6 +623,13 @@ export function startScheduler(action: 'Buy' | 'Sell' | 'Auto', symbol: string,
state.running = true;
state.stopAfterAll = stopAfterAll;
// Mirror to the settings table so a restart can pick the schedule back up.
// The interval itself is in-memory only; resumeSchedulerIfPersisted() recreates it.
setSetting('scheduler_running', '1');
setSetting('scheduler_action', action);
setSetting('scheduler_symbol', symbol);
setSetting('scheduler_stop_after_all', stopAfterAll ? '1' : '0');
const tick = async () => {
if (!state.running) return;
state.lastRun = new Date();
@@ -677,9 +684,28 @@ export function stopScheduler() {
state.intervalId = null;
}
state.running = false;
setSetting('scheduler_running', '0');
console.log('[scheduler] stopped');
}
/**
* Restart the schedule that was running before the process went down.
*
* No sync-wait here on purpose: tick() already skips while any client reports
* !syncComplete, and again while any account holds an open position. So the
* worst case is a few logged no-op ticks until the clients finish syncing.
*/
export function resumeSchedulerIfPersisted(): void {
if (getSetting('scheduler_running') !== '1') return;
const action = (getSetting('scheduler_action') ?? 'Buy') as 'Buy' | 'Sell' | 'Auto';
const symbol = getSetting('scheduler_symbol') ?? 'NQ';
const stopAfterAll = getSetting('scheduler_stop_after_all') === '1';
console.log(`[scheduler] resuming persisted schedule — ${action} ${symbol}`);
startScheduler(action, symbol, stopAfterAll);
}
export function getSchedulerStatus() {
const state = getState();
return {
+4
View File
@@ -2,6 +2,7 @@ import { TradovateClient } from './tradovate-class';
import { getFirms, getInstruments } from './db';
import { resolveContracts } from './contract-resolver';
import { startReporter } from './reporter';
import { resumeSchedulerIfPersisted } from './auto-trade';
// Use global to persist the client pool across HMR reloads in dev mode
const g = global as typeof globalThis & {
@@ -63,6 +64,9 @@ export function getClients(): Map<number, TradovateClient> {
// Start master dashboard reporter
startReporter();
// Pick the auto-trade schedule back up if it was running before restart
resumeSchedulerIfPersisted();
} catch (err) {
console.error('[clients] Failed to initialize clients', err);
}
+5
View File
@@ -185,6 +185,11 @@ seedSetting.run('tick_interval_seconds', '60');
seedSetting.run('master_dashboard_url', 'https://master.juicerroom.com');
seedSetting.run('instance_name', '');
seedSetting.run('trading_hours', 'full_cme');
// Mirrors the in-memory scheduler state so a restart can resume it.
seedSetting.run('scheduler_running', '0');
seedSetting.run('scheduler_action', 'Buy');
seedSetting.run('scheduler_symbol', 'NQ');
seedSetting.run('scheduler_stop_after_all', '0');
export function getSetting(key: string): string | null {
const row = db.prepare('SELECT value FROM settings WHERE key = ?').get(key) as { value: string } | undefined;