Skip to content

Commit 93f54ca

Browse files
fredrikekelundbcotrim
authored andcommitted
Fix types and remove sendStopMessage
1 parent e2191b9 commit 93f54ca

File tree

2 files changed

+20
-72
lines changed

2 files changed

+20
-72
lines changed

cli/lib/types/wordpress-server-ipc.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { z } from 'zod';
22

33
// Zod schemas for validating IPC messages from wordpress-server-manager
4-
const managerMessageBase = z.object( {
5-
messageId: z.number(),
6-
} );
7-
84
const serverConfig = z.object( {
95
siteId: z.string(),
106
sitePath: z.string(),
@@ -21,30 +17,37 @@ const serverConfig = z.object( {
2117

2218
export type ServerConfig = z.infer< typeof serverConfig >;
2319

24-
const managerMessageStartServer = managerMessageBase.extend( {
20+
const managerMessageStartServer = z.object( {
2521
topic: z.literal( 'start-server' ),
2622
data: z.object( {
2723
config: serverConfig,
2824
} ),
2925
} );
3026

31-
const managerMessageRunBlueprint = managerMessageBase.extend( {
27+
const managerMessageRunBlueprint = z.object( {
3228
topic: z.literal( 'run-blueprint' ),
3329
data: z.object( {
3430
config: serverConfig,
3531
} ),
3632
} );
3733

38-
const managerMessageStopServer = managerMessageBase.extend( {
34+
const managerMessageStopServer = z.object( {
3935
topic: z.literal( 'stop-server' ),
40-
data: z.object( {} ),
4136
} );
4237

43-
export const managerMessageSchema = z.discriminatedUnion( 'topic', [
38+
const _managerMessagePayloadSchema = z.discriminatedUnion( 'topic', [
4439
managerMessageStartServer,
4540
managerMessageRunBlueprint,
4641
managerMessageStopServer,
4742
] );
43+
export type ManagerMessagePayload = z.infer< typeof _managerMessagePayloadSchema >;
44+
45+
const managerMessageBase = z.object( { messageId: z.number() } );
46+
export const managerMessageSchema = z.discriminatedUnion( 'topic', [
47+
managerMessageBase.merge( managerMessageStartServer ),
48+
managerMessageBase.merge( managerMessageRunBlueprint ),
49+
managerMessageBase.merge( managerMessageStopServer ),
50+
] );
4851
export type ManagerMessage = z.infer< typeof managerMessageSchema >;
4952

5053
// Zod schemas for validating IPC messages from wordpress-server-child

cli/lib/wordpress-server-manager.ts

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ServerConfig,
2424
childMessagePm2Schema,
2525
ManagerMessage,
26+
ManagerMessagePayload,
2627
} from 'cli/lib/types/wordpress-server-ipc';
2728

2829
function getProcessName( siteId: string ): string {
@@ -137,7 +138,8 @@ const messageActivityTrackers = new Map<
137138

138139
async 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

Comments
 (0)