this.opts.workerChoiceStrategyOptions =
opts.workerChoiceStrategyOptions ??
DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+ this.checkValidWorkerChoiceStrategyOptions(
+ this.opts.workerChoiceStrategyOptions
+ )
this.opts.enableEvents = opts.enableEvents ?? true
this.opts.enableTasksQueue = opts.enableTasksQueue ?? false
if (this.opts.enableTasksQueue) {
'Invalid worker choice strategy options: must be a plain object'
)
}
+ if (
+ workerChoiceStrategyOptions.weights != null &&
+ Object.keys(workerChoiceStrategyOptions.weights).length !== this.size
+ ) {
+ throw new Error(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
+ }
}
private checkValidTasksQueueOptions (
this.choose = this.choose.bind(this)
}
- protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
+ protected setRequiredStatistics (opts: WorkerChoiceStrategyOptions): void {
if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
this.requiredStatistics.avgRunTime = false
this.requiredStatistics.medRunTime = opts.medRunTime as boolean
this.requiredStatistics.avgRunTime = true
this.requiredStatistics.medRunTime = opts.medRunTime as boolean
}
- if (
- opts.weights != null &&
- Object.keys(opts.weights).length < this.pool.size
- ) {
- throw new Error(
- 'Worker choice strategy options must have a weight for each worker node.'
- )
- }
}
/** @inheritDoc */
/** @inheritDoc */
public setOptions (opts: WorkerChoiceStrategyOptions): void {
opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
- this.checkOptions(opts)
+ this.setRequiredStatistics(opts)
this.opts = opts
}
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
}
/** @inheritDoc */
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
}
/** @inheritDoc */
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
}
/** @inheritDoc */
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
}
/** @inheritDoc */
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
}
}
return item
}
+
+ /**
+ * Peek at the first item.
+ */
+ public peek (): T | undefined {
+ if (this.size <= 0) return undefined
+ return this.items[this.head]
+ }
}
'./tests/worker-files/thread/testWorker.js',
{
workerChoiceStrategy: WorkerChoiceStrategies.LESS_USED,
- workerChoiceStrategyOptions: { medRunTime: true },
+ workerChoiceStrategyOptions: {
+ medRunTime: true,
+ weights: { 0: 300 }
+ },
enableEvents: false,
enableTasksQueue: true,
tasksQueueOptions: { concurrency: 2 },
WorkerChoiceStrategies.LESS_USED
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
- medRunTime: true
+ medRunTime: true,
+ weights: { 0: 300 }
})
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
}
)
).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
+ expect(
+ () =>
+ new FixedThreadPool(
+ numberOfWorkers,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ workerChoiceStrategyOptions: { weights: {} }
+ }
+ )
+ ).toThrowError(
+ 'Invalid worker choice strategy options: must have a weight for each worker node'
+ )
})
it('Verify that worker choice strategy options can be set', async () => {