2022-03-20 02:13:54 +01:00
|
|
|
import workerFarm from "worker-farm"
|
|
|
|
import * as env from "../environment"
|
2021-11-11 13:11:09 +01:00
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export const ThreadType = {
|
2021-11-11 13:11:09 +01:00
|
|
|
QUERY: "query",
|
|
|
|
AUTOMATION: "automation",
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
function typeToFile(type: any) {
|
2021-11-11 13:11:09 +01:00
|
|
|
let filename = null
|
|
|
|
switch (type) {
|
|
|
|
case ThreadType.QUERY:
|
|
|
|
filename = "./query"
|
|
|
|
break
|
|
|
|
case ThreadType.AUTOMATION:
|
|
|
|
filename = "./automation"
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
throw "Unknown thread type"
|
|
|
|
}
|
|
|
|
return require.resolve(filename)
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
export class Thread {
|
|
|
|
type: any
|
|
|
|
count: any
|
|
|
|
disableThreading: any
|
|
|
|
workers: any
|
2022-04-26 11:21:45 +02:00
|
|
|
timeoutMs: any
|
2022-03-20 02:13:54 +01:00
|
|
|
|
|
|
|
constructor(type: any, opts: any = { timeoutMs: null, count: 1 }) {
|
2021-11-11 17:20:30 +01:00
|
|
|
this.type = type
|
2021-11-22 18:42:41 +01:00
|
|
|
this.count = opts.count ? opts.count : 1
|
|
|
|
this.disableThreading =
|
|
|
|
env.isTest() ||
|
|
|
|
env.DISABLE_THREADING ||
|
|
|
|
this.count === 0 ||
|
|
|
|
env.isInThread()
|
|
|
|
if (!this.disableThreading) {
|
2022-03-20 02:13:54 +01:00
|
|
|
const workerOpts: any = {
|
2021-11-11 17:20:30 +01:00
|
|
|
autoStart: true,
|
2021-11-22 18:42:41 +01:00
|
|
|
maxConcurrentWorkers: this.count,
|
2021-11-11 17:20:30 +01:00
|
|
|
}
|
|
|
|
if (opts.timeoutMs) {
|
2022-04-14 18:28:14 +02:00
|
|
|
this.timeoutMs = opts.timeoutMs
|
2021-11-11 17:20:30 +01:00
|
|
|
workerOpts.maxCallTime = opts.timeoutMs
|
|
|
|
}
|
|
|
|
this.workers = workerFarm(workerOpts, typeToFile(type))
|
2021-11-11 13:11:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 02:13:54 +01:00
|
|
|
run(data: any) {
|
2021-11-11 13:11:09 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-11-11 17:20:30 +01:00
|
|
|
let fncToCall
|
|
|
|
// if in test then don't use threading
|
2021-11-22 18:42:41 +01:00
|
|
|
if (this.disableThreading) {
|
2021-11-11 17:20:30 +01:00
|
|
|
fncToCall = require(typeToFile(this.type))
|
|
|
|
} else {
|
|
|
|
fncToCall = this.workers
|
|
|
|
}
|
2022-03-20 02:13:54 +01:00
|
|
|
fncToCall(data, (err: any, response: any) => {
|
2022-04-14 18:28:14 +02:00
|
|
|
if (err && err.type === "TimeoutError") {
|
|
|
|
reject(
|
2022-04-26 11:21:45 +02:00
|
|
|
new Error(
|
|
|
|
`Query response time exceeded ${this.timeoutMs}ms timeout.`
|
|
|
|
)
|
2022-04-14 18:28:14 +02:00
|
|
|
)
|
|
|
|
} else if (err) {
|
2021-11-11 13:11:09 +01:00
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(response)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|