node-downloader-manager
Version:
node-downloader-manager is a simple yet powerful package manager-like download manager built with NodeJs. It allows you to download files sequentially or with a queue-based approach, handling retries and concurrency limits efficiently.
76 lines (75 loc) • 2.84 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_worker_threads_1 = require("node:worker_threads");
const node_path_1 = __importDefault(require("node:path"));
class Thread {
workerPool = [];
maxWorkers;
scriptPath;
log;
activeWorkers = 0;
constructor(maxWorkers, consoleLog = false, scriptPath = node_path_1.default.join(__dirname, "../scripts/worker-script.js")) {
this.scriptPath = scriptPath;
this.maxWorkers = maxWorkers;
this.log = consoleLog;
}
logger(message, type = "info") {
if (this.log || type === "error") {
console[type](`[${new Date().toLocaleString()}] ${message}`);
}
}
createWorker(task) {
return new Promise((resolve, reject) => {
try {
const worker = new node_worker_threads_1.Worker(this.scriptPath, {
workerData: task,
});
this.activeWorkers++;
worker.on("message", (message) => {
this.logger(`Task ${task.id} completed successfully.`, "info");
this.activeWorkers--;
resolve(message);
this.runNext();
});
worker.on("error", (error) => {
this.logger(`Task ${task.id} failed: ${error.message}`, "error");
this.activeWorkers--;
reject(error);
this.runNext();
});
worker.on("exit", (code) => {
if (code !== 0) {
this.logger(`Task ${task.id} exited with code ${code}`, "error");
this.activeWorkers--;
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
this.workerPool.push(worker);
}
catch (error) {
this.logger(`Getting error while creating worker thread. ${error.message}`);
}
});
}
async runThreadTask(task) {
while (this.activeWorkers >= this.maxWorkers) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
return this.createWorker(task);
}
runNext() {
if (this.activeWorkers < this.maxWorkers) {
this.logger("Worker available for the next task.", "info");
}
}
terminateAll() {
this.workerPool.forEach((worker) => worker.terminate());
this.workerPool.length = 0;
this.activeWorkers = 0;
this.logger("All workers terminated.", "info");
}
}
exports.default = Thread;
;