I need to run queries jobs at specific dates, After researching nodejs libraries for that node schedule was more recommended then node-cron because it handles better for one time jobs then for repetitive jobs. The problem is that sometimes the job is running like it should and sometimes it not running i'm not getting any error. Usually the cron jobs does not get executed if the selected date is longer then 2 hours and works properly if the selected date is 30 minutes or less.
I tried both node-cron and node schedule libraries, i added error handler to catch the error when cron job does not get executed but no error is thrown when the job is not getting executed.
route.js :
router.put('/preview', auth, async (req, res) => {
const auctionId = req.body.auctionId;
const auctionStartDate = req.body.auctionStartDate;
let bidEndDate = req.body.bidEndDate;
let auctionEndDate = req.body.auctionEndDate;
const chargeableWeight = req.body.chargeableWeight;
//create auction serial number if the auctions is new and not modified
await db.query(
`SELECT right(AuctionSerialNumber,length(AuctionSerialNumber)-1) as serialCount
FROM auctions
ORDER BY cast(right(AuctionSerialNumber,length(AuctionSerialNumber)-1) as unsigned) DESC
LIMIT 1`,
async (error2, results2, fields2) => {
if (error2) res.status(400).json({ errors: error2.message });
let serialCount = results2[0].serialCount;
let tempSerialNumber = parseInt(serialCount, 10);
let nextSerial = tempSerialNumber + 1;
nextSerial = 'A' + nextSerial;
await db.query(
`UPDATE fretit.auctions SET
fretit.auctions.AuctionEndDate = '${auctionEndDate}',
fretit.auctions.StartDate='${auctionStartDate}',
fretit.auctions.BidEndDate='${bidEndDate}',
fretit.auctions.AuctionSerialNumber='${nextSerial}',
fretit.auctions.AuctionState=2,
fretit.auctions.WinningBidID='000',
fretit.auctions.ChargeableWeight='${chargeableWeight}'
WHERE fretit.auctions.UID='${auctionId}'`,
(error, result, fields) => {
if (error) res.status(400).json({ errors: error.message });
}
);
//start bid end date timer
var j1 = schedule.scheduleJob(bidEndDate, async () => {
console.log('starting bid end date');
await db.query(
`SELECT auctions.AuctionState as auctionState
FROM auctions
WHERE auctions.UID='${auctionId}'
`,
async (error2, result2, fields2) => {
if (error2) res.status(400).json({ errors: error.message });
let auctionState = result2[0].auctionState;
console.log(auctionState);
//if auction state is LiveNoBids give notification and start auction end date
if (auctionState == 2) {
await db.query(
`UPDATE auctions SET
auctions.AuctionLostNotification=1,
auctions.AuctionState=4
WHERE auctions.UID='${auctionId}'
`,
(error, result, fields) => {
if (error) res.status(400).json({ errors: error.message });
var j3 = schedule.scheduleJob(auctionEndDate, async () => {
console.log(auctionEndDate);
console.log(
'this auction reached bidEndDate and does not have bids'
);
await db.query(
`SELECT auctions.AuctionState as auctionState
FROM auctions
WHERE auctions.UID='${auctionId}'
`,
async (error2, results2, fields2) => {
if (error2)
res.status(400).json({ errors: error.message });
let auctionState = results2[0].auctionState;
console.log(auctionState);
//if auction state is DueDateExceeded when auctionEndDate ends move auction state to Expired
if (auctionState == 4) {
console.log(auctionState);
await db.query(
`UPDATE auctions SET
auctions.AuctionState=7
WHERE auctions.UID='${auctionId}'
`,
(error3, result3, fields3) => {
if (error3)
res
.status(400)
.json({ errors: error.message });
}
);
}
}
);
});
}
);
//if auction state is LiveWithBids move auction state to DueDateExceeded
} else if (auctionState == 3) {
console.log(auctionState);
await db.query(
`UPDATE auctions SET
auctions.AuctionState=4,
auctions.AuctionWonNotification=1
WHERE auctions.UID='${auctionId}'
`,
(error, result, fields) => {
if (error) res.status(400).json({ errors: error.message });
//start auction end date timer
var j2 = schedule.scheduleJob(auctionEndDate, async () => {
console.log(auctionEndDate);
console.log(
'this auction reached bidEndDate and have bids'
);
await db.query(
`SELECT auctions.AuctionState as auctionState
FROM auctions
WHERE auctions.UID='${auctionId}'
`,
async (error2, result2, fields2) => {
if (error2)
res.status(400).json({ errors: error.message });
let auctionState = result2[0].auctionState;
console.log(auctionState);
//if auction state is DueDateExceeded when auctionEndDate ends move auction state to Expired
if (auctionState == 4) {
console.log(auctionState);
await db.query(
`UPDATE auctions SET
auctions.AuctionState=7
WHERE auctions.UID='${auctionId}'
`,
(error3, result3, fields3) => {
if (error3)
res
.status(400)
.json({ errors: error.message });
}
);
}
}
);
});
}
);
}
}
);
});
}
);
});
catchErrorAsync.js:
module.exports = function(handler) {
return async (req, res, next) => {
try {
await handler(req, res);
} catch (ex) {
next(ex);
}
};
};
error.js:
const winston = require('winston');
module.exports = function(err, req, res, next) {
// log error message to file
winston.error(err.message, err);
// Send a user-friendly message back to the user
res.status(500).send('Something went wrong...');
};
Why the cron jobs sometimes working and sometimes not? What is the correct library to use for this usage?