I'm creating a NodeJs application that sends alerts to a user's email ID when the price of the bitcoin goes above the price specified by the user. For scheduling tasks, I'm using cron. Also, I've used the bull as a message broker. When I'm running this program, It isn't working and it isn't sending emails when the price is above the specified price. Please help me to find out what is the problem.
require('dotenv').config({path: require("find-config")(".env")});
const CronJob = require("cron").CronJob;
let Queue = require("bull");
const Alert = require("../models/alert");
const { currentPrice } = require("../utilities/currentPrice");
const { sendEmail } = require("../utilities/sendEmailNotification");
//Creating a Queue
let alertQueue = new Queue("alerts", process.env.RedisURL);
//Consumer Process
alertQueue.process(async function(job, done) {
const {mailTo, title, text} = job.data;
const mailObj = {
from: process.env.SendGridForm,
recipients: mailTo,
subject: title,
message: text
}
const response = sendEmail(mailObj);
if(response.error) {
done(new Error("Error Sending Alert!!!"));
}
done();
})
let sendAlert = new CronJob("*/25 * * * * *", async function () {
let priceObj = await currentPrice();
if (priceObj.error)
return;
let price = priceObj.data;
const alerts = await Alert.find({status: "Created"});
alerts.forEach((alert) => {
if(alert.price <= price) {
mailTo = alert.email;
title = `Bitcoint is UP!`;
text = `Price of Bitcoin has just exceeded your alert price of ${alert.price} USD. Current price is ${price} USD.`;
alertQueue.add(
{mailTo, title, text},
{
attempts: 3,
backoff: 3000
}
)
alert.status = "Triggered";
alert.save();
}
})
});
sendAlert.start();