autosql
Version:
An auto-parser of JSON into SQL.
43 lines (42 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const worker_threads_1 = require("worker_threads");
const path_1 = require("path");
class WorkerPool {
dbConfig;
workers = [];
queue = [];
workerFile;
constructor(size, dbConfig) {
this.dbConfig = dbConfig;
// Ensure the correct worker file path
this.workerFile = (0, path_1.resolve)(__dirname, "worker.js"); // Match compiled file
for (let i = 0; i < size; i++) {
// ✅ Pass `workerData` to each worker when created
const worker = new worker_threads_1.Worker(this.workerFile, {
workerData: { dbConfig } // Ensure each worker starts with dbConfig
});
worker.on("message", (msg) => {
const queuedItem = this.queue.shift();
if (queuedItem)
queuedItem.resolve(msg);
});
this.workers.push(worker);
}
}
runTask(method, params) {
return new Promise((resolve) => {
this.queue.push({ resolve, task: { method, params } });
const worker = this.workers.pop();
if (worker) {
// ✅ Send `method` and `params` to the worker
worker.postMessage({ method, params });
this.workers.push(worker);
}
});
}
close() {
this.workers.forEach((worker) => worker.terminate());
}
}
exports.default = WorkerPool;