request queue process modified

main
Bukxara 2 weeks ago
parent 652c7e3609
commit 1d8b875bf3

@ -1,5 +1,5 @@
import axios from "axios"; import axios from "axios";
import express, { json, query } from "express"; import express, { json } from "express";
import Queue from "bull"; import Queue from "bull";
import Bottleneck from "bottleneck"; import Bottleneck from "bottleneck";
@ -8,9 +8,9 @@ app.use(json());
const requestQueue = new Queue("request-queue", { const requestQueue = new Queue("request-queue", {
redis: { redis: {
host: '127.0.0.1', host: "127.0.0.1",
port: 6379 port: 6379,
} },
}); });
const limiter = new Bottleneck({ const limiter = new Bottleneck({
@ -41,8 +41,7 @@ async function sendRequest(data) {
requestQueue.process(async (job) => { requestQueue.process(async (job) => {
try { try {
const result = await limiter.schedule(() => sendRequest(job.data)); await limiter.schedule(() => sendRequest(job.data));
return result;
} catch (error) { } catch (error) {
throw new Error(`Request failed: ${error.message}`); throw new Error(`Request failed: ${error.message}`);
} }
@ -89,7 +88,6 @@ app.get("/bot:token/*", async (req, res) => {
}); });
app.post("/bot:token/*", async (req, res) => { app.post("/bot:token/*", async (req, res) => {
const url = req.url; const url = req.url;
const botTokenEndIndex = url.indexOf("/", 1); const botTokenEndIndex = url.indexOf("/", 1);
const botToken = url.substring(4, botTokenEndIndex); const botToken = url.substring(4, botTokenEndIndex);
@ -112,7 +110,6 @@ app.post("/bot:token/*", async (req, res) => {
} }
}); });
app.listen(3000, () => { app.listen(3000, () => {
console.log("Microservice listening on port 3000"); console.log("Microservice listening on port 3000");
}); });

@ -15,4 +15,4 @@
"express": "^4.19.2" "express": "^4.19.2"
}, },
"type": "module" "type": "module"
} }
Loading…
Cancel
Save