Schedule a job with node-schedule

Hi there, im new at nodejs and im trying to build a really simple twitter bot that tweets once a day.

I’ve tried to set a recurrent task with node-schedule this is my code:

var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(0, 7)];
rule.hour = 13;
rule.minute = 0;
 
var j = schedule.scheduleJob(rule, function(){
    console.log ('here i will post the tweet')
});

I’ve copied that code from the node-schedule documentation.

The first question is if i can do something like: set a range of dates like this (0,7) in order to tweet everyday.

The next question that i’ve is the following:

once the code is writed and “working” i’ve to run a command in the glitch console “node server” to run that process, the thing is that the scheduled task executes one time, but it is not recurrent.

How could i make that task recurrent in the glitch console?

Thanks in advance!

Agustín.

uptime robot, will fix the one time execution

1 Like

It helps prevent the going to sleep after 5 mins of inactivity, but doesn’t stop the forced shutdown that occurs at a minimum of every 12 hours.

The app will need to persist the schedule over a restart … save to a file or database on shutdown, and read back in on startup.

Edit - forgot to mention …

Place the command in a start script in package.json to have your app run automatically when the container restarts. You’ll also likely want a web listener to keep it running instead of exiting straight away.

The hello-express project has a decent example.

@mishavee, isn’t this how the start script should be?:

"scripts": {
    "start": "node server.js"
}

Hey Misave thanks a lot! i think i could figure that out based on your comment.

The thing was that my package.json have some errors, i’ve added a start as you told me and some other things from the hello-express project and now it seems to be working.

The uptime robot isnt working for me, it gives me an error Forbidden (403).

Also thanks to @khalby786!