提前感谢阅读这篇文章的人。
我需要能够在特定时间向客户端ID列表发送gcm消息(通知)。
我正在尝试使用Agenda.js,因为它有一个持久层。
下面的代码一开始似乎工作得很好,完全在它应该执行的时候执行。但是,在让服务器不做任何事情的一段时间后,作业将开始循环执行。
它还将包括
“警告:日期在过去。永远不会被解雇。”
以下是相关代码。
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) {});
}有人知道为什么会发生这样的事情吗?有什么关于如何调试这个问题的提示吗?
谢谢!
发布于 2016-07-21 01:01:40
我解决了这个问题。我添加了job.remove函数,它不再有效。
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) {});
}https://stackoverflow.com/questions/38484273
复制相似问题