@@ -25,6 +25,7 @@ import {
2525 SERVICE_RESTART_PENDING_FILE ,
2626 SERVICE_STATE_FILE ,
2727 compareExactServiceVersions ,
28+ parseLegacyServiceState ,
2829 parseServiceState ,
2930 serviceStateActiveVersion ,
3031 serviceStateHasPendingUpdate ,
@@ -491,6 +492,15 @@ export class BootServiceUpdatePendingError extends Schema.TaggedError<BootServic
491492 }
492493}
493494
495+ export class BootServiceMigrationRequiredError extends Schema . TaggedError < BootServiceMigrationRequiredError > ( ) (
496+ "BootServiceMigrationRequiredError" ,
497+ { } ,
498+ ) {
499+ override get message ( ) : string {
500+ return "Cannot defer this install while the service state migration is pending. Start or restart the service first." ;
501+ }
502+ }
503+
494504export class BootServiceDowngradeRefusedError extends Schema . TaggedError < BootServiceDowngradeRefusedError > ( ) (
495505 "BootServiceDowngradeRefusedError" ,
496506 {
@@ -509,6 +519,7 @@ export type BootServiceError =
509519 | BootServiceInstallError
510520 | BootServicePrerequisiteError
511521 | BootServiceUpdatePendingError
522+ | BootServiceMigrationRequiredError
512523 | BootServiceDowngradeRefusedError ;
513524
514525export interface BootServiceStatus {
@@ -755,6 +766,22 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
755766 readonly start ?: boolean ;
756767 } ) {
757768 const manager = yield * requireManager ;
769+ const start = options ?. start !== false ;
770+ let deferredInstallUnitExists = false ;
771+ if ( ! start ) {
772+ deferredInstallUnitExists = yield * fs
773+ . exists ( unitPath )
774+ . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
775+ }
776+ // A deferred install leaves the launcher running, so it must not replace a
777+ // protocol-2 state while startup is validating and migrating that state.
778+ if ( deferredInstallUnitExists ) {
779+ const previousStateText = yield * fs . readFileString ( statePath ) . pipe ( Effect . option ) ;
780+ if ( Option . isSome ( previousStateText ) && parseLegacyServiceState ( previousStateText . value ) ) {
781+ return yield * new BootServiceMigrationRequiredError ( ) ;
782+ }
783+ }
784+
758785 yield * fs
759786 . makeDirectory ( input . logsDir , { recursive : true } )
760787 . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
@@ -827,7 +854,6 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
827854 // state file itself. That case is refused below, before anything is
828855 // written, from the same read the downgrade check uses; the stop that
829856 // normally serialises against the launcher is skipped on purpose.
830- const start = options ?. start !== false ;
831857 if ( installed && start ) {
832858 yield * runSteps ( manager . stop ) ;
833859 }
@@ -836,6 +862,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
836862 if ( installed ) {
837863 const previousStateText = yield * fs . readFileString ( statePath ) . pipe ( Effect . option ) ;
838864 if ( Option . isSome ( previousStateText ) ) {
865+ if ( ! start && parseLegacyServiceState ( previousStateText . value ) ) {
866+ return yield * new BootServiceMigrationRequiredError ( ) ;
867+ }
839868 if ( serviceStateHasPendingUpdate ( previousStateText . value ) ) {
840869 return yield * new BootServiceUpdatePendingError ( ) ;
841870 }
0 commit comments