我需要在特定日期运行查询作业,在研究 nodejs 库后,更推荐使用 node-cron,因为它对一次性作业的处理比重复性作业更好。问题是,有时作业按应有的方式运行,有时它没有运行,我没有收到任何错误。通常,如果所选日期长于 2 小时,则 cron 作业不会执行,如果所选日期为 30 分钟或更短,则可以正常工作。
我尝试了 node-cron 和 node schedule 库,我添加了错误处理程序以在 cron 作业未执行时捕获错误,但在作业未执行时未引发错误。
路由.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);
}
};
};
错误.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 作业有时有效,有时无效?用于此用途的正确库是什么?