1- /**
2- * Controller entry point — the control loops as their own process.
3- *
4- * Distributed mode only. Server replicas run no loops, so this deployment is the sole
5- * controller for the database. Loops assume no peer runs alongside them,
6- * so it MUST have exactly one replica.
7- *
8- * In standalone mode the controller runs inside the server instead — one process owns
9- * everything, and this entry point refuses to start.
10- */
11- import { extractErrorLogFields } from '@truefoundry/trueforge-core/core' ;
12-
13- let configuration : typeof import ( './config' ) . default ;
14-
15- try {
16- ( { default : configuration } = await import ( './config' ) ) ;
17- } catch ( error ) {
18- console . error (
19- 'Failed to start controller: Failed to load configuration:' ,
20- error instanceof Error ? error . message : error ,
21- ) ;
22- process . exit ( 1 ) ;
23- }
24-
1+ import { TrueForge } from '@truefoundry/trueforge-sdk' ;
2+ import type { Logger } from 'winston' ;
253import { Controller } from './controller/Controller' ;
26- import { createScheduleDispatchLoop , logTriggeredRun } from './controller/scheduleDispatch' ;
27- import { createDb } from './db/postgres/client' ;
28- import { PostgresScheduleStore } from './db/postgres/schedule-store/PostgresScheduleStore' ;
29- import { createServerLogger } from './logger' ;
30- import { PACKAGE_VERSION } from './packageVersion' ;
31-
32- try {
33- const logger = createServerLogger ( {
34- level : configuration . LOG_LEVEL ,
35- standalone : configuration . STANDALONE ,
36- version : PACKAGE_VERSION ,
37- } ) ;
4+ import { scheduleDispatchLoop } from './controller/scheduleDispatch' ;
5+ import type { IScheduleStore } from './db/scheduleStore' ;
6+ import type { WithTransaction } from './db/transaction' ;
387
39- // Narrow through a const: the config union is keyed on STANDALONE, and only the
40- // distributed variant carries the Postgres connection settings.
41- const config = configuration ;
42- if ( config . STANDALONE ) {
43- throw new Error (
44- 'The controller process is for distributed mode only. ' +
45- 'In standalone mode the server runs the control loops in-process.' ,
46- ) ;
47- }
48-
49- const db = createDb ( {
50- connectionString : config . DATABASE_URL ,
51- poolMax : config . DATABASE_POOL_MAX ,
52- statementTimeoutMs : config . POSTGRES_STATEMENT_TIMEOUT_MS ,
53- idleInTransactionSessionTimeoutMs : config . POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS ,
54- } ) ;
55- const controller = new Controller ( {
8+ /**
9+ * The loops the controller runs.
10+ */
11+ export function createController < TTransaction > ( params : {
12+ scheduleStore : IScheduleStore < TTransaction > ;
13+ withTransaction : WithTransaction < TTransaction > ;
14+ logger : Logger ;
15+ baseUrl : string ;
16+ } ) : Controller {
17+ const { scheduleStore, withTransaction, logger, baseUrl } = params ;
18+ return new Controller ( {
5619 loops : [
57- createScheduleDispatchLoop ( {
58- scheduleStore : new PostgresScheduleStore ( db ) ,
59- withTransaction : callback => db . transaction ( ) . execute ( callback ) ,
60- onTriggered : logTriggeredRun ( logger ) ,
20+ scheduleDispatchLoop ( {
21+ scheduleStore,
22+ client : new TrueForge ( { baseUrl , auth : false } ) ,
23+ withTransaction ,
6124 logger,
6225 } ) ,
6326 ] ,
6427 logger,
6528 } ) ;
29+ }
6630
67- logger . info ( 'TrueForge controller starting' , { mode : 'distributed' } ) ;
31+ /**
32+ * Runs the controller: starts the loops and drains them on SIGTERM/SIGINT.
33+ */
34+ export function runController < TTransaction > ( params : {
35+ scheduleStore : IScheduleStore < TTransaction > ;
36+ withTransaction : WithTransaction < TTransaction > ;
37+ logger : Logger ;
38+ baseUrl : string ;
39+ gracefulTimeoutSeconds : number ;
40+ /** Releases what the caller opened for the loops, e.g. its database pool. */
41+ onStopped ?: ( ) => Promise < void > ;
42+ } ) : Controller {
43+ const { logger, gracefulTimeoutSeconds, onStopped } = params ;
44+ const controller = createController ( params ) ;
6845 controller . start ( ) ;
6946
7047 let shuttingDown = false ;
@@ -75,17 +52,14 @@ try {
7552 shuttingDown = true ;
7653 logger . info ( `Received ${ signal } , stopping control loops` ) ;
7754
78- // Start the deadline at the top of shutdown; unref so this timer alone cannot keep the process
79- // alive. Passes only hold short transactions, so this should never elapse.
55+ // Passes only hold short transactions, so the deadline should never elapse.
8056 setTimeout ( ( ) => {
81- logger . warn ( `Controller drain timed out after ${ String ( configuration . GRACEFUL_TIMEOUT_SECONDS ) } s, exiting` ) ;
57+ logger . warn ( `Controller drain timed out after ${ String ( gracefulTimeoutSeconds ) } s, exiting` ) ;
8258 process . exit ( 1 ) ;
83- } , configuration . GRACEFUL_TIMEOUT_SECONDS * 1000 ) . unref ( ) ;
59+ } , gracefulTimeoutSeconds * 1000 ) . unref ( ) ;
8460
8561 await controller . stop ( ) ;
86- await db . destroy ( ) . catch ( ( error : unknown ) => {
87- logger . warn ( 'Error closing the database pool during shutdown' , extractErrorLogFields ( error ) ) ;
88- } ) ;
62+ await onStopped ?.( ) ;
8963 process . exit ( 0 ) ;
9064 } ;
9165 process . on ( 'SIGTERM' , signal => {
9468 process . on ( 'SIGINT' , signal => {
9569 void shutdown ( signal ) ;
9670 } ) ;
97- } catch ( error ) {
98- console . error ( 'Failed to start controller:' , error instanceof Error ? error . message : error ) ;
99- process . exit ( 1 ) ;
71+
72+ return controller ;
10073}
0 commit comments