Worker,
Data,
Response
- >(
- this,
- () => {
- const createdWorker = this.createAndSetupWorker()
- this.registerWorkerMessageListener(createdWorker, message => {
- if (
- isKillBehavior(KillBehaviors.HARD, message.kill) ||
- this.getWorkerTasksUsage(createdWorker)?.running === 0
- ) {
- // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
- void this.destroyWorker(createdWorker)
- }
- })
- return this.getWorkerKey(createdWorker)
- },
- this.opts.workerChoiceStrategy
- )
+ >(this, this.opts.workerChoiceStrategy)
}
private checkFilePath (filePath: string): void {
* @returns [worker key, worker].
*/
protected chooseWorker (): [number, Worker] {
- const workerKey = this.workerChoiceStrategyContext.execute()
+ let workerKey: number
+ if (
+ this.type === PoolType.DYNAMIC &&
+ !this.full &&
+ this.findFreeWorkerKey() === -1
+ ) {
+ const createdWorker = this.createAndSetupWorker()
+ this.registerWorkerMessageListener(createdWorker, message => {
+ if (
+ isKillBehavior(KillBehaviors.HARD, message.kill) ||
+ this.getWorkerTasksUsage(createdWorker)?.running === 0
+ ) {
+ // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
+ void this.destroyWorker(createdWorker)
+ }
+ })
+ workerKey = this.getWorkerKey(createdWorker)
+ } else {
+ workerKey = this.workerChoiceStrategyContext.execute()
+ }
return [workerKey, this.workers[workerKey].worker]
}
Worker extends IPoolWorker,
Data = unknown,
Response = unknown
-> implements IWorkerChoiceStrategy<Worker, Data, Response> {
+> implements IWorkerChoiceStrategy {
/** @inheritDoc */
public readonly isDynamicPool: boolean
/** @inheritDoc */
Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
+ implements IWorkerChoiceStrategy {
/** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
+ implements IWorkerChoiceStrategy {
/** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
+ implements IWorkerChoiceStrategy {
/** @inheritDoc */
public reset (): boolean {
return true
Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
+ implements IWorkerChoiceStrategy {
/**
* Id of the next worker.
*/
-import type { IPoolInternal } from '../pool-internal'
-import type { IPoolWorker } from '../pool-worker'
-
/**
* Enumeration of worker choice strategies.
*/
/**
* Worker choice strategy interface.
*/
-export interface IWorkerChoiceStrategy<
- Worker extends IPoolWorker,
- Data = unknown,
- Response = unknown
-> {
- /**
- * The pool instance.
- */
- readonly pool: IPoolInternal<Worker, Data, Response>
+export interface IWorkerChoiceStrategy {
/**
* Is the pool bound to the strategy dynamic?.
*/
Response = unknown
>
extends AbstractWorkerChoiceStrategy<Worker, Data, Response>
- implements IWorkerChoiceStrategy<Worker, Data, Response> {
+ implements IWorkerChoiceStrategy {
/** @inheritDoc */
public readonly requiredStatistics: RequiredStatistics = {
runTime: true,
> {
private readonly workerChoiceStrategies: Map<
WorkerChoiceStrategy,
- IWorkerChoiceStrategy<Worker, Data, Response>
+ IWorkerChoiceStrategy
>
/**
*/
public constructor (
pool: IPoolInternal<Worker, Data, Response>,
- private readonly createWorkerCallback: () => number,
private workerChoiceStrategyType: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN
) {
this.execute.bind(this)
this.workerChoiceStrategies = new Map<
WorkerChoiceStrategy,
- IWorkerChoiceStrategy<Worker, Data, Response>
+ IWorkerChoiceStrategy
>([
[
WorkerChoiceStrategies.ROUND_ROBIN,
return (
this.workerChoiceStrategies.get(
this.workerChoiceStrategyType
- ) as IWorkerChoiceStrategy<Worker, Data, Response>
+ ) as IWorkerChoiceStrategy
).requiredStatistics
}
* @returns The key of the chosen one.
*/
public execute (): number {
- const workerChoiceStrategy = this.workerChoiceStrategies.get(
- this.workerChoiceStrategyType
- ) as IWorkerChoiceStrategy<Worker, Data, Response>
- if (
- workerChoiceStrategy.isDynamicPool &&
- !workerChoiceStrategy.pool.full &&
- workerChoiceStrategy.pool.findFreeWorkerKey() === -1
- ) {
- return this.createWorkerCallback()
- }
- return workerChoiceStrategy.choose()
+ return (
+ this.workerChoiceStrategies.get(
+ this.workerChoiceStrategyType
+ ) as IWorkerChoiceStrategy
+ ).choose()
}
/**
return (
this.workerChoiceStrategies.get(
this.workerChoiceStrategyType
- ) as IWorkerChoiceStrategy<Worker, Data, Response>
+ ) as IWorkerChoiceStrategy
).remove(workerKey)
}
}