44 * @module
55 */
66
7- import {
8- callback ,
9- createRunner ,
10- createUnknownError ,
11- type MainTask ,
12- } from "@evolu/common" ;
7+ import { callback , createRunner , createUnknownError , type MainTask } from '@evolu/common'
138
149/**
1510 * Runs a main task with proper Node.js signal handling.
@@ -49,8 +44,8 @@ export const runMain =
4944 < D > ( deps : D ) =>
5045 ( main : MainTask < D > ) : void => {
5146 void ( async ( ) => {
52- await using run = createRunner ( deps ) ;
53- const console = run . deps . console . child ( " process" ) ;
47+ await using run = createRunner ( deps )
48+ const console = run . deps . console . child ( ' process' )
5449
5550 /**
5651 * "The correct use of 'uncaughtException' is to perform synchronous
@@ -63,38 +58,44 @@ export const runMain =
6358 * We log and initiate graceful shutdown.
6459 */
6560 const handleError = ( error : unknown ) : void => {
66- console . error ( createUnknownError ( error ) ) ;
61+ console . error ( createUnknownError ( error ) )
6762 // https://nodejs.org/api/process.html#processexitcode
68- process . exitCode = 1 ;
69- void run [ Symbol . asyncDispose ] ( ) ;
70- } ;
63+ process . exitCode = 1
64+ void run [ Symbol . asyncDispose ] ( )
65+ }
7166
72- process . on ( " uncaughtException" , handleError ) ;
73- process . on ( " unhandledRejection" , handleError ) ;
67+ process . on ( ' uncaughtException' , handleError )
68+ process . on ( ' unhandledRejection' , handleError )
7469
75- const _ = await run ( main ) ;
70+ const result = await run ( main )
7671 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
77- await using _stack = _ . ok ? ( _ . value ?? undefined ) : undefined ;
72+ const stack = result . ok ? ( result . value ?? undefined ) : undefined
7873
79- await run (
80- callback ( ( { ok } ) => {
81- // https://nodejs.org/api/process.html#signal-events
82- process . on ( "SIGINT" , ok ) ; // Ctrl-C (all platforms)
83- process . on ( "SIGTERM" , ok ) ; // OS/k8s/Docker termination (Unix)
84- process . on ( "SIGHUP" , ok ) ; // Console close (Windows), terminal disconnect (Unix)
74+ try {
75+ await run (
76+ callback ( ( { ok } ) => {
77+ // https://nodejs.org/api/process.html#signal-events
78+ process . on ( 'SIGINT' , ok ) // Ctrl-C (all platforms)
79+ process . on ( 'SIGTERM' , ok ) // OS/k8s/Docker termination (Unix)
80+ process . on ( 'SIGHUP' , ok ) // Console close (Windows), terminal disconnect (Unix)
8581
86- // TODO: Explain why it's important to use run.onAbort and not
87- // unregister sooner (cli shows gracefull shutdown was terminated.)
88- run . onAbort ( ( ) => {
89- process . off ( "SIGINT" , ok ) ;
90- process . off ( "SIGTERM" , ok ) ;
91- process . off ( "SIGHUP" , ok ) ;
92- } ) ;
93- return undefined ;
94- } ) ,
95- ) ;
82+ // TODO: Explain why it's important to use run.onAbort and not
83+ // unregister sooner (cli shows gracefull shutdown was terminated.)
84+ run . onAbort ( ( ) => {
85+ process . off ( 'SIGINT' , ok )
86+ process . off ( 'SIGTERM' , ok )
87+ process . off ( 'SIGHUP' , ok )
88+ } )
89+ return undefined
90+ } ) ,
91+ )
92+ } finally {
93+ if ( stack ) {
94+ await stack [ Symbol . asyncDispose ] ( )
95+ }
96+ }
9697
97- process . off ( " uncaughtException" , handleError ) ;
98- process . off ( " unhandledRejection" , handleError ) ;
99- } ) ( ) ;
100- } ;
98+ process . off ( ' uncaughtException' , handleError )
99+ process . off ( ' unhandledRejection' , handleError )
100+ } ) ( )
101+ }
0 commit comments