Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
schedule route or controller

i have a controller

exports.updateDaily = async (req, res) => {
  try {
    const updateDaily = await transaction.decrement(
      {
        remainActive: 1,
      },
      {
        where: {
          remainActive: { [Op.gte]: 1 },
        },
      }
    );
    console.log(updateDaily);
    res.status(200).send({
      status: "Success",
    });
  } catch (error) {
    console.log(error);
    res.status(400).send({
      status: "Failed",
    });
  }
};

and route like this

router.patch('/updateDaily', updateDaily)

Using cron-node

let job = new cron.schedule('2 * * * *', () => {
    router.patch('/updateDaily', updateDaily);
});
job.start()

Using setIntverval

const scheduller = () => {
     return router.patch('/updateDaily', updateDaily);
}
setInterval(scheduller,600000)

how can i make one of them run every 10 minutes? i already try it with node-cron or setInterval but there is nothing happen

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You got the concept wrongly. Having a scheduled job means running something periodically which has to be a function that does something. You don't need a router defined in it. Your code does only one thing - schedules a router to be available for the user once the scheduled time happens and redefines it every 10 minutes.

What you need is to create a separate function for the operation and call it on a scheduled time and having a router at this point is extra unless you want to run it when the user sends a request as well.

const myDailyTask = async () => {
  await transaction.decrement(
    {
      remainActive: 1,
    },
    {
      where: {
        remainActive: { [Op.gte]: 1 },
      },
    }
  );
};

const id = setInterval(myDailyTask, 600_000);

It should work for the cron job the same way

const job = new cron.schedule('2 * * * *', () => {
    myDailyTask();
});
job.start();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda