我正在开发一个nodejs应用程序来调度多个cron作业。顺便说一下,当我试图取消工作时,我遇到了一个错误。
情况如下。
node-cron
或node-schedule
创建了多个cron作业。TypeError: testJob.destory is not a function
你能帮我解决这个问题吗?
cron模块/ cronManager.js
const cron = require("node-cron")
// cron jobs
let testJob1
let testJob2
let testJob3
async function startCronjobs(cronTimes) {
testJob1 = cron.schedule(cronTimes.testTime1, () => {
console.log("test 1 job")
}, {
scheduled: true,
timezone: "America/New_York"
})
testJob1.start()
testJob2 = cron.schedule(cronTimes.testTime2, () => {
console.log("test 2 job")
}, {
scheduled: true,
timezone: "America/New_York"
})
testJob2.start()
testJob3 = cron.schedule(cronTimes.testTime3, () => {
console.log("test 3 job")
}, {
scheduled: true,
timezone: "America/New_York"
})
testJob3.start()
}
async function destroyCronjobs() {
console.log("============= Destroy node-cron Jobs ================")
return new Promise((resolve, reject) => {
if(testJob1 !== undefined && testJob1 !== null) testJob1.destory()
if(testJob2 !== undefined && testJob2 !== null) testJob2.destory()
if(testJob3 !== undefined && testJob3 !== null) testJob3.destory()
})
}
module.exports.destroyJobs = destroyCronjobs
module.exports.startCronJobs = startCronjobs
脚本/ main.js
const cronManager = require("./cronManager")
const express = require("express")
const router = express.Router()
router.post("/start", wrapper(async (req, res) => {
await cronManager.startCronjobs()
}))
router.post("/destroy", wrapper(async (req, res) => {
await cronManager.destoryCronjobs()
}))
发布于 2019-03-06 22:56:37
您的代码中有一个错误错误,您有testJob1.destory()
,但是它应该是testJob.destroy()
销毁()将被停止并完全销毁计划的任务。
假设这是示例代码,这样它就缺少了cronManager.startCronjobs()
的一些参数,而且这个函数也没有返回任何promise
来使用await
。
https://stackoverflow.com/questions/55036349
复制