2016-07-20 16 views
0

Vielen Dank im Voraus für jeden, der dies liest.Agenda.js Jobplanung, Jobs wiederholen und Schleife

Ich muss gcm Nachrichten (Benachrichtigungen) zu einer bestimmten Zeit an eine Liste von Client-IDs senden können.

Ich versuche, Agenda.js zu verwenden, da es eine Persistenzschicht hat.

Der folgende Code scheint zunächst gut zu funktionieren, genau ausgeführt, wenn es soll. Aber nach einer Weile, in der der Server einfach nichts tun kann, wird der Job in einer Schleife ausgeführt.

Es wird auch

umfassen "WARNUNG:.. Datum in Vergangenheit wird nie gefeuert werden"

Hier ist der entsprechende Code.

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) {});  
} 

Hat jemand eine Vorstellung davon, warum das passieren könnte? Irgendwelche Tipps zum Debuggen dieses Problems?

Danke!

Antwort

1

Ich habe das Problem behoben. Ich habe in der job.remove-Funktion hinzugefügt und es nicht mehr spazzes.

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) {});  
}