Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial commit

  • Loading branch information...
commit dd8b489674700ddcafa074093d2458f0ba19c10c 0 parents
@miyagawa authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
3  CHANGES.md
@@ -0,0 +1,3 @@
+v0.1.0
+======
+* Intial release
45 README.md
@@ -0,0 +1,45 @@
+# hubot-cron
+
+hubot-cron adds a cronjob system to hubot to schedule messages on a specific date and time.
+
+## Installation
+
+Add `hubot-cron` to your package.json, run `npm install` and put a symlink from your `scripts` directory.
+
+## Usage
+
+Add hubot-cron to your `package.json` dependencies.
+
+```
+"dependencies": {
+ "hubot-cron": ">= 0.1.0"
+}
+```
+
+Install it and put a symlink from your scripts directory.
+
+```
+> npm install
+> ln -s ../node_modules/hubot-cron/src/scripts/cron.coffee scripts/
+```
+
+## Usage
+
+```
+miyagawa> hubot new job "0 9 * * 1-5" Good morning everyone!
+hubot> Job 12345 created
+
+miyagawa> hubot list jobs
+hubot> (list of jobs)
+
+miyagawa> hubot rm job 12345
+hubot> Job 12345 removed
+```
+
+You can use any [node-cron](https://github.com/ncb000gt/node-cron) compatible crontab format to schedule messages. Registered message will be sent to the same channel where you created a job.
+
+To persist the cron job in the hubot restart, you're recommended to use redis to persist Hubot brain.
+
+## See Also
+
+`reminder.coffee` in hubot-scripts.
20 package.json
@@ -0,0 +1,20 @@
+{
+ "author": "Tatsuhiko Miyagawa",
+ "name": "hubot-cron",
+ "version": "0.1.0",
+ "description": "Cron job messaging for hubot",
+ "homepage": "https://github.com/miyagawa/hubot-cron",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/miyagawa/hubot-cron.git"
+ },
+ "license": "MIT",
+ "keywords": [
+ "hubot",
+ "cron"
+ ],
+ "dependencies": {
+ "coffee-script": ">= 1.1.0",
+ "cron": ">= 1.0.1"
+ }
+}
74 src/scripts/cron.coffee
@@ -0,0 +1,74 @@
+# Description:
+# register cron jobs to schedule messages on the current channel
+#
+# Commands:
+# hubot new job "<crontab format>" <message> - Schedule a cron job to say something
+# hubot list jobs - List current cron jobs
+# hubot remove job <id> - remove job
+#
+# Author:
+# miyagawa
+
+cronJob = require('cron').CronJob
+
+JOBS = {}
+
+createNewJob = (robot, pattern, user, message) ->
+ id = Math.floor(Math.random() * 1000000)
+ job = registerNewJob robot, id, pattern, user, message
+ robot.brain.data.cronjob[id] = job.serialize()
+ id
+
+registerNewJob = (robot, id, pattern, user, message) ->
+ JOBS[id] = new Job(id, pattern, user, message)
+ JOBS[id].start(robot)
+ JOBS[id]
+
+module.exports = (robot) ->
+ robot.brain.on 'loaded', =>
+ robot.brain.data.cronjob or= {}
+ for own id, job of robot.brain.data.cronjob
+ registerNewJob robot, id, job[0], job[1], job[2]
+
+ robot.respond /(?:new|add) job "(.*?)" (.*)$/i, (msg) ->
+ try
+ id = createNewJob robot, msg.match[1], msg.message.user, msg.match[2]
+ msg.send "Job #{id} created"
+ catch error
+ msg.send "Error caught parsing crontab pattern: #{error}. See http://crontab.org/ for the syntax"
+
+ robot.respond /(?:list|ls) jobs?/i, (msg) ->
+ for own id, job of robot.brain.data.cronjob
+ msg.send "#{id}: #{job[0]} @#{job[1].room} \"#{job[2]}\""
+
+ robot.respond /(?:rm|remove|del|delete) job (\d+)/i, (msg) ->
+ id = msg.match[1]
+ if JOBS[id]
+ JOBS[id].stop()
+ delete robot.brain.data.cronjob[id]
+ msg.send "Job #{id} deleted"
+ else
+ msg.send "Job #{id} does not exist"
+
+class Job
+ constructor: (id, pattern, user, message) ->
+ @id = id
+ @pattern = pattern
+ @user = user
+ @message = message
+
+ start: (robot) ->
+ @cronjob = new cronJob(@pattern, =>
+ @sendMessage robot
+ )
+ @cronjob.start()
+
+ stop: ->
+ @cronjob.stop()
+
+ serialize: ->
+ [@pattern, @user, @message]
+
+ sendMessage: (robot) ->
+ robot.send @user, @message
+
Please sign in to comment.
Something went wrong with that request. Please try again.