patch: refactor api to use a single topic

This commit is contained in:
Edwin Joassart 2024-02-29 18:06:03 +01:00
parent b1d2bdaa06
commit 6582260355
2 changed files with 90 additions and 65 deletions

View File

@ -128,34 +128,44 @@ function startApiAndSpawnChild({
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
ipc.serve(); ipc.serve();
// log is special message which brings back the logs from the child process and prints them to the console // parse and route messages
ipc.server.on('log', (message: string) => { const messagesHandler: any = {
log: (message: any) => {
console.log(message); console.log(message);
}); },
// api to register more handlers with callbacks error: (error: any) => {
const registerHandler = (event: string, handler: any) => { terminateServer(ipc.server);
ipc.server.on(event, handler); const errorObject = errors.fromJSON(error);
}; reject(errorObject);
},
// once api is ready (means child process is connected) we pass the emit and terminate function to the caller // once api is ready (means child process is connected) we pass the emit and terminate function to the caller
ipc.server.on('ready', (_: any, socket) => { ready: (_: any, socket: any) => {
const emit = (channel: string, data: any) => { const emit = (type: string, payload: any) => {
ipc.server.emit(socket, channel, data); ipc.server.emit(socket, 'message', { type, payload });
}; };
resolve({ resolve({
emit, emit,
terminateServer: () => terminateServer(ipc.server), terminateServer: () => terminateServer(ipc.server),
registerHandler, registerHandler,
}); });
},
};
ipc.server.on('message', (data: any, socket: any) => {
const message = messagesHandler[data.type];
if (message) {
message(data.payload, socket);
} else {
throw new Error(`Unknown message type: ${data.type}`);
}
}); });
// on api error we terminate // api to register more handlers with callbacks
ipc.server.on('error', (error: any) => { const registerHandler = (event: string, handler: any) => {
terminateServer(ipc.server); messagesHandler[event] = handler;
const errorObject = errors.fromJSON(error); };
reject(errorObject);
});
// when the api is started we spawn the child process // when the api is started we spawn the child process
ipc.server.on('start', async () => { ipc.server.on('start', async () => {

View File

@ -48,11 +48,16 @@ ipc.config.stopRetrying = 0;
const DISCONNECT_DELAY = 100; const DISCONNECT_DELAY = 100;
const IPC_SERVER_ID = process.env.IPC_SERVER_ID as string; const IPC_SERVER_ID = process.env.IPC_SERVER_ID as string;
console.log('starting ');
if (!IPC_SERVER_ID) {
console.log('IPC_SERVER_ID is not defined, exiting');
}
/** /**
* @summary Send a message to the IPC server * @summary Send a message to the IPC server
*/ */
function emit(channel: string, message?: any) { function emit(type: string, payload?: any) {
ipc.of[IPC_SERVER_ID].emit(channel, message); ipc.of[IPC_SERVER_ID].emit('message', { type, payload });
} }
/** /**
@ -129,26 +134,12 @@ ipc.connectTo(IPC_SERVER_ID, () => {
await terminate(SUCCESS); await terminate(SUCCESS);
}); });
ipc.of[IPC_SERVER_ID].on('sourceMetadata', async (params) => { const messagesHandler: any = {
const { selected, SourceType, auth } = JSON.parse(params); scan: () => {
try {
const sourceMatadata = await getSourceMetadata(
selected,
SourceType,
auth,
);
emitSourceMetadata(sourceMatadata);
} catch (error: any) {
emitFail(error);
}
});
ipc.of[IPC_SERVER_ID].on('scan', async () => {
startScanning(); startScanning();
}); },
// write handler write: async (options: WriteOptions) => {
ipc.of[IPC_SERVER_ID].on('write', async (options: WriteOptions) => {
// Remove leftover tmp files older than 1 hour // Remove leftover tmp files older than 1 hour
cleanup(Date.now() - 60 * 60 * 1000); cleanup(Date.now() - 60 * 60 * 1000);
@ -170,6 +161,30 @@ ipc.connectTo(IPC_SERVER_ID, () => {
emit('done', { results }); emit('done', { results });
await delay(DISCONNECT_DELAY); await delay(DISCONNECT_DELAY);
await terminate(exitCode); await terminate(exitCode);
},
sourceMetadata: async (params: any) => {
const { selected, SourceType, auth } = JSON.parse(params);
try {
const sourceMatadata = await getSourceMetadata(
selected,
SourceType,
auth,
);
emitSourceMetadata(sourceMatadata);
} catch (error: any) {
emitFail(error);
}
},
};
ipc.of[IPC_SERVER_ID].on('message', async (data: any) => {
const message = messagesHandler[data.type];
if (message) {
await message(data.payload);
} else {
throw new Error(`Unknown message type: ${data.type}`);
}
}); });
ipc.of[IPC_SERVER_ID].on('connect', () => { ipc.of[IPC_SERVER_ID].on('connect', () => {