首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Agenda.js作业调度、作业重复和循环

Agenda.js作业调度、作业重复和循环
EN

Stack Overflow用户
提问于 2016-07-20 22:37:24
回答 1查看 1.4K关注 0票数 0

提前感谢阅读这篇文章的人。

我需要能够在特定时间向客户端ID列表发送gcm消息(通知)。

我正在尝试使用Agenda.js,因为它有一个持久层。

下面的代码一开始似乎工作得很好,完全在它应该执行的时候执行。但是,在让服务器不做任何事情的一段时间后,作业将开始循环执行。

它还将包括

“警告:日期在过去。永远不会被解雇。”

以下是相关代码。

代码语言:javascript
复制
var agenda = new agenda({db: {address: configParams.db}});

schedule_notifications = function(req) {

// define an agenda task named notify
    agenda.define('notify', function(job, done) {

     // create a gcm message
        var message = new gcm.Message({
            notification: { "body": 'test' }
        });
        var sender = new gcm.Sender('server id');
        var regTokens = ['phone id'];

        // send the message
        sender.send(message, { registrationTokens: regTokens }, function(err, response) {
            if (err) console.error(err);
            else console.log(response);
            done();
        });
    });


    // get the object from the request
    var req_json = JSON.parse(req.body.data),
        keys = Object.keys(req_json),
        key_string = keys[0],
        start_obj = new Date(req_json[key_string][0].start);

    // schedule the job with the date object found in the request
    // start_obj, for example could be made using
    // start_obj = new Date();
    // notify is the name of the job to run

    agenda.schedule(start_obj, 'notify');
    agenda.start();

// can comment agenda.schedule and uncomment the following line to delete the unfinished jobs in db
// agenda.purge(function(err, numRemoved) {});    
}

有人知道为什么会发生这样的事情吗?有什么关于如何调试这个问题的提示吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-07-21 01:01:40

我解决了这个问题。我添加了job.remove函数,它不再有效。

代码语言:javascript
复制
var agenda = new agenda({db: {address: configParams.db}});

schedule_notifications = function(req) {

// define an agenda task named notify
    agenda.define('notify', function(job, done) {

     // create a gcm message
        var message = new gcm.Message({
            notification: { "body": 'test' }
        });
        var sender = new gcm.Sender('server id');
        var regTokens = ['phone id'];

        // send the message
        sender.send(message, { registrationTokens: regTokens }, function(err, response) {
            if (err) console.error(err);
            else console.log(response);
            done();
        });
    job.remove(function(err) {
      if(!err) console.log("Successfully removed job from collection");
    })
    });


    // get the object from the request
    var req_json = JSON.parse(req.body.data),
        keys = Object.keys(req_json),
        key_string = keys[0],
        start_obj = new Date(req_json[key_string][0].start);

    // schedule the job with the date object found in the request
    // start_obj, for example could be made using
    // start_obj = new Date();
    // notify is the name of the job to run

    agenda.schedule(start_obj, 'notify');
    agenda.start();

// can comment agenda.schedule and uncomment the following line to delete the unfinished jobs in db
// agenda.purge(function(err, numRemoved) {});    
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38484273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档