Я занимаюсь разработкой приложения nodejs, которое планирует несколько заданий cron.Кстати, я получил ошибку при попытке отменить работу.
Ситуация как ниже.
- Я создал несколько заданий cron, используя
node-cron
или node-schedule
. - Время начала нескольких заданий уже прошло, затем я попытался отменить все задания cron, используя скрипт.
- Я получил ошибку, как показано ниже.
TypeError: testJob.destory is not a function
Не могли бы вы помочь мне решить эту проблему?
cron module / 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
script / 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()
}))