Scheduled tasks in Node.js

1

They asked me to design a script in nodejs which, when executed, performs tasks on a windows server. The problem is that I need to execute this task at a specific time of day so I thought about using the administrator windows tasks.

After defining the task I give it to execute manually but my script apparently does not start. Does anyone know how to fix it?

    
asked by Christian Lopez 08.06.2016 в 16:02
source

3 answers

3

You do not need to use any external program to run your script. You can use node-cron and put the code in a callback. It is extremely simple to use but you must learn the patterns cron

* * * * *

They mean

minutos(0-59)
horas(0-23)
día del mes(0-31)
mes(0-12 o nombres)
día de la semana (0-7, 7 es Domingo, o nombres)

Your code would be something like

var CronJob = require('cron').CronJob;
// Patrón de cron
// Corre todos los lunes a la 1:00 PM
new CronJob('0 13 * * mon', function() {
  // Código a ejecutar
}, function() {
  // Código a ejecutar cuando la tarea termina. 
  // Puedes pasar null para que no haga nada
}, true);

The last argument indicates "start immediately".

You can see the full list of arguments in the API

documentation     
answered by 08.06.2016 / 16:30
source
1

We are going to an easy solution. Run a task (which is inside a function) that runs on a specific date and time:

"use strict";

function tarea(){
    console.log('acá va la tarea', new Date());
}

function lanzarElDia(momento, tarea){
    console.log('lanzado',new Date());
    console.log('para ser ejecutado en',momento);
    setTimeout(tarea, momento.getTime()-(new Date()).getTime());
}

lanzarElDia(new Date('2016-06-10 20:29'), tarea);

Run every day at a certain time

If instead we want to execute a task every day at 21:10 we must calculate how many milliseconds are missing for that moment:

"use strict";

function tarea(){
    console.log('acá va la tarea', new Date());
}

function lanzarSiempreALaHora(hora, minutos, tarea){
    var ahora = new Date();
    console.log('lanzado',ahora);
    var momento = new Date(ahora.getFullYear(), ahora.getMonth(), ahora.getDate(), hora, minutos);
    if(momento<=ahora){ // la hora era anterior a la hora actual, debo sumar un día
        momento = new Date(momento.getTime()+1000*60*60*24);
    }
    console.log('para ser ejecutado en',momento);
    setTimeout(function(){
        tarea();
        lanzarSiempreALaHora(hora,minutos,tarea);
    },momento.getTime()-ahora.getTime());
}

lanzarSiempreALaHora(21,10, tarea);

When the date of the machine is changed

The problem is when you change the date or time of the machine. To do this, you should have a timer that controls every minute (or every x seconds, according to the precision you want) if you have reached the desired time.

Attention

There are no guarantees (with setTimeout or with any cron) that something will be executed exactly on a schedule, the only thing that is known is that it will be executed after the appointed time and as soon as possible close to that time.

    
answered by 11.06.2016 в 02:17
-1

You can create a Windows scheduled task that calls a .bat file that contains the node program call.

    
answered by 14.12.2017 в 13:58