@@ -39,9 +39,11 @@ import {
3939import { readLogFileSize , readLogTextSince } from "./logs.ts" ;
4040import {
4141 canConnectToLoopbackPort ,
42+ hasChildExited ,
4243 resolveCommandSpawnInvocation ,
4344 runCommand ,
4445 runCommandInvocation ,
46+ waitForGatewayWithStartupMigrationRestart ,
4547 withAllocatedGatewayPort ,
4648} from "./process.ts" ;
4749import { formatError , shellEscapeForSh , sleep } from "./shared.ts" ;
@@ -506,6 +508,7 @@ export async function startManualGatewayFromInstalledCli(params: {
506508 logPath : string ;
507509} ) : Promise < GatewayHandle > {
508510 mkdirSync ( dirname ( params . logPath ) , { recursive : true } ) ;
511+ const launchLogOffset = readLogFileSize ( params . logPath ) ;
509512 const gatewayLog = createWriteStream ( params . logPath , { flags : "a" } ) ;
510513 const invocation = resolveInstalledCliInvocation (
511514 params . cliPath ,
@@ -529,24 +532,33 @@ export async function startManualGatewayFromInstalledCli(params: {
529532 child . stderr ?. on ( "data" , ( chunk ) => {
530533 gatewayLog . write ( chunk ) ;
531534 } ) ;
532- let logClosed = false ;
533- const closeLog = async ( ) => {
534- if ( logClosed ) {
535- return ;
536- }
537- logClosed = true ;
538- await new Promise < void > ( ( resolvePromise ) => {
535+ let resolveChildClose : ( ) => void ;
536+ const childClosePromise = new Promise < void > ( ( resolvePromise ) => {
537+ resolveChildClose = resolvePromise ;
538+ } ) ;
539+ let closeLogPromise : Promise < void > | undefined ;
540+ const closeLog = ( ) => {
541+ closeLogPromise ??= new Promise < void > ( ( resolvePromise ) => {
539542 gatewayLog . once ( "error" , ( ) => resolvePromise ( ) ) ;
540543 gatewayLog . end ( ( ) => resolvePromise ( ) ) ;
541544 } ) ;
545+ return closeLogPromise ;
542546 } ;
543547 child . once ( "close" , ( ) => {
548+ resolveChildClose ( ) ;
544549 void closeLog ( ) ;
545550 } ) ;
546551 child . once ( "error" , ( ) => {
552+ resolveChildClose ( ) ;
547553 void closeLog ( ) ;
548554 } ) ;
549- return { child, closeLog, logPath : params . logPath } ;
555+ return {
556+ child,
557+ closeLog,
558+ launchLogOffset,
559+ logPath : params . logPath ,
560+ waitForClose : ( ) => childClosePromise ,
561+ } ;
550562}
551563
552564async function resolveInstalledGatewayStatusArgs ( params : {
@@ -609,8 +621,37 @@ export async function waitForInstalledGateway(params: {
609621 lane : LaneState ;
610622 cliPath : string ;
611623 env : NodeJS . ProcessEnv ;
624+ gateway ?: GatewayHandle ;
625+ gatewayHolder ?: { current : GatewayHandle | null } ;
626+ gatewayLogPath ?: string ;
612627 logPath : string ;
613628} ) {
629+ if ( params . gatewayHolder ) {
630+ if ( ! params . gatewayLogPath ) {
631+ throw new Error ( "Gateway restart coordination requires a gateway log path." ) ;
632+ }
633+ const gatewayLogPath = params . gatewayLogPath ;
634+ await waitForGatewayWithStartupMigrationRestart ( {
635+ gatewayHolder : params . gatewayHolder ,
636+ restartGateway : ( ) =>
637+ startManualGatewayFromInstalledCli ( {
638+ lane : params . lane ,
639+ cliPath : params . cliPath ,
640+ env : params . env ,
641+ logPath : gatewayLogPath ,
642+ } ) ,
643+ waitUntilReady : ( gateway ) =>
644+ waitForInstalledGateway ( {
645+ lane : params . lane ,
646+ cliPath : params . cliPath ,
647+ env : params . env ,
648+ gateway,
649+ logPath : params . logPath ,
650+ } ) ,
651+ } ) ;
652+ return ;
653+ }
654+
614655 const statusArgs = await resolveInstalledGatewayStatusArgs ( {
615656 cliPath : params . cliPath ,
616657 cwd : params . lane . homeDir ,
@@ -619,6 +660,9 @@ export async function waitForInstalledGateway(params: {
619660 } ) ;
620661 const deadline = Date . now ( ) + gatewayReadyDeadlineMs ( ) ;
621662 while ( Date . now ( ) < deadline ) {
663+ if ( params . gateway && hasChildExited ( params . gateway . child ) ) {
664+ throw new Error ( `Gateway exited before becoming ready on port ${ params . lane . gatewayPort } .` ) ;
665+ }
622666 const result = await runInstalledCli ( {
623667 cliPath : params . cliPath ,
624668 args : statusArgs ,
@@ -631,6 +675,9 @@ export async function waitForInstalledGateway(params: {
631675 if ( result . exitCode === 0 ) {
632676 return ;
633677 }
678+ if ( params . gateway && hasChildExited ( params . gateway . child ) ) {
679+ throw new Error ( `Gateway exited before becoming ready on port ${ params . lane . gatewayPort } .` ) ;
680+ }
634681 await sleep ( 2_000 ) ;
635682 }
636683 throw new Error ( `Gateway did not become ready on port ${ params . lane . gatewayPort } .` ) ;
0 commit comments