@@ -23,6 +23,7 @@ import {
2323 ServerConfig ,
2424 childMessagePm2Schema ,
2525 ManagerMessage ,
26+ ManagerMessagePayload ,
2627} from 'cli/lib/types/wordpress-server-ipc' ;
2728
2829function getProcessName ( siteId : string ) : string {
@@ -137,7 +138,8 @@ const messageActivityTrackers = new Map<
137138
138139async function sendMessage (
139140 pmId : number ,
140- message : Omit < ManagerMessage , 'messageId' >
141+ message : ManagerMessagePayload ,
142+ maxTotalElapsedTime = PLAYGROUND_CLI_INACTIVITY_TIMEOUT
141143) : Promise < unknown > {
142144 const bus = await getPm2Bus ( ) ;
143145
@@ -162,12 +164,12 @@ async function sendMessage(
162164
163165 if (
164166 timeSinceLastActivity > PLAYGROUND_CLI_INACTIVITY_TIMEOUT ||
165- totalElapsedTime > PLAYGROUND_CLI_MAX_TIMEOUT
167+ totalElapsedTime > maxTotalElapsedTime
166168 ) {
167169 cleanup ( ) ;
168170 const timeoutReason =
169- totalElapsedTime > PLAYGROUND_CLI_MAX_TIMEOUT
170- ? `Maximum timeout of ${ PLAYGROUND_CLI_MAX_TIMEOUT / 1000 } s exceeded`
171+ totalElapsedTime > maxTotalElapsedTime
172+ ? `Maximum timeout of ${ maxTotalElapsedTime / 1000 } s exceeded`
171173 : `No activity for ${ PLAYGROUND_CLI_INACTIVITY_TIMEOUT / 1000 } s` ;
172174 reject (
173175 new Error ( `Timeout waiting for response to message ${ messageId } : ${ timeoutReason } ` )
@@ -213,7 +215,7 @@ async function sendMessage(
213215
214216 bus . on ( 'process:msg' , responseHandler ) ;
215217
216- sendMessageToProcess ( pmId , { ...message , messageId } as ManagerMessage ) . catch ( reject ) ;
218+ sendMessageToProcess ( pmId , { ...message , messageId } ) . catch ( reject ) ;
217219 } ) ;
218220}
219221
@@ -225,7 +227,7 @@ export async function stopWordPressServer( siteId: string ): Promise< void > {
225227
226228 if ( runningProcess ) {
227229 try {
228- await sendStopMessage ( runningProcess . pmId ) ;
230+ await sendMessage ( runningProcess . pmId , { topic : 'stop-server' } , GRACEFUL_STOP_TIMEOUT ) ;
229231 } catch {
230232 // Graceful shutdown failed, PM2 delete will handle it
231233 }
@@ -234,63 +236,6 @@ export async function stopWordPressServer( siteId: string ): Promise< void > {
234236 return stopProcess ( processName ) ;
235237}
236238
237- /**
238- * Send stop message to the child process with a timeout
239- */
240- async function sendStopMessage ( pmId : number ) : Promise < void > {
241- const bus = await getPm2Bus ( ) ;
242-
243- return new Promise ( ( resolve , reject ) => {
244- const messageId = nextMessageId ++ ;
245-
246- const timeout = setTimeout ( ( ) => {
247- cleanup ( ) ;
248- reject ( new Error ( 'Graceful stop timeout' ) ) ;
249- } , GRACEFUL_STOP_TIMEOUT ) ;
250-
251- const cleanup = ( ) => {
252- clearTimeout ( timeout ) ;
253- bus . off ( 'process:msg' , responseHandler ) ;
254- } ;
255-
256- const responseHandler = ( packet : unknown ) => {
257- const validationResult = childMessagePm2Schema . safeParse ( packet ) ;
258- if ( ! validationResult . success ) {
259- return ;
260- }
261-
262- const validPacket = validationResult . data ;
263-
264- if ( validPacket . process . pm_id !== pmId ) {
265- return ;
266- }
267-
268- if ( validPacket . raw . topic === 'error' && validPacket . raw . originalMessageId === messageId ) {
269- cleanup ( ) ;
270- reject ( new Error ( validPacket . raw . errorMessage ) ) ;
271- } else if (
272- validPacket . raw . topic === 'result' &&
273- validPacket . raw . originalMessageId === messageId
274- ) {
275- cleanup ( ) ;
276- resolve ( ) ;
277- }
278- } ;
279-
280- bus . on ( 'process:msg' , responseHandler ) ;
281-
282- const stopMessage = {
283- topic : 'stop-server' as const ,
284- data : { } ,
285- messageId,
286- } ;
287- sendMessageToProcess ( pmId , stopMessage ) . catch ( ( error ) => {
288- cleanup ( ) ;
289- reject ( error ) ;
290- } ) ;
291- } ) ;
292- }
293-
294239/**
295240 * Run a blueprint on a site without starting a server
296241 * 1. Start the PM2 process
0 commit comments