summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
fa418e1)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
this.opts.workerChoiceStrategyOptions =
opts.workerChoiceStrategyOptions ??
DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
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) {
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'
)
}
'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 (
}
private checkValidTasksQueueOptions (
this.choose = this.choose.bind(this)
}
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
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
}
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 */
public setOptions (opts: WorkerChoiceStrategyOptions): void {
opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
/** @inheritDoc */
public setOptions (opts: WorkerChoiceStrategyOptions): void {
opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
- this.checkOptions(opts)
+ this.setRequiredStatistics(opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
) {
super(pool, opts)
- this.checkOptions(this.opts)
+ this.setRequiredStatistics(this.opts)
this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
}
this.defaultWorkerWeight = this.computeDefaultWorkerWeight()
}
+
+ /**
+ * 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,
'./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 },
enableEvents: false,
enableTasksQueue: true,
tasksQueueOptions: { concurrency: 2 },
WorkerChoiceStrategies.LESS_USED
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
WorkerChoiceStrategies.LESS_USED
)
expect(pool.opts.workerChoiceStrategyOptions).toStrictEqual({
+ medRunTime: true,
+ weights: { 0: 300 }
})
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
})
expect(pool.opts.messageHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
}
)
).toThrowError("Invalid worker choice strategy 'invalidStrategy'")
}
)
).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 () => {
})
it('Verify that worker choice strategy options can be set', async () => {