This option only apply to the newly created workers.
Default: `KillBehaviors.SOFT`
-- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die.
+- `maxInactiveTime` (optional) - Maximum waiting time in milliseconds for tasks on newly created workers. After this time newly created workers will die. It must be a positive integer greater or equal than 5.
The last active time of your worker will be updated when it terminates a task.
If `killBehavior` is set to `KillBehaviors.HARD` this value represents also the timeout for the tasks that you submit to the pool, when this timeout expires your tasks is interrupted before completion and removed. The worker is killed if is not part of the minimum size of the pool.
If `killBehavior` is set to `KillBehaviors.SOFT` your tasks have no timeout and your workers will not be terminated until your task is completed.
}
private checkWorkerOptions (opts: WorkerOptions): void {
+ if (opts != null && !isPlainObject(opts)) {
+ throw new TypeError('opts worker options parameter is not a plain object')
+ }
+ if (
+ opts?.killBehavior != null &&
+ !Object.values(KillBehaviors).includes(opts.killBehavior)
+ ) {
+ throw new TypeError(
+ `killBehavior option '${opts.killBehavior}' is not valid`
+ )
+ }
+ if (
+ opts?.maxInactiveTime != null &&
+ !Number.isSafeInteger(opts.maxInactiveTime)
+ ) {
+ throw new TypeError('maxInactiveTime option is not an integer')
+ }
+ if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) {
+ throw new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ }
+ if (opts?.killHandler != null && typeof opts.killHandler !== 'function') {
+ throw new TypeError('killHandler option is not a function')
+ }
+ if (opts?.async != null) {
+ throw new Error('async option is deprecated')
+ }
this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts }
- delete this.opts.async
}
private checkValidTaskFunction (
}
/**
- * Checks if the `taskFunctions` parameter is passed to the constructor.
+ * Checks if the `taskFunctions` parameter is passed to the constructor and valid.
*
* @param taskFunctions - The task function(s) parameter that should be checked.
*/
})
})
+ it('Verify that worker options are checked at worker creation', () => {
+ expect(() => new ClusterWorker(() => {}, '')).toThrowError(
+ new TypeError('opts worker options parameter is not a plain object')
+ )
+ expect(
+ () => new ClusterWorker(() => {}, { killBehavior: '' })
+ ).toThrowError(new TypeError("killBehavior option '' is not valid"))
+ expect(() => new ClusterWorker(() => {}, { killBehavior: 0 })).toThrowError(
+ new TypeError("killBehavior option '0' is not valid")
+ )
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: '' })
+ ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 0.5 })
+ ).toThrowError(new TypeError('maxInactiveTime option is not an integer'))
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 0 })
+ ).toThrowError(
+ new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ )
+ expect(
+ () => new ThreadWorker(() => {}, { maxInactiveTime: 4 })
+ ).toThrowError(
+ new TypeError(
+ 'maxInactiveTime option is not a positive integer greater or equal than 5'
+ )
+ )
+ expect(() => new ThreadWorker(() => {}, { killHandler: '' })).toThrowError(
+ new TypeError('killHandler option is not a function')
+ )
+ expect(() => new ThreadWorker(() => {}, { killHandler: 0 })).toThrowError(
+ new TypeError('killHandler option is not a function')
+ )
+ expect(() => new ThreadWorker(() => {}, { async: true })).toThrowError(
+ new TypeError('async option is deprecated')
+ )
+ })
+
it('Verify that worker options are set at worker creation', () => {
const killHandler = () => {
console.info('Worker received kill message')
const worker = new ClusterWorker(() => {}, {
killBehavior: KillBehaviors.HARD,
maxInactiveTime: 6000,
- killHandler,
- async: true
+ killHandler
})
expect(worker.opts).toStrictEqual({
killBehavior: KillBehaviors.HARD,
it('Verify that taskFunctions parameter is mandatory', () => {
expect(() => new ClusterWorker()).toThrowError(
- 'taskFunctions parameter is mandatory'
+ new Error('taskFunctions parameter is mandatory')
)
})