repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix worker startup and cleanup some attributes in Wrk class.
[e-mobility-charging-stations-simulator.git]
/
src
/
charging-station
/
Worker.ts
diff --git
a/src/charging-station/Worker.ts
b/src/charging-station/Worker.ts
index fbfdf3d1f047fa1886febf126750265ae73c5d79..efa327fd8a0666d301bfcae95d8b332a79158958 100644
(file)
--- a/
src/charging-station/Worker.ts
+++ b/
src/charging-station/Worker.ts
@@
-1,15
+1,13
@@
import { Worker, WorkerOptions } from 'worker_threads';
import Configuration from '../utils/Configuration';
import { Worker, WorkerOptions } from 'worker_threads';
import Configuration from '../utils/Configuration';
+import Constants from '../utils/Constants';
import Pool from 'worker-threads-pool';
import WorkerData from '../types/WorkerData';
import Pool from 'worker-threads-pool';
import WorkerData from '../types/WorkerData';
-import Constants from '../utils/Constants';
export default class Wrk {
private _workerScript: string;
private _workerData: WorkerData;
export default class Wrk {
private _workerScript: string;
private _workerData: WorkerData;
- private _index: number;
- private _concurrentWorkers: number;
private _worker: Worker;
/**
private _worker: Worker;
/**
@@
-17,28
+15,15
@@
export default class Wrk {
*
* @param {string} workerScript
* @param {WorkerData} workerData
*
* @param {string} workerScript
* @param {WorkerData} workerData
- * @param {number} numConcurrentWorkers
*/
*/
- constructor(workerScript: string, workerData: WorkerData
, numConcurrentWorkers: number
) {
+ constructor(workerScript: string, workerData: WorkerData) {
this._workerData = workerData;
this._workerData = workerData;
- this._index = workerData.index;
this._workerScript = workerScript;
if (Configuration.useWorkerPool()) {
this._workerScript = workerScript;
if (Configuration.useWorkerPool()) {
- this._concurrentWorkers = Configuration.getWorkerPoolSize();
- WorkerPool.concurrentWorkers = this._concurrentWorkers;
- } else {
- this._concurrentWorkers = numConcurrentWorkers;
+ WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize();
}
}
}
}
- /**
- * @return {number}
- * @public
- */
- public get concurrentWorkers(): number {
- return this._concurrentWorkers;
- }
-
/**
*
* @return {Promise}
/**
*
* @return {Promise}
@@
-46,23
+31,25
@@
export default class Wrk {
*/
async start(): Promise<Worker> {
if (Configuration.useWorkerPool()) {
*/
async start(): Promise<Worker> {
if (Configuration.useWorkerPool()) {
- this._startWorkerWithPool();
+
await
this._startWorkerWithPool();
} else {
} else {
- this._startWorker();
+
await
this._startWorker();
}
return this._worker;
}
}
return this._worker;
}
-
/**
+ /**
*
*
- * @return {
Promise
}
+ * @return {
void
}
* @public
*/
* @public
*/
- async startNewChargingStation(workerData: WorkerData, numConcurrentWorkers: number): Promise<void> {
+ addWorkerElement(workerData: WorkerData): void {
+ // FIXME: also forbid to add an element if the current number of elements > max number of elements
+ if (Configuration.useWorkerPool()) {
+ return;
+ }
this._workerData = workerData;
this._workerData = workerData;
- this._index = workerData.index;
- this._concurrentWorkers = numConcurrentWorkers;
- this._worker.postMessage({ id : Constants.START_NEW_CHARGING_STATION, workerData: workerData });
+ this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData });
}
/**
}
/**
@@
-95,7
+82,7
@@
export default class Wrk {
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0) {
- reject(new Error(`Worker
id ${this._index}
stopped with exit code ${code}`));
+ reject(new Error(`Worker stopped with exit code ${code}`));
}
});
this._worker = worker;
}
});
this._worker = worker;
@@
-104,14
+91,14
@@
export default class Wrk {
}
class WorkerPool {
}
class WorkerPool {
- public static
c
oncurrentWorkers: number;
+ public static
maxC
oncurrentWorkers: number;
private static _instance: Pool;
private constructor() { }
public static getInstance(): Pool {
private static _instance: Pool;
private constructor() { }
public static getInstance(): Pool {
- if (!WorkerPool._instance
|| (WorkerPool._instance?.size === WorkerPool.concurrentWorkers)
) {
- WorkerPool._instance = new Pool({ max: WorkerPool.
c
oncurrentWorkers });
+ if (!WorkerPool._instance) {
+ WorkerPool._instance = new Pool({ max: WorkerPool.
maxC
oncurrentWorkers });
}
return WorkerPool._instance;
}
}
return WorkerPool._instance;
}