@@ -19,10 +19,10 @@ import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease";
1919import * as ProcessRunner from "../processRunner.ts" ;
2020import { acquireServiceSelectionLock } from "./serviceSelectionLock.ts" ;
2121import {
22- requestServiceLauncherStop ,
23- serviceControlPipePath ,
2422 serviceLauncherIsRunning ,
23+ serviceLauncherLockPath ,
2524 serviceTaskName ,
25+ waitForServiceLauncherExit ,
2626} from "../serviceLauncher.ts" ;
2727import {
2828 ensurePinnedRuntimeInstalled ,
@@ -38,6 +38,7 @@ import {
3838 SERVICE_STATE_FILE ,
3939 SERVICE_UNIT_ENV ,
4040 compareExactServiceVersions ,
41+ serviceTaskIsDisabled ,
4142 serviceUnitBaseDirOf as bootServiceBaseDirOf ,
4243 parseServiceState ,
4344 serviceStateActiveVersion ,
@@ -196,7 +197,7 @@ export function renderBootServiceTask(
196197 "--log-file" ,
197198 plan . logPath ,
198199 "--service-unit" ,
199- BOOT_SERVICE_UNIT_FILE ,
200+ BOOT_SERVICE_TASK_FILE ,
200201 ]
201202 . map ( quoteWindowsArgument )
202203 . join ( " " ) ;
@@ -252,7 +253,7 @@ export type BootServiceStep = {
252253 readonly timeout ?: Duration . Input ;
253254} & (
254255 | { readonly command : string ; readonly args : ReadonlyArray < string > }
255- /** A step no command can do, such as waiting on the Windows launcher's control pipe . */
256+ /** A step no command can do, such as waiting for the Windows launcher to exit . */
256257 | { readonly run : Effect . Effect < void , BootServiceCommandError > }
257258) ;
258259
@@ -279,6 +280,9 @@ const defaultSystemdUnitDir = (path: Path.Path, homeDir: string, xdgConfigHome:
279280 "user" ,
280281 ) ;
281282
283+ const scheduledTaskPath = ( path : Path . Path , localAppData : string ) =>
284+ path . join ( localAppData , "t3code" , BOOT_SERVICE_TASK_FILE ) ;
285+
282286const launchdPlistPath = ( path : Path . Path , homeDir : string ) =>
283287 path . join ( homeDir , "Library" , "LaunchAgents" , BOOT_SERVICE_PLIST_FILE ) ;
284288
@@ -427,10 +431,10 @@ function schtasksManager(input: {
427431 readonly path : Path . Path ;
428432 readonly homeDir : string ;
429433 readonly session : WindowsSession ;
430- /** Asks the installed launcher to stop and waits for it ; Task Scheduler cannot . */
434+ /** Ends the task and waits for its launcher to exit ; Task Scheduler does not wait . */
431435 readonly stopLauncher : Effect . Effect < void , BootServiceCommandError > ;
432436} ) : BootServiceManager {
433- const unitPath = input . path . join ( input . session . localAppData , "t3code" , BOOT_SERVICE_TASK_FILE ) ;
437+ const unitPath = scheduledTaskPath ( input . path , input . session . localAppData ) ;
434438 const task = [ "/TN" , input . session . taskName ] ;
435439 const register = {
436440 step : "registering the scheduled task" ,
@@ -469,7 +473,7 @@ function schtasksManager(input: {
469473 { ...run , step : "restarting the service after a failed update" } ,
470474 ] ,
471475 // Ending the task kills only its console host; the launcher sees the console close and
472- // stops gracefully while the replacement waits for its control pipe .
476+ // stops gracefully while the replacement waits for its lock .
473477 handoff : [
474478 register ,
475479 {
@@ -572,6 +576,10 @@ export const bootServiceUnitPath = Effect.fn("cloud.boot_service.unit_path")(fun
572576 const homeDir = yield * HostProcess . HomeDirectory ;
573577 if ( homeDir === "" ) return undefined ;
574578 if ( platform === "darwin" ) return launchdPlistPath ( path , homeDir ) ;
579+ if ( platform === "win32" ) {
580+ const localAppData = ( yield * HostProcess . Environment ) . LOCALAPPDATA ?? "" ;
581+ return localAppData === "" ? undefined : scheduledTaskPath ( path , localAppData ) ;
582+ }
575583 if ( platform !== "linux" ) return undefined ;
576584 const unitDir =
577585 ( yield * readSystemdUnitDirectory ( yield * ProcessRunner . ProcessRunner , path ) ) ??
@@ -770,9 +778,9 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
770778 // Root under sudo must not reach or start a separate root service.
771779 const rootUnderSudo =
772780 uid === 0 && ( yield * Config . String ( "SUDO_USER" ) . pipe ( Config . withDefault ( "" ) ) ) !== "" ;
773- const runningInsideService =
774- ( yield * Config . String ( SERVICE_UNIT_ENV ) . pipe ( Config . withDefault ( "" ) ) ) ===
775- BOOT_SERVICE_UNIT_FILE ;
781+ const runningInsideService = [ BOOT_SERVICE_UNIT_FILE , BOOT_SERVICE_TASK_FILE ] . includes (
782+ yield * Config . String ( SERVICE_UNIT_ENV ) . pipe ( Config . withDefault ( "" ) ) ,
783+ ) ;
776784 const fs = yield * FileSystem . FileSystem ;
777785 const path = yield * Path . Path ;
778786 const processRunner = yield * ProcessRunner . ProcessRunner ;
@@ -831,21 +839,22 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
831839 systemRoot : ( yield * windowsVariable ( "SystemRoot" ) ) || "C:\\Windows" ,
832840 } ;
833841 // The unit is per user, so the launcher to stop is the one serving the installed unit's home.
834- const launcherControlPath = ( unit : Option . Option < string > ) =>
835- serviceControlPipePath (
842+ const launcherLockPath = ( unit : Option . Option < string > ) =>
843+ serviceLauncherLockPath (
836844 Option . getOrUndefined ( Option . flatMapNullishOr ( unit , bootServiceBaseDirOf ) ) ?? input . baseDir ,
837845 ) ;
846+ // Ending the task closes the launcher's console, which the launcher takes as its SIGTERM.
847+ // Task Scheduler then reports the task stopped at once, so the lock says when it really is.
838848 const stopWindowsLauncher = Effect . gen ( function * ( ) {
839849 const unit = yield * fs . readFileString ( unitPath ) . pipe ( Effect . option ) ;
850+ yield * runner
851+ . run ( { command : "schtasks" , args : [ "/End" , "/TN" , windowsSession . taskName ] } )
852+ . pipe ( Effect . ignore ) ;
840853 yield * Effect . tryPromise ( {
841- try : ( ) => requestServiceLauncherStop ( launcherControlPath ( unit ) ) ,
854+ try : ( signal ) => waitForServiceLauncherExit ( launcherLockPath ( unit ) , signal ) ,
842855 catch : ( cause ) =>
843856 new BootServiceCommandError ( { step : "stopping the installed service" , cause } ) ,
844857 } ) ;
845- // Agents the server left behind keep its console host, and so the task, running.
846- yield * runner
847- . run ( { command : "schtasks" , args : [ "/End" , "/TN" , windowsSession . taskName ] } )
848- . pipe ( Effect . ignore ) ;
849858 } ) ;
850859 const logPath = path . join ( input . logsDir , BOOT_SERVICE_LOG_FILE ) ;
851860 const resolveTarget = ( systemdUnitDir : string | undefined ) => {
@@ -1049,23 +1058,20 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
10491058 ( release ) => Effect . sync ( release ) ,
10501059 ) ;
10511060
1052- // Running means the launcher answers its control pipe : a task can stay running after the
1061+ // Running means the launcher holds its lock : a task can stay running after the
10531062 // launcher exits, while agents it left behind hold the console host open.
10541063 const readSchtasksProblems = Effect . fn ( "cloud.boot_service.read_schtasks_problems" ) ( function * (
10551064 unit : Option . Option < string > ,
10561065 ) {
10571066 const [ task , running ] = yield * Effect . all (
10581067 [
10591068 probe ( "schtasks" , [ "/Query" , "/TN" , windowsSession . taskName , "/XML" ] ) ,
1060- Effect . promise ( ( ) => serviceLauncherIsRunning ( launcherControlPath ( unit ) ) ) ,
1069+ Effect . promise ( ( ) => serviceLauncherIsRunning ( launcherLockPath ( unit ) ) ) ,
10611070 ] ,
10621071 { concurrency : "unbounded" } ,
10631072 ) ;
1064- const settings = Option . isSome ( task )
1065- ? ( / < S e t t i n g s > [ \s \S ] * ?< \/ S e t t i n g s > / . exec ( task . value . stdout ) ?. [ 0 ] ?? "" )
1066- : "" ;
10671073 const problems : BootServiceProblem [ ] = [ ] ;
1068- if ( ! succeeded ( task ) || settings . includes ( "<Enabled>false</Enabled>" ) ) {
1074+ if ( Option . isNone ( task ) || task . value . code !== 0 || serviceTaskIsDisabled ( task . value . stdout ) ) {
10691075 problems . push ( "service-disabled" ) ;
10701076 }
10711077 if ( ! running ) problems . push ( "service-stopped" ) ;
@@ -1206,11 +1212,17 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
12061212 if ( Option . isSome ( previousStateText ) ) {
12071213 const installedVersion = serviceStateActiveVersion ( previousStateText . value ) ;
12081214 if ( serviceStateHasPendingUpdate ( previousStateText . value ) ) {
1209- const quote = ( word : string ) => `'${ word . replaceAll ( "'" , "'\\''" ) } '` ;
1215+ const windows = platform === "win32" ;
1216+ const quote = ( word : string ) => `'${ word . replaceAll ( "'" , windows ? "''" : "'\\''" ) } '` ;
1217+ const launcher = ( version : string ) =>
1218+ quote ( pinnedRuntimePaths ( path , input . baseDir , version , platform ) . entryPath ) ;
12101219 return yield * new BootServiceUpdatePendingError (
12111220 ! currentlyInstalled && installedVersion !== undefined
12121221 ? {
1213- recoveryCommand : `T3CODE_HOME=${ quote ( input . baseDir ) } ${ quote ( pinnedRuntimePaths ( path , input . baseDir , installedVersion , platform ) . entryPath ) } __service-launcher` ,
1222+ // PowerShell has no inline variable assignment; the launcher takes a flag.
1223+ recoveryCommand : windows
1224+ ? `& ${ launcher ( installedVersion ) } __service-launcher --base-dir ${ quote ( input . baseDir ) } `
1225+ : `T3CODE_HOME=${ quote ( input . baseDir ) } ${ launcher ( installedVersion ) } __service-launcher` ,
12141226 }
12151227 : { } ,
12161228 ) ;
@@ -1345,7 +1357,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
13451357 ( [ enabled , active ] ) => succeeded ( active ) || succeeded ( enabled ) ,
13461358 )
13471359 : manager . kind === "schtasks"
1348- ? ! ( yield * readSchtasksProblems ( unit ) ) . includes ( "service-disabled" )
1360+ ? // A task disabled while its launcher runs was left that way by an interrupted stop.
1361+ yield * Effect . map (
1362+ readSchtasksProblems ( unit ) ,
1363+ ( problems ) =>
1364+ ! problems . includes ( "service-disabled" ) || ! problems . includes ( "service-stopped" ) ,
1365+ )
13491366 : succeeded (
13501367 yield * probe ( "launchctl" , [ "print" , `gui/${ uid } /${ BOOT_SERVICE_LAUNCHD_LABEL } ` ] ) ,
13511368 ) ;
0 commit comments