const { ClusterWorker } = require('../../../lib/index')
function yourFunction (data) {
- for (let i = 0; i <= 1000; i++) {
+ for (let i = 0; i < 1000; i++) {
const o = {
a: i
}
const { ThreadWorker } = require('../../../lib/index')
function yourFunction (data) {
- for (let i = 0; i <= 1000; i++) {
+ for (let i = 0; i < 1000; i++) {
const o = {
a: i
}
module.exports = function (data) {
if (data.taskType === 'CPU_INTENSIVE') {
// CPU intensive task
- for (let i = 0; i <= 5000; i++) {
+ for (let i = 0; i < 5000; i++) {
const o = {
a: i
}
MessageValue,
PromiseWorkerResponseWrapper
} from '../utility-types'
+import { EMPTY_FUNCTION } from '../utils'
import { isKillBehavior, KillBehaviors } from '../worker/worker-options'
import type { IPoolInternal } from './pool-internal'
import { PoolEmitter, PoolType } from './pool-internal'
WorkerChoiceStrategyContext
} from './selection-strategies'
-/**
- * An intentional empty function.
- */
-const EMPTY_FUNCTION: () => void = () => {
- /* Intentionally empty */
-}
-
/**
* Callback invoked if the worker raised an error.
*/
super(numberOfWorkers, filePath, opts)
}
+ /** @inheritdoc */
protected setupHook (): void {
setupMaster({
exec: this.filePath
})
}
+ /** @inheritdoc */
protected isMain (): boolean {
return isMaster
}
worker.kill()
}
+ /** @inheritdoc */
protected sendToWorker (worker: Worker, message: MessageValue<Data>): void {
worker.send(message)
}
worker.on('message', listener)
}
+ /** @inheritdoc */
protected createWorker (): Worker {
return fork(this.opts.env)
}
+ /** @inheritdoc */
protected afterWorkerSetup (worker: Worker): void {
// Listen worker messages.
this.registerWorkerMessageListener(worker, super.workerListener())
super(numberOfThreads, filePath, opts)
}
+ /** @inheritdoc */
protected isMain (): boolean {
return isMainThread
}
await worker.terminate()
}
+ /** @inheritdoc */
protected sendToWorker (
worker: ThreadWorkerWithMessageChannel,
message: MessageValue<Data>
messageChannel.port2?.on('message', listener)
}
+ /** @inheritdoc */
protected createWorker (): ThreadWorkerWithMessageChannel {
return new Worker(this.filePath, {
env: SHARE_ENV
})
}
+ /** @inheritdoc */
protected afterWorkerSetup (worker: ThreadWorkerWithMessageChannel): void {
const { port1, port2 } = new MessageChannel()
worker.postMessage({ parent: port1 }, [port1])
--- /dev/null
+/**
+ * An intentional empty function.
+ */
+export const EMPTY_FUNCTION: () => void = () => {
+ /* Intentionally empty */
+}
import type { Worker } from 'cluster'
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 { KillBehaviors } from './worker-options'
try {
const res = fn(value.data)
this.sendToMainWorker({ data: res, id: value.id })
- this.lastTask = Date.now()
} catch (e) {
const err = this.handleError(e)
this.sendToMainWorker({ error: err, id: value.id })
+ } finally {
this.lastTask = Date.now()
}
}
fn(value.data)
.then(res => {
this.sendToMainWorker({ data: res, id: value.id })
- this.lastTask = Date.now()
return null
})
.catch(e => {
const err = this.handleError(e)
this.sendToMainWorker({ error: err, id: value.id })
+ })
+ .finally(() => {
this.lastTask = Date.now()
})
+ .catch(EMPTY_FUNCTION)
}
}
super('worker-cluster-pool:pioardi', isMaster, fn, worker, opts)
}
+ /** @inheritdoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
this.getMainWorker().send(message)
}
+ /** @inheritdoc */
protected handleError (e: Error | string): string {
return e instanceof Error ? e.message : e
}
super('worker-thread-pool:pioardi', isMainThread, fn, parentPort, opts)
}
+ /** @inheritdoc */
protected sendToMainWorker (message: MessageValue<Response>): void {
this.getMainWorker().postMessage(message)
}
const { isMaster } = require('cluster')
function test (data) {
- for (let i = 0; i <= 50; i++) {
+ for (let i = 0; i < 50; i++) {
const o = {
a: i
}
const { isMainThread } = require('worker_threads')
function test (data) {
- for (let i = 0; i <= 50; i++) {
+ for (let i = 0; i < 50; i++) {
const o = {
a: i
}