66 ServerSelfUpdateError ,
77 type ServerSelfUpdateCapability ,
88 type ServerSelfUpdateInput ,
9+ type ServerSelfUpdateProgressStage ,
910 type ServerSelfUpdateResult ,
1011} from "@t3tools/contracts" ;
1112import {
@@ -147,6 +148,7 @@ export class ServerSelfUpdate extends Context.Service<
147148 {
148149 readonly update : (
149150 input : ServerSelfUpdateInput ,
151+ reportProgress ?: ( stage : ServerSelfUpdateProgressStage ) => Effect . Effect < void , never , never > ,
150152 ) => Effect . Effect < ServerSelfUpdateResult , ServerSelfUpdateError > ;
151153 }
152154> ( ) ( "t3/cloud/selfUpdate/ServerSelfUpdate" ) { }
@@ -227,7 +229,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
227229
228230 const update : ServerSelfUpdate [ "Service" ] [ "update" ] = Effect . fn (
229231 "cloud.server_self_update.update" ,
230- ) ( function * ( input ) {
232+ ) ( function * ( input , reportProgress = ( ) => Effect . void ) {
231233 if ( capability === "desktop-managed" ) {
232234 return yield * failWith (
233235 "This server is managed by the T3 Code desktop app on its machine; update the desktop app to update it." ,
@@ -250,6 +252,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
250252 }
251253
252254 return yield * Effect . gen ( function * ( ) {
255+ yield * reportProgress ( "downloading" ) ;
253256 const runtimePaths = yield * ensurePinnedRuntimeInstalled ( {
254257 baseDir : serverConfig . baseDir ,
255258 version : targetVersion ,
@@ -260,6 +263,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
260263 Effect . mapError ( ( error ) => failWith ( "Could not install the requested t3 version." , error ) ) ,
261264 ) ;
262265
266+ yield * reportProgress ( "installing" ) ;
263267 // A broken artifact (failed native build, incompatible node) must be
264268 // caught while the current server is still alive to report it.
265269 const preflight = yield * runner
@@ -349,38 +353,45 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* (option
349353 yield * Effect . logInfo ( "Server self-update installed; restarting boot service." , {
350354 targetVersion,
351355 } ) ;
352- // A successful systemd restart stops this process, so the RPC is
353- // interrupted and the reconnecting client observes the new version.
354- // A rejected restart returns while the old process is still alive;
355- // restore the previous unit and report that failure through the RPC.
356- yield * Effect . gen ( function * ( ) {
357- const restart = yield * runner
358- . run ( {
359- command : "systemctl" ,
360- args : [ "--user" , "restart" , BOOT_SERVICE_UNIT_FILE ] ,
361- } )
362- . pipe (
363- Effect . mapError ( ( cause ) =>
364- failWith ( "Could not restart the systemd boot service." , cause ) ,
356+ // Restart after the acknowledgement has had time to cross any relay
357+ // hop. If systemd rejects the handoff, restore the previous unit while
358+ // this process is still alive and log the failure for diagnostics.
359+ yield * scheduleRestart (
360+ Effect . gen ( function * ( ) {
361+ const restart = yield * runner
362+ . run ( {
363+ command : "systemctl" ,
364+ args : [ "--user" , "restart" , BOOT_SERVICE_UNIT_FILE ] ,
365+ } )
366+ . pipe (
367+ Effect . mapError ( ( cause ) =>
368+ failWith ( "Could not restart the systemd boot service." , cause ) ,
369+ ) ,
370+ ) ;
371+ if ( restart . code !== 0 ) {
372+ return yield * failWith (
373+ `Restarting the systemd boot service failed (exit code ${ String ( restart . code ) } ).` ,
374+ ) ;
375+ }
376+ } ) . pipe (
377+ Effect . catch ( ( restartError ) =>
378+ writeUnitAtomically ( unitPath , previousUnit ) . pipe (
379+ Effect . andThen ( reloadSystemd ( ) ) ,
380+ Effect . mapError ( ( rollbackError ) =>
381+ failWith ( "Could not restore the previous systemd unit." , {
382+ restartError,
383+ rollbackError,
384+ } ) ,
385+ ) ,
386+ Effect . andThen ( Effect . fail ( restartError ) ) ,
365387 ) ,
366- ) ;
367- if ( restart . code !== 0 ) {
368- return yield * failWith (
369- `Restarting the systemd boot service failed (exit code ${ String ( restart . code ) } ).` ,
370- ) ;
371- }
372- } ) . pipe (
373- Effect . catch ( ( restartError ) =>
374- writeUnitAtomically ( unitPath , previousUnit ) . pipe (
375- Effect . andThen ( reloadSystemd ( ) ) ,
376- Effect . mapError ( ( rollbackError ) =>
377- failWith ( "Could not restore the previous systemd unit." , {
378- restartError,
379- rollbackError,
380- } ) ,
388+ ) ,
389+ Effect . catch ( ( error ) =>
390+ Effect . logError ( "Server self-update could not restart the boot service." ) . pipe (
391+ Effect . annotateLogs ( { targetVersion, error : error . reason } ) ,
381392 ) ,
382- Effect . andThen ( Effect . fail ( restartError ) ) ,
383393 ) ,
394+ Effect . ensuring ( Ref . set ( inFlight , false ) ) ,
384395 ) ,
385396 ) ;
386397 } else {
0 commit comments