0

Changed from active to waiting jobs

This commit is contained in:
rafaelsideguide 2024-04-23 16:07:22 -03:00
parent a680c7ce84
commit 9b01dc6281

View File

@ -90,14 +90,14 @@ app.get(`/admin/${process.env.BULL_AUTH_KEY}/queues`, async (req, res) => {
app.get(`/serverHealthCheck`, async (req, res) => { app.get(`/serverHealthCheck`, async (req, res) => {
try { try {
const webScraperQueue = getWebScraperQueue(); const webScraperQueue = getWebScraperQueue();
const [activeJobs] = await Promise.all([ const [waitingJobs] = await Promise.all([
webScraperQueue.getActiveCount(), webScraperQueue.getWaitingCount(),
]); ]);
const noActiveJobs = activeJobs === 0; const noWaitingJobs = waitingJobs === 0;
// 200 if no active jobs, 503 if there are active jobs // 200 if no active jobs, 503 if there are active jobs
return res.status(noActiveJobs ? 200 : 500).json({ return res.status(noWaitingJobs ? 200 : 500).json({
activeJobs, waitingJobs,
}); });
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@ -107,30 +107,31 @@ app.get(`/serverHealthCheck`, async (req, res) => {
app.get('/serverHealthCheck/notify', async (req, res) => { app.get('/serverHealthCheck/notify', async (req, res) => {
if (process.env.SLACK_WEBHOOK_URL) { if (process.env.SLACK_WEBHOOK_URL) {
const treshold = 5; // The treshold value for the active jobs const treshold = 1; // The treshold value for the active jobs
const timeout = 60000; // 1 minute // The timeout value for the check in milliseconds const timeout = 60000; // 1 minute // The timeout value for the check in milliseconds
const getActiveJobs = async () => { const getWaitingJobsCount = async () => {
const webScraperQueue = getWebScraperQueue(); const webScraperQueue = getWebScraperQueue();
const [activeJobs] = await Promise.all([ const [waitingJobsCount] = await Promise.all([
webScraperQueue.getActiveCount(), webScraperQueue.getWaitingCount(),
]); ]);
return activeJobs; return waitingJobsCount;
}; };
res.status(200).json({ message: "Check initiated" }); res.status(200).json({ message: "Check initiated" });
const checkActiveJobs = async () => { const checkWaitingJobs = async () => {
try { try {
let activeJobs = await getActiveJobs(); let waitingJobsCount = await getWaitingJobsCount();
if (activeJobs >= treshold) { if (waitingJobsCount >= treshold) {
setTimeout(async () => { setTimeout(async () => {
activeJobs = await getActiveJobs(); // Re-check the active jobs count // Re-check the waiting jobs count after the timeout
if (activeJobs >= treshold) { waitingJobsCount = await getWaitingJobsCount();
if (waitingJobsCount >= treshold) {
const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL; const slackWebhookUrl = process.env.SLACK_WEBHOOK_URL;
const message = { const message = {
text: `⚠️ Warning: The number of active jobs (${activeJobs}) has exceeded the threshold (${treshold}) for more than ${timeout/60000} minute(s).`, text: `⚠️ Warning: The number of active jobs (${waitingJobsCount}) has exceeded the threshold (${treshold}) for more than ${timeout/60000} minute(s).`,
}; };
const response = await fetch(slackWebhookUrl, { const response = await fetch(slackWebhookUrl, {
@ -152,7 +153,7 @@ app.get('/serverHealthCheck/notify', async (req, res) => {
} }
}; };
checkActiveJobs(); checkWaitingJobs();
} }
}); });