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.
Related
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();
});
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();
});
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
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.
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