msg.data as Statistics,
);
break;
+ case ChargingStationWorkerMessageEvents.startWorkerElementError:
+ logger.error(
+ `${this.logPrefix()} ${moduleName}.messageHandler: Error occured while starting worker element:`,
+ msg.data,
+ );
+ this.emit(ChargingStationWorkerMessageEvents.startWorkerElementError, msg.data);
+ break;
+ case ChargingStationWorkerMessageEvents.startedWorkerElement:
+ break;
default:
throw new BaseError(
`Unknown event type: '${msg.event}' for data: ${JSON.stringify(msg.data, null, 2)}`,
// Add message listener to create and start charging station from the main thread
parentPort?.on('message', (message: WorkerMessage<ChargingStationWorkerData>) => {
if (message.event === WorkerMessageEvents.startWorkerElement) {
- this.runInAsyncScope(
- startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void,
- this,
- message.data,
- );
+ try {
+ this.runInAsyncScope(
+ startChargingStation.bind(this) as (data?: ChargingStationWorkerData) => void,
+ this,
+ message.data,
+ );
+ parentPort?.postMessage({
+ event: WorkerMessageEvents.startedWorkerElement,
+ });
+ } catch (error) {
+ parentPort?.postMessage({
+ event: WorkerMessageEvents.startWorkerElementError,
+ data: {
+ message: (error as Error).message,
+ stack: (error as Error).stack,
+ },
+ });
+ }
}
});
}
public static readonly DEFAULT_POOL_MAX_SIZE = availableParallelism();
public static readonly DEFAULT_ELEMENTS_PER_WORKER = 1;
- public static readonly version = '1.0.0';
+ public static readonly version = '1.0.1';
private constructor() {
// This is intentional
}
const workerSetElement = await this.getWorkerSetElement();
workerSetElement.worker.postMessage({
- id: WorkerMessageEvents.startWorkerElement,
+ event: WorkerMessageEvents.startWorkerElement,
data: elementData,
});
++workerSetElement.numberOfWorkerElements;
export enum WorkerMessageEvents {
startWorkerElement = 'startWorkerElement',
+ startWorkerElementError = 'startWorkerElementError',
+ startedWorkerElement = 'startedWorkerElement',
}