我正在使用 node-cron 在我的快速后端中进行调度,这是示例。 我在 index.js 中设置了调度配置,并开发了将由cron在 training.js
中执行的功能index.js :
const training = require('./training');
const DAILY = 'DAILY';
const HOURLY = 'HOURLY';
function getCronSchedule(when){
switch (when) {
case DAILY : return "55 22 * * *";
case HOURLY : return "10 * * * *";
}
}
function initJob()
{
training.initJob(getCronSchedule(HOURLY));
training.initJob(getCronSchedule(DAILY));
}
module.exports={
initJob
}
training.js :
function initJob(when)
{
console.log('This is daily scheduling');
console.log('This is hourly scheduling');
}
module.exports={
initJob
}
当前,
This is daily scheduling
This is hourly scheduling
由于每天和每小时计划打印一次,因此每天将打印两次。
我需要的是每天将它们打印一次。
这是每日计划安排,印刷在每日计划中,
这是每小时计划一次,印刷在每小时cron上。
我该怎么做?我不知道该怎么做,因为我从参数中得到的只是cron时间表。
答案 0 :(得分:0)
在node-cron示例中,您的代码应如下所示:
const cron = require('node-cron');
cron.schedule('55 22 * * *', () => {
console.log('This is daily scheduling');
});
cron.schedule('55 22 * * *', () => {
console.log('This is hourly scheduling');
});
答案 1 :(得分:0)
尝试下面的代码,希望对您有所帮助:
index.js
const training = require('./training');
const DAILY = 'DAILY';
const HOURLY = 'HOURLY';
function getCronSchedule(when){
switch (when) {
case DAILY : return "2 * * * *";
case HOURLY : return "* * * * *";
}
}
function initJob()
{
training.initJob(getCronSchedule(HOURLY),HOURLY);
training.initJob(getCronSchedule(DAILY),DAILY);
}
module.exports={
initJob
}
initJob()
training.js
var cron = require('node-cron');
const DAILY = 'DAILY';
const HOURLY = 'HOURLY';
function initJob(when, name)
{
switch(name){
case DAILY : cron.schedule(when, () => {
console.log('This is daily scheduling');
});
case HOURLY : cron.schedule(when, () => {
console.log('This is hourly scheduling');
});
}
}
module.exports={
initJob
}
希望这会有所帮助。