I am currently trying to call an api from workers in bullmq.
The api is working correctly from other paths.
Though it is not working from inside WorkerHandler.
It simply says fetch failed!
Sending request to writer API at https://localhost:3000/api/writer
articleWorkerHandler file
export const articleWorkerHandler = async (job) => {
let { articleId, allParams, } = job.data;
console.info(`I am processing the job with id ${job.id} & articleId ${articleId}`);
const siteHomepageUrl = process.env.NEXT_PUBLIC_SITE_HOMEPAGE_URL;
let writerAPILocation = siteHomepageUrl + '/api/writer';
console.log(`Sending request to writer API at ${writerAPILocation}`);
const response = await fetch(writerAPILocation, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(allParams),
cache: 'no-store'
});
if (response.ok) {
const data = await response.json();
console.log(`Article ${articleId} has been generated successfully`);
}
else {
console.error(`Error generating article ${articleId}`);
}
return job.data;
};
Instrumentation file
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { Worker } = await import('bullmq');
const { redisConnection } = await import('./lib/Jobs/Redis/redis');
const { Worker } = await import('bullmq');
const { redisConnection } = await import('./lib/Jobs/Redis/redis');
const { articleWorkerHandler } = await import('./lib/Jobs/Workers/articleWorkerHandler');
const articleWorkerOptions = {
connection: redisConnection,
concurrency: 1,
removeOnComplete: { count: 100 },
removeOnFail: { count: 500 },
}; const articleWorker = new Worker('articleQueue', articleWorkerHandler, articleWorkerOptions);
// Handle completion and errors for articleWorker
articleWorker.on('completed', (job) => {
console.log(`articleWorkerHandler Job completed with result ${job.id}`);
});
articleWorker.on('failed', (job, err) => {
console.error(`articleWorkerHandler Job failed with error ${err} for ${job.id}`);
});
articlequeue file
const articleQueue = new Queue('articleQueue', {
connection: redisConnection,
defaultJobOptions: {
attempts: 3,
backoff: {
type: 'exponential',
delay: 5000,
},
},
});
export { articleQueue };
Anyone have any experience with it? Thanks