So I created a simple express server that uses bull and Redis to offload tasks and run them even if the user leaves the page.
The api works perfectly on my machine but when i upload it to render (with paid plan) it does not work and i sometimes get this error in the logs:
Error adding job to queue: MaxRetriesPerRequestError: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
I created a smaller api to check the error and it behaves the same as the big api, and this is the code
const express = require('express'); // Import Express
const Queue = require('bull'); // Import Bull
const redis = require('redis'); // Import Redis
// Create Redis client
const redisClient = redis.createClient({
host: '',
port: '',
});
redisClient.on('error', (err) => {
console.error('Redis error:', err);
});
// Initialize Bull queue
const queue = new Queue('jobQueue', {
redis: {
config here
},
});
let jobResults = []; // Array to store results of jobs
// Function to execute a random task and store the result
const executeRandomFunction = () => {
const results = [];
const functions = [
() => results.push("Random number: " + Math.random()),
() => results.push("Current time: " + new Date().toLocaleTimeString()),
() => results.push("Random boolean: " + (Math.random() > 0.5)),
];
// Pick a random function and execute it
const randomFunc = functions[Math.floor(Math.random() * functions.length)];
randomFunc();
return results.join(' | ');
};
// Define the job processor
queue.process('extractTextJob', async (job) => {
console.log(`Processing job ${job.id}`);
const result = executeRandomFunction();
jobResults.push(result); // Store result in the array
return Promise.resolve();
});
// Add a job to the queue every 5 seconds
setInterval(async () => {
await queue.add('extractTextJob',{}); // You can pass data to jobs if needed
console.log('Job added to queue');
}, 5000);
console.log('Queue and processor set up. Jobs will be added every 5 seconds.');
// Set up Express server
const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());
// Route to get job results
app.get('/results', (req, res) => {
res.json({ results: jobResults });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
Does anyone have any ideas why it does not work?