Skip to content

Commit

Permalink
Use handlers approach in server child process
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed May 6, 2024
1 parent 3f7c463 commit 3566662
Showing 1 changed file with 51 additions and 36 deletions.
87 changes: 51 additions & 36 deletions src/lib/site-server-process-child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { WPNowOptions } from '../../vendor/wp-now/src/config';
import { setupLogging } from '../logging';
import type { MessageName } from './site-server-process';

type Handler = ( message: string, messageId: number, data: unknown ) => void;
type Handlers = { [ K in MessageName ]: Handler };

// Setup logging for the forked process
if ( process.env.STUDIO_APP_LOGS_PATH ) {
setupLogging( {
Expand All @@ -16,57 +19,69 @@ if ( process.env.STUDIO_APP_LOGS_PATH ) {
const options = JSON.parse( process.argv[ 2 ] ) as WPNowOptions;
let server: WPNowServer;

async function start( message: string, messageId: number ) {
const handlers: Handlers = {
'start-server': createHandler( start ),
'stop-server': createHandler( stop ),
'run-php': createHandler( runPhp ),
};

async function start() {
server = await startServer( options );
process.parentPort.postMessage( {
message,
messageId,
data: {
php: {
documentRoot: server.php.documentRoot,
},
return {
php: {
documentRoot: server.php.documentRoot,
},
} );
};
}

async function stop( message: string, messageId: number ) {
async function stop() {
await server.stopServer();
process.parentPort.postMessage( {
message,
messageId,
} );
}

async function runPhp( message: string, messageId: number, data: PHPRunOptions ) {
const response = await server.php.run( data );
process.parentPort.postMessage( { message, messageId, data: response.text } );
async function runPhp( data: unknown ) {
const request = data as PHPRunOptions;
if ( ! request ) {
throw Error( 'PHP request is not valid' );
}
const response = await server.php.run( request );
return response.text;
}

function createHandler< T >( handler: ( data: unknown ) => T ) {
return async ( message: string, messageId: number, data: unknown ) => {
try {
const response = await handler( data );
process.parentPort.postMessage( {
message,
messageId,
data: response,
} );
} catch ( error ) {
const errorObj = error as Error;
if ( ! errorObj ) {
process.parentPort.postMessage( { message, messageId, error: Error( 'Unknown Error' ) } );
return;
}
process.parentPort.postMessage( {
message,
messageId,
error: errorObj,
} );
}
};
}

process.parentPort.on( 'message', async ( { data: messagePayload } ) => {
const { message, messageId, data }: { message: MessageName; messageId: number; data: unknown } =
messagePayload;
try {
switch ( message ) {
case 'start-server':
await start( message, messageId );
break;
case 'stop-server':
await stop( message, messageId );
break;
case 'run-php':
await runPhp( message, messageId, data as PHPRunOptions );
break;
}
} catch ( error ) {
const errorObj = error as Error;
if ( ! errorObj ) {
process.parentPort.postMessage( { message, messageId, error: Error( 'Unknown Error' ) } );
return;
}
const handler = handlers[ message ];
if ( ! handler ) {
process.parentPort.postMessage( {
message,
messageId,
error: errorObj,
error: Error( `No handler defined for message '${ message }'` ),
} );
return;
}
await handler( message, messageId, data );
} );

0 comments on commit 3566662

Please sign in to comment.