import { ChargingStation } from './ChargingStation';
import type { ChargingStationWorkerData } from '../types';
import { Configuration } from '../utils';
-import { WorkerConstants, type WorkerMessage, WorkerMessageEvents } from '../worker';
+import { POOL_MAX_INACTIVE_TIME, type WorkerMessage, WorkerMessageEvents } from '../worker';
const moduleName = 'ChargingStationWorker';
export let chargingStationWorker: ChargingStationWorker | ThreadWorker<ChargingStationWorkerData>;
if (Configuration.workerPoolInUse()) {
chargingStationWorker = new ThreadWorker<ChargingStationWorkerData>(startChargingStation, {
- maxInactiveTime: WorkerConstants.POOL_MAX_INACTIVE_TIME,
+ maxInactiveTime: POOL_MAX_INACTIVE_TIME,
});
} else {
chargingStationWorker = new ChargingStationWorker();
type UIServerConfiguration,
type WorkerConfiguration,
} from '../types';
-import { WorkerConstants, WorkerProcessType } from '../worker';
+import {
+ DEFAULT_ELEMENT_START_DELAY,
+ DEFAULT_POOL_MAX_SIZE,
+ DEFAULT_POOL_MIN_SIZE,
+ DEFAULT_WORKER_START_DELAY,
+ WorkerProcessType,
+} from '../worker';
type ConfigurationSectionType =
| LogConfiguration
);
const defaultWorkerConfiguration: WorkerConfiguration = {
processType: WorkerProcessType.workerSet,
- startDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
+ startDelay: DEFAULT_WORKER_START_DELAY,
elementsPerWorker: 'auto',
- elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
- poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
- poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
+ elementStartDelay: DEFAULT_ELEMENT_START_DELAY,
+ poolMinSize: DEFAULT_POOL_MIN_SIZE,
+ poolMaxSize: DEFAULT_POOL_MAX_SIZE,
};
hasOwnProp(Configuration.getConfigurationData(), 'workerPoolStrategy') &&
delete Configuration.getConfigurationData()?.workerPoolStrategy;
-import { availableParallelism } from 'poolifier';
+import { type ThreadPoolOptions, availableParallelism } from 'poolifier';
-export class WorkerConstants {
- public static readonly EMPTY_FUNCTION = Object.freeze(() => {
- /* This is intentional */
- });
+import type { WorkerOptions } from './WorkerTypes';
- public static readonly DEFAULT_ELEMENT_START_DELAY = 0;
- public static readonly DEFAULT_WORKER_START_DELAY = 500;
- public static readonly POOL_MAX_INACTIVE_TIME = 60000;
- public static readonly DEFAULT_POOL_MIN_SIZE = Math.floor(availableParallelism() / 2);
- public static readonly DEFAULT_POOL_MAX_SIZE = Math.round(availableParallelism() * 1.5);
- public static readonly DEFAULT_ELEMENTS_PER_WORKER = 1;
+export const EMPTY_FUNCTION = Object.freeze(() => {
+ /* This is intentional */
+});
- public static readonly version = '1.0.1';
+export const workerSetVersion = '1.0.1';
- private constructor() {
- // This is intentional
- }
-}
+export const DEFAULT_ELEMENT_START_DELAY = 0;
+export const DEFAULT_WORKER_START_DELAY = 500;
+export const POOL_MAX_INACTIVE_TIME = 60000;
+export const DEFAULT_POOL_MIN_SIZE = Math.floor(availableParallelism() / 2);
+export const DEFAULT_POOL_MAX_SIZE = Math.round(availableParallelism() * 1.5);
+export const DEFAULT_ELEMENTS_PER_WORKER = 1;
+
+export const DEFAULT_WORKER_OPTIONS: WorkerOptions = Object.freeze({
+ workerStartDelay: DEFAULT_WORKER_START_DELAY,
+ elementStartDelay: DEFAULT_ELEMENT_START_DELAY,
+ poolMinSize: DEFAULT_POOL_MIN_SIZE,
+ poolMaxSize: DEFAULT_POOL_MAX_SIZE,
+ elementsPerWorker: DEFAULT_ELEMENTS_PER_WORKER,
+ poolOptions: {},
+});
+
+export const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = {
+ enableEvents: true,
+ restartWorkerOnError: true,
+};
import { isMainThread } from 'node:worker_threads';
import type { WorkerAbstract } from './WorkerAbstract';
-import { WorkerConstants } from './WorkerConstants';
+import { DEFAULT_WORKER_OPTIONS } from './WorkerConstants';
import { WorkerDynamicPool } from './WorkerDynamicPool';
import { WorkerSet } from './WorkerSet';
import { WorkerStaticPool } from './WorkerStaticPool';
import { type WorkerData, type WorkerOptions, WorkerProcessType } from './WorkerTypes';
-const DEFAULT_WORKER_OPTIONS: WorkerOptions = {
- workerStartDelay: WorkerConstants.DEFAULT_WORKER_START_DELAY,
- elementStartDelay: WorkerConstants.DEFAULT_ELEMENT_START_DELAY,
- poolMinSize: WorkerConstants.DEFAULT_POOL_MIN_SIZE,
- poolMaxSize: WorkerConstants.DEFAULT_POOL_MAX_SIZE,
- elementsPerWorker: WorkerConstants.DEFAULT_ELEMENTS_PER_WORKER,
- poolOptions: {},
-};
-
export class WorkerFactory {
private constructor() {
// This is intentional
import { EventEmitter } from 'node:events';
import { SHARE_ENV, Worker } from 'node:worker_threads';
-import type { ThreadPoolOptions } from 'poolifier';
-
import { WorkerAbstract } from './WorkerAbstract';
-import { WorkerConstants } from './WorkerConstants';
+import { DEFAULT_POOL_OPTIONS, EMPTY_FUNCTION, workerSetVersion } from './WorkerConstants';
import {
type SetInfo,
type WorkerData,
} from './WorkerTypes';
import { sleep } from './WorkerUtils';
-const DEFAULT_POOL_OPTIONS: ThreadPoolOptions = {
- enableEvents: true,
- restartWorkerOnError: true,
-};
-
export class WorkerSet extends WorkerAbstract<WorkerData> {
public readonly emitter!: EventEmitter;
private readonly workerSet: Set<WorkerSetElement>;
get info(): SetInfo {
return {
- version: WorkerConstants.version,
+ version: workerSetVersion,
type: 'set',
worker: 'thread',
size: this.size,
env: SHARE_ENV,
...this.workerOptions.poolOptions?.workerOptions,
});
- worker.on(
- 'message',
- this.workerOptions.poolOptions?.messageHandler ?? WorkerConstants.EMPTY_FUNCTION,
- );
+ worker.on('message', this.workerOptions.poolOptions?.messageHandler ?? EMPTY_FUNCTION);
worker.on('message', (message: WorkerMessage<WorkerData>) => {
if (message.event === WorkerMessageEvents.startedWorkerElement) {
this.emitter?.emit(WorkerSetEvents.elementStarted, this.info);
this.emitter?.emit(WorkerSetEvents.elementError, message.data);
}
});
- worker.on(
- 'error',
- this.workerOptions.poolOptions?.errorHandler ?? WorkerConstants.EMPTY_FUNCTION,
- );
+ worker.on('error', this.workerOptions.poolOptions?.errorHandler ?? EMPTY_FUNCTION);
worker.on('error', (error) => {
this.emitter?.emit(WorkerSetEvents.error, error);
if (this.workerOptions.poolOptions?.restartWorkerOnError) {
this.addWorkerSetElement();
}
});
- worker.on(
- 'online',
- this.workerOptions.poolOptions?.onlineHandler ?? WorkerConstants.EMPTY_FUNCTION,
- );
- worker.on(
- 'exit',
- this.workerOptions.poolOptions?.exitHandler ?? WorkerConstants.EMPTY_FUNCTION,
- );
+ worker.on('online', this.workerOptions.poolOptions?.onlineHandler ?? EMPTY_FUNCTION);
+ worker.on('exit', this.workerOptions.poolOptions?.exitHandler ?? EMPTY_FUNCTION);
worker.once('exit', () =>
this.removeWorkerSetElement(this.getWorkerSetElementByWorker(worker)!),
);
export type { WorkerAbstract } from './WorkerAbstract';
-export { WorkerConstants } from './WorkerConstants';
+export {
+ DEFAULT_ELEMENT_START_DELAY,
+ DEFAULT_POOL_MAX_SIZE,
+ DEFAULT_POOL_MIN_SIZE,
+ DEFAULT_WORKER_START_DELAY,
+ POOL_MAX_INACTIVE_TIME,
+} from './WorkerConstants';
export { WorkerFactory } from './WorkerFactory';
export {
type WorkerData,