- `async` - true/false, true if your function contains async code pieces, else false
- `killBehavior` - Dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
**KillBehaviors.SOFT**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted.
- **KillBehaviors.HARD**: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
+ **KillBehaviors.HARD**: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
This option only apply to the newly created workers.
Default: `KillBehaviors.SOFT`
import type { MessagePort } from 'worker_threads'
import type { MessageValue } from '../utility-types'
import { EMPTY_FUNCTION } from '../utils'
-import type { KillBehavior, WorkerOptions } from './worker-options'
+import {
+ isKillBehavior,
+ type KillBehavior,
+ type WorkerOptions
+} from './worker-options'
import { KillBehaviors } from './worker-options'
const DEFAULT_MAX_INACTIVE_TIME = 60000
/**
* Timestamp of the last task processed by this worker.
*/
- protected lastTaskTimestamp: number
+ protected lastTaskTimestamp!: number
/**
* Handler Id of the `aliveInterval` worker alive check.
*/
this.opts = opts
this.checkFunctionInput(fn)
this.checkWorkerOptions(this.opts)
- this.lastTaskTimestamp = Date.now()
- // Keep the worker active
- if (!isMain) {
+ if (!isMain && isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior)) {
+ this.lastTaskTimestamp = Date.now()
this.aliveInterval = setInterval(
this.checkAlive.bind(this),
(this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
const err = this.handleError(e as Error)
this.sendToMainWorker({ error: err, id: value.id })
} finally {
- this.lastTaskTimestamp = Date.now()
+ isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) &&
+ (this.lastTaskTimestamp = Date.now())
}
}
this.sendToMainWorker({ error: err, id: value.id })
})
.finally(() => {
- this.lastTaskTimestamp = Date.now()
+ isKillBehavior(KillBehaviors.HARD, this.opts.killBehavior) &&
+ (this.lastTaskTimestamp = Date.now())
})
.catch(EMPTY_FUNCTION)
}
*/
SOFT: 'SOFT',
/**
- * If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
+ * If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
*/
HARD: 'HARD'
} as const)
* `killBehavior` dictates if your async unit (worker/process) will be deleted in case that a task is active on it.
*
* - SOFT: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker **won't** be deleted.
- * - HARD: If `lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
+ * - HARD: If `currentTime - lastActiveTime` is greater than `maxInactiveTime` but a task is still running, then the worker will be deleted.
*
* This option only apply to the newly created workers.
*