gpt4 book ai didi

javascript - 销毁 Node cron 作业或取消 Node 调度程序作业

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:21 24 4
gpt4 key购买 nike

我正在开发一个调度多个 cron 作业的 Nodejs 应用程序。顺便说一句,当我尝试取消作业时出现错误。

情况如下。

  • 我使用 node-cronnode-schedule 创建了多个 cron 作业。
  • 一些作业的开始时间已经过去,然后我尝试使用脚本取消所有 cron 作业。
  • 我收到如下错误。类型错误:testJob.destory不是一个函数

你能帮我解决这个问题吗?

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()
}))

最佳答案

您的代码中有拼写错误,您有 testJob1.destory() 但它应该是 testJob.destroy()

destroy()将被停止并完全破坏计划任务。

假设这是示例代码,因此它缺少 cronManager.startCronjobs() 的一些参数,并且该函数不会返回任何使用 awaitpromise

关于javascript - 销毁 Node cron 作业或取消 Node 调度程序作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55036349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com