2016-07-20 11 views
0

これを読んだ人は、事前に感謝します。Agenda.jsジョブスケジューリング、ジョブの繰り返しとループ

特定の時刻にgcmメッセージ(通知)をクライアントIDのリストに送信できる必要があります。

私はAgenda.jsに永続性レイヤーがあるので、これを使用しようとしています。

次のコードは、最初はうまく動作していると思われます。しかし、しばらくしても何もせずにサーバーを冷やすと、ジョブはループで実行されます。

また

が含まれます "WARNING:過去の日付が解雇されることはありません。"

ここに関連コードがあります。

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

なぜこのようなことが起こっているのでしょうか?どのようにこの問題をデバッグするためのヒント?

ありがとうございます!

答えて

1

問題を修正しました。私はjob.remove関数に追加し、もはや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) {});  
} 
関連する問題