我需要在特定的日期运行查询作业,因此,建议先研究针对该节点计划的nodejs库,然后再选择node-cron,因为它对一次性作业的处理要好于对重复作业的处理。 问题是,有时作业正在按预期方式运行,而有时却未运行,但我没有收到任何错误。 通常,如果选择的日期长于2个小时,则cron作业将不会执行,如果选择的日期是30分钟或更短,则cron作业将正常工作。
我尝试了node-cron和node Schedule库,我添加了错误处理程序以捕获cron作业未执行时捕获的错误,但未执行cron作业时不会引发错误。
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...');
};
为什么Cron工作有时工作而有时却不工作? 用于此用法的正确库是什么?