running a cron in nodejs

0

I use the cron library to run the following snippet of code

pepinosModelo.find()
  .exec()
  .then(pepinos => {

    pepinos.forEach(pepino => {

      let frecuencia = pepino.frecuente();
      let nuevoTrans = [{
        chocolate:true,
        abena:"de dos a tres porcines",
        lsd: "No aplica"
      }];


      nuevoTrans.push({
        chocolate:false,
        coco:"Completo",
        lsd: "Aplica"
      });

      return TransModel.create(nuevoTrans)
      .then((trans) => {

        fumadorModelo
        .update({
          pepino: pepino._id
        }, { 
          'comprador': false,
          'nuevaDocis': zero 
        })
        .exec()
        .then((usuario) => { 
          pepino.cambio += frecuencia;
          pepino.balance = zero;
          pepino.puntos = zero;
          return pepino
          .save()
          .then(() => {
            console.log(´fin actualizacion usuario ciclo ${usuario}´);
          });
        });
      });
    });
  });

and to run it I do it this way

  try{
  // let when = '00 30 11 1-2 * *';
  // let timeZone = '';
  let when = '* 10 * * * *';

  new CronJob({
    cronTime: when,
    onTick: cronFn,
    start: true,
    // timeZone
  });

}catch(err) {
  console.error('something happening with cron:', err);
}
  • The issue is that I'm not so sure if you run the task several times or just once
  • Could this saturate the server depending on the number of documents in the cucumber collection?
  • What happens if you fail to update or create a document
asked by soldat25 31.01.2017 в 21:49
source

1 answer

0

According to your cron pattern, it runs every 30 minutes.

  • If you want to know how many times you run, add logs and check them.
  • I do not know what you are using to bring your documents, but I recommend you make a page so that it does not break, because if you bring 1 million records it is more likely to happen depending on the server
  • If the update or creation fails, I should throw you a warning by console since what I see is not using any catch

1.) First I recommend that you bring the content page

2.) You create a separate function with Promise to do the creation and the update and once it is finished do the resolve.

3.) Then I recommend that your for just do the inserts in nuevaTrans with the promise of that function.

4.) With that list do a Promise.all

I would seem much neater and more aligned to ES6

    
answered by 14.03.2017 в 19:09