Setting up node-cron to run with Hubot - cron

I'm trying to setup Hubot to run a cronjob but for some reason, the cronjob using node-cron is not firing. I've read and implemented things a few different ways (one example: https://leanpub.com/automation-and-monitoring-with-hubot/read) but nothing seems to work. Currently the code I'm using is
module.exports = (robot) ->
cronJob = require('cron').CronJob
tz = 'America/Los_Angeles'
pattern = '*/1 * * * *'
cronjob = new cronJob(pattern, everyMinute, null, true, tz)
console.log "reading cron"
room = "#testing"
robot.messageRoom room, 'startup message'
everyMinute = ->
robot.messageRoom '#testing', 'I will nag you every minute'
console.log "every minute should be executed"
I see the startup messages but the messages in everyMinute don't come up in the room or the log. I've tried different formats for the pattern but haven't had any luck.
What's odd is hubot-cron (https://github.com/miyagawa/hubot-cron) works fine. I can setup a job and see the output message from a cronjob so I know it works. If I look through the hubot-cron source, I see
start: (robot) ->
#cronjob = new cronJob(#pattern, =>
#sendMessage robot
, null, false, #timezone)
#cronjob.start()
This looks like what I'm doing but hubot-cron works and my code doesn't. Any ideas?

It turns out that node-cron didn't like the way I was passing that function. Looks like the proper way to pass a function to node-cron is
module.exports = (robot) ->
cronJob = require('cron').CronJob
tz = 'America/Los_Angeles'
pattern = '* * * * *'
new cronJob(pattern, (->
do everyMinute
), null, true, tz)
everyMinute = ->
console.log "every minute should be executed"
This works for me. Note that have the 'do everyMinute' on a separate line is necessary to avoid coffeescript complaining about a trailing comma.

Related

NPM cronJob - dynamically set time

here is my code for scheduling a task. i have used separate routes for starting,stopping and changing the time as given below. please tell me if its correct. and also im getting an error for changing the time frequency.please help me.
const cronjob=require('cron').CronJob
var first='* * * * * *'
var task=function() {
console.log('last'+job.lastDate()+' next '+job.nextDates(1));
}
var complete=function(){
console.log('the end.........')
}
var job = new cronjob(first,task, complete, false, 'Asia/Kolkata');
app.get('/start',(req,res)=>{
job.start()
console.log('is job running? ', job.running);
res.send('cron started')
})
app.get('/set',(req,res)=>{
var time=req.headers.time /* input taken from user for changing the frequency*/
time='30 5 1-31 0-11 1-7' /*hard-coded temporary for testing*/
console.log(time)
job.setTime(time)
})
/set api is throwing an error
/Error: time must be an instance of CronTime./
we have to use instance of a cronTime i.e.
const CronTime = require('cron').CronTime
.
.
.
var time=req.query.time
//time='*/5 * * * * *'
job.setTime(new CronTime(time))
res.send('time changes')

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-cron is not working on my server

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

How do invoke script with another node script?

I have an node app that runs as a cron job every few seconds:
var CronJob = require('cron').CronJob;
new CronJob('*/5 * * * * *', function(){
console.log('Here invoke a script called requestdata');
}, null, true, "America/Los_Angeles");
and I just want to call a script without invoking functions on it. So its not
requestdata.foo();
but just call requestdata in same directory. how is this done? If it was on command line I would just do:
node requestdata
but how do I do this inside another script?
Use child_process, like so
var cp = require('child_process');
cp.fork(__dirname + '/request data.js');
See http://nodejs.org/api/child_process.html
This solution also shows how to pass parameters to the "request data.js" script
var cp = require('child_process');
cp.fork(__dirname + '/request data.js',[array,of,string,prams]);

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