node-cron is not working on my server - node.js

So I'm using node-cron module.
I'm using this simple code and it should print go! everyday at 14:17.
I'm running the code in my local machine, everythings going well. But then I try to run the code on my server in DigitalOcean (Ubuntu 14), it never prints go! at 14:17. Nothing happen. Can you tell me what I've been missing?
var cron = require('node-cron');
var task = cron.schedule('0 17 14 * 1-12 0-7', function() {
console.log('go!');
}, false);
task.start();
Oh, by the way I tried running another code on my server and it works, print go! at 0 second
var cron = require('node-cron');
var task = cron.schedule('0 * * * 1-12 0-7', function() {
console.log('go!');
}, false);
task.start();

Check your server timezone and your local timezone and see if they match.
Reconfigure your server timezone with sudo dpkg-reconfigure tzdata

Related

How can I replace node-cron with a command line script in a node.js application

I would like to change the implementation shown below to using a command line script in a node.js application. How do I do it?
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
getBananas();
});

How to run node.js code on the server automatically?

I am using a node package(sitemap-generator) to create a sitemap.xml file for my angular website,
At the moment I am running this code manually on my machine then uploading the generated file each time.
Is there any way I can automate this process, maybe by uploading the code and running it periodically and automatically
You can use node-scehdule module to run sitemap generator everyday at a specific time , you can set time according to your requirement
const SitemapGenerator = require('sitemap-generator');
const cron = require('node-schedule');
const generator = SitemapGenerator('https://examaple.com', {
maxDepth: 0,
filepath: '/var/www/example.com/sitemap.xml',
maxEntriesPerFile: 50000,
stripQuerystring: true,
lastMod:true
});
cron.schedule('0 22 * * *', () => {
console.log("will run at 10:00 PM everyday");
generator.start();
});

NodeJS Dynamic Service Loader & Runner

Context:
I'm trying to build a few slack hooks / notification services for a channel I'm active on, being the cheap-skate finance savvy person that I am, I'd like to make use of a free service as such (trail Heroku accounts, or similar products), thus I'd like to be able to run multiple services on a single instance.
I've created a generic runner, that should be based on a config and be able to pick up some node modules and supply them the config settings.
I'm using the following node_modules:
"auto-loader": "^0.2.0",
"node-cron": "^1.2.0",
"read-yaml": "^1.1.0"
config.yml
foo:
cron: 1 * * * *
url: http://www.foo.com
team:
-
slackId: bar
bnetId: 1
eloId: 1
-
slackId: baz
bnetId: 2
eloId: 2
app.js
const autoLoader = require('auto-loader');
const readYaml = require('read-yaml');
const cron = require('node-cron');
const services = autoLoader.load(__dirname +'/services')
readYaml('config.yml', function(err, conf) {
if (err) throw err;
Object.keys(conf).forEach(function (key) {
console.log('Creating CRON for ' + key);
if(cron.validate(conf[key].cron)) {
console.log(conf[key].cron, '-> is valid cron');
// the cron task below does not seem to fire ever
cron.schedule(conf[key].cron, function(){
services[key](conf[key]);
});
} else {
console.log('Cron invalid for::' + key)
}
});
});
service/foo.js
module.exports = function (config) {
console.log('foo entered!!');
console.log(config)
}
Question:
What am I missing? If I remove the cron schedule, my services get hit, thus my assumptions are as follows...
Either I'm missing something conceptually about how long running process are meant to work in NodeJS (as this is my first), or I'm missing a super (not obvious) to me bug.
How do you create a long running task/process in NodeJS with separate scheduled code sections / tasks?
The code itself works as expected. The issue appears to be with the configuration of the cron.
cron: 1 * * * * will run at 1 minute past the hour.
cron: "*/1 * * * *" # would run it every minute
cron: "*/5 * * * *" # would run it every 5 minutes
cron: "*/10 * * * * *" # would run every 10 seconds

Node JS: node-shedule parser bug

This code does not work:
var schedule = require('node-schedule');
var j = schedule.scheduleJob('0,30 7-21 * * *', function(){
console.log(Date.now());
});
If I replace 7-21 with * it starts working. Why and how can I make it work from 7am to 9 pm?
Define your package.json file
run the follwoing commands.
npm install
npm install node-schedule
node <your js file/app.js>
(your sample code above works as expected)

How to use Hubot and node-cron with the IRC adapter

I've been unable to properly setup Hubot and node-cron to execute tasks within my IRC channels.
This page shows how I initially setup my code:
https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-periodic-task-execution
Here is the link to node-cron:
https://github.com/ncb000gt/node-cron
I'm thinking I'm running into an issue with Hubot's IRC adapter, but I'm not sure. Any advice and code examples would be welcome.
Here is where I've ended up in testing:
module.exports = (robot) ->
cronJob = require('cron').CronJob
tz = 'America/Minneapolis'
new cronJob('30 * * * * *', testFunction, true, tz)
room = '#support' #not used in this case
testFunction = ->
robot.send "I work!"
or per example from Leanpub
testFunction = ->
robot.messageRoom room "I work!"
cron jobs setup after Hubot is running work fine:
Hubot new job "<crontab format>" <message> - Schedule a cron job to say something
Thank you again, all!
So we ended up using a slightly different format to get this up and running. For our uses, we excluded the time zone info, but it works with it as well.
module.exports = (robot) ->
cronJob = require('cron').CronJob
new cronJob('0 */1 * * * *', everyMinute(robot), null, true)
everyMinute = (robot) ->
-> robot.messageRoom '#billing', 'hey brah!'
If anyone has this running with code closer to the examples, feel free to answer.

Resources