@@ -4,7 +4,7 @@ import { formatConfigIssueLines } from "../../config/issue-format.js";
44import { resolveIsNixMode } from "../../config/paths.js" ;
55import { checkTokenDrift } from "../../daemon/service-audit.js" ;
66import type { GatewayServiceRestartResult } from "../../daemon/service-types.js" ;
7- import { describeGatewayServiceRestart } from "../../daemon/service.js" ;
7+ import { describeGatewayServiceRestart , startGatewayService } from "../../daemon/service.js" ;
88import type { GatewayService } from "../../daemon/service.js" ;
99import { renderSystemdUnavailableHints } from "../../daemon/systemd-hints.js" ;
1010import { isSystemdUserServiceAvailable } from "../../daemon/systemd.js" ;
@@ -77,6 +77,17 @@ function createActionIO(params: { action: DaemonAction; json: boolean }) {
7777 return { stdout, emit, fail } ;
7878}
7979
80+ function emitActionMessage ( params : {
81+ json : boolean ;
82+ emit : ReturnType < typeof createActionIO > [ "emit" ] ;
83+ payload : Omit < DaemonActionResponse , "action" > ;
84+ } ) {
85+ params . emit ( params . payload ) ;
86+ if ( ! params . json && params . payload . message ) {
87+ defaultRuntime . log ( params . payload . message ) ;
88+ }
89+ }
90+
8091async function handleServiceNotLoaded ( params : {
8192 serviceNoun : string ;
8293 service : GatewayService ;
@@ -200,12 +211,13 @@ export async function runServiceStart(params: {
200211 const json = Boolean ( params . opts ?. json ) ;
201212 const { stdout, emit, fail } = createActionIO ( { action : "start" , json } ) ;
202213
203- const loaded = await resolveServiceLoadedOrFail ( {
204- serviceNoun : params . serviceNoun ,
205- service : params . service ,
206- fail,
207- } ) ;
208- if ( loaded === null ) {
214+ if (
215+ ( await resolveServiceLoadedOrFail ( {
216+ serviceNoun : params . serviceNoun ,
217+ service : params . service ,
218+ fail,
219+ } ) ) === null
220+ ) {
209221 return ;
210222 }
211223 // Pre-flight config validation (#35862) — run for both loaded and not-loaded
@@ -219,71 +231,45 @@ export async function runServiceStart(params: {
219231 return ;
220232 }
221233 }
222- if ( ! loaded ) {
223- // Service was stopped (e.g. `gateway stop` booted out the LaunchAgent).
224- // Attempt a restart, which handles re-bootstrapping the service. Without
225- // this, `start` after `stop` just prints hints and does nothing (#53878).
226- try {
227- const restartResult = await params . service . restart ( { env : process . env , stdout } ) ;
228- const restartStatus = describeGatewayServiceRestart ( params . serviceNoun , restartResult ) ;
229- const postLoaded = await params . service . isLoaded ( { env : process . env } ) . catch ( ( ) => true ) ;
230- emit ( {
231- ok : true ,
232- result : restartStatus . daemonActionResult ,
233- message : restartStatus . message ,
234- service : buildDaemonServiceSnapshot ( params . service , postLoaded ) ,
235- } ) ;
236- if ( ! json ) {
237- defaultRuntime . log ( restartStatus . message ) ;
238- }
239- return ;
240- } catch {
241- // Bootstrap failed (e.g. plist was deleted, not just booted out).
242- // Fall through to the not-loaded hints.
234+ try {
235+ const startResult = await startGatewayService ( params . service , { env : process . env , stdout } ) ;
236+ if ( startResult . outcome === "missing-install" ) {
243237 await handleServiceNotLoaded ( {
244238 serviceNoun : params . serviceNoun ,
245239 service : params . service ,
246- loaded,
240+ loaded : startResult . state . loaded ,
247241 renderStartHints : params . renderStartHints ,
248242 json,
249243 emit,
250244 } ) ;
251245 return ;
252246 }
253- }
254-
255- try {
256- const restartResult = await params . service . restart ( { env : process . env , stdout } ) ;
257- const restartStatus = describeGatewayServiceRestart ( params . serviceNoun , restartResult ) ;
258- if ( restartStatus . scheduled ) {
259- emit ( {
260- ok : true ,
261- result : restartStatus . daemonActionResult ,
262- message : restartStatus . message ,
263- service : buildDaemonServiceSnapshot ( params . service , loaded ) ,
247+ if ( startResult . outcome === "scheduled" ) {
248+ const restartStatus = describeGatewayServiceRestart ( params . serviceNoun , {
249+ outcome : "scheduled" ,
250+ } ) ;
251+ emitActionMessage ( {
252+ json,
253+ emit,
254+ payload : {
255+ ok : true ,
256+ result : "scheduled" ,
257+ message : restartStatus . message ,
258+ service : buildDaemonServiceSnapshot ( params . service , startResult . state . loaded ) ,
259+ } ,
264260 } ) ;
265- if ( ! json ) {
266- defaultRuntime . log ( restartStatus . message ) ;
267- }
268261 return ;
269262 }
263+ emit ( {
264+ ok : true ,
265+ result : "started" ,
266+ service : buildDaemonServiceSnapshot ( params . service , startResult . state . loaded ) ,
267+ } ) ;
270268 } catch ( err ) {
271269 const hints = params . renderStartHints ( ) ;
272270 fail ( `${ params . serviceNoun } start failed: ${ String ( err ) } ` , hints ) ;
273271 return ;
274272 }
275-
276- let started = true ;
277- try {
278- started = await params . service . isLoaded ( { env : process . env } ) ;
279- } catch {
280- started = true ;
281- }
282- emit ( {
283- ok : true ,
284- result : "started" ,
285- service : buildDaemonServiceSnapshot ( params . service , started ) ,
286- } ) ;
287273}
288274
289275export async function runServiceStop ( params : {
@@ -371,16 +357,17 @@ export async function runServiceRestart(params: {
371357 restartStatus : ReturnType < typeof describeGatewayServiceRestart > ,
372358 serviceLoaded : boolean ,
373359 ) => {
374- emit ( {
375- ok : true ,
376- result : restartStatus . daemonActionResult ,
377- message : restartStatus . message ,
378- service : buildDaemonServiceSnapshot ( params . service , serviceLoaded ) ,
379- warnings : warnings . length ? warnings : undefined ,
360+ emitActionMessage ( {
361+ json,
362+ emit,
363+ payload : {
364+ ok : true ,
365+ result : restartStatus . daemonActionResult ,
366+ message : restartStatus . message ,
367+ service : buildDaemonServiceSnapshot ( params . service , serviceLoaded ) ,
368+ warnings : warnings . length ? warnings : undefined ,
369+ } ,
380370 } ) ;
381- if ( ! json ) {
382- defaultRuntime . log ( restartStatus . message ) ;
383- }
384371 return true ;
385372 } ;
386373
0 commit comments