javascript – 如何在Node Agenda中以编程方式定义具有相同名称的多个作业

我正在使用节点议程模块来启动用户创建的各种作业/事件.我希望能够创建Jobs,使得所有这些都由一个函数回调处理,每个事件都根据事件参数进行区分.示例代码如下

var mongoConnectionString = "mongodb://127.0.0.1/agenda";
var Agenda = require('agenda');
var agenda = new Agenda({db: {address: mongoConnectionString}});

agenda.define('user defined event', function(job,done) {
    var eventParams = job.attrs.data;
    if(eventParams.params === "Test"){
        handleEvent1();
    } else if (eventParams.params === "Test 2") {
        handleEvent2();
    } else {
        handleEvent3();
    }

    done();
});

agenda.on('ready', function() {
  console.log("Ok Lets get start");
  agenda.start();
});

// some how we get our call back executed. Note that params is the unique to each job.
var userEvent = function(params) {
    // Handle a event which repeats every 10 secs
    agenda.every('10 seconds','user defined event',params);
}

使用此代码,更新mongodb中的作业而不是插入.有什么方法可以强制议程插入而不是更新?如果议程不可能,是否还有其他模块可以做到这一点?

提前感谢您宝贵的时间

解决方法:

这是因为:

Every creates a job of type single, which means that it will only create one job in the database, even if that line is run multiple times.

您需要使用agenda.create,job.repeatEveryjob.save创建具有相同名称的多个作业:

const job = agenda.create("user defined event", params);
job.repeatEvery("10 seconds");
job.save();
上一篇:Agenda for Mac(日历,任务管理,笔记)


下一篇:梦断代码读后感 (一)