我正在开发可调度多个cron作业的nodejs应用程序。 顺便说一句,当我尝试取消作业时出现错误。
情况如下。
node-cron
或node-schedule
创建了多个cron作业。 TypeError: testJob.destory is not a function
您能帮我解决这个问题吗?
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
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()
}))
答案 0 :(得分:0)
您的代码中有错别字,您有[Code]
function MyTest(): Boolean;
Var
TmpFileName: String;
ExecStdout: AnsiString;
ResultCode: Integer;
begin
TmpFileName := 'C:\Users\My\Desktop\123.txt';
Exec('C:\Users\My\Desktop\devcon.exe', 'help find > "' + TmpFileName + '"', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode);
if LoadStringFromFile(TmpFileName, ExecStdout) then
begin
MsgBox(ExecStdout, mbInformation, MB_OK);
end;
//DeleteFile(TmpFileName);
Result := true;
end;
function InitializeSetup(): boolean;
begin
MyTest;
Result := true;
end;
,但应该是testJob1.destory()
destroy()将停止并完全破坏计划的任务。
假设这是示例代码,因此它缺少testJob.destroy()
的某些参数,并且此函数未返回任何cronManager.startCronjobs()
来使用promise
。