Permalink
Browse files

Updated tasks to take 'every' timers

  • Loading branch information...
1 parent 8b4562a commit 599b9d06eb50255a295d5e87b11adc64dec50743 @karlbright committed Sep 26, 2011
Showing with 65 additions and 10 deletions.
  1. +62 −9 lib/base/task.coffee
  2. +3 −1 lib/tasks/pullrequests.coffee
View
@@ -3,23 +3,22 @@ sys = require 'sys'
class YvaineTask
# Public variables
- every : "2 minutes"
- daily : "9:00 PM"
+ every : null # 2 milliseconds/seconds/hours/minutes/days/years
+ daily : null
hourly : null
weekly : null
yearly : null
- from : "9:00 AM"
- to : "5:00 PM"
+ from : null # 9:00 AM
+ to : null # 5:00 PM
yvaine : null
name : null
options : null
now : null
timeout : null
- timers : []
+ timer : null
interval : 0
# Private variables
-
_hourly_regex : /(\d+:\d+) to ?(\d+:\d+)?/ # 9:00 to 12:00
_weekly_regex : /(\w+) at (\d+:\d+)/ # Friday at 5:00
_yearly_regex : /(\d+)(st|th|nd|rd) of (\w+) at (\d+:\d+)/ # 31st of October at 1:00
@@ -35,25 +34,79 @@ class YvaineTask
setTimers: ->
@now = new Date.now()
- if @hourly? || @daily? || @weekly? || @yearly?
+ if @every? || @hourly? || @daily? || @weekly? || @yearly?
+ @_setEveryTimer() if @every?
@_setDailyTimer() if @daily?
else
sys.log "[Yvaine] Unable to load #{@name}. No schedule given."
apply: ->
sys.log "[Task] #{@name}"
- @room = room
+ # @room = room
# Private
+ _getMillisecondsFrom: (str) ->
+ match = str.match(/([\d\.]+) (milliseconds|seconds|second|minutes|minute|hours|hour|days|day|weeks|week|months|month|years|year)/)
+ future = Date.now().add(parseInt(match[1]))[match[2]]()
+ return future - @now
+
+ _getDateFromTime: (str) ->
+ match = str.match(/(\d{1,2}):(\d{2}) (\w{1,2})/)
+ if match.length?
+ match[1] = parseInt(match[1])
+ match[1] = match[1] + 12 if match[3] is "PM"
+ return Date.today().set hour: match[1], minute: parseInt(match[2])
+ else
+ sys.log "[Yvaine] Unable to find time from string with _getDateFromTime(#{str})"
+
+ _setEveryTimer: ->
+ interval = @_getMillisecondsFrom(@every)
+ if @at?
+ # TO BE IMPLEMENTED
+ at = @_getDateFromTime @at
+ console.log at - @now
+ else if @from? and @to?
+ from = @_getDateFromTime @from
+ to = @_getDateFromTime @to
+ if @now.between from, to
+ @_setTaskInterval interval, from, to
+ sys.log "[Yvaine] Task created to apply every #{@every} from now until #{@to}"
+ else
+ countdown = from - @now
+ if countdown < 0
+ countdown = from.add(1).day() - @now
+ @timeout = setTimeout =>
+ @_setTaskInterval interval, from, to
+ , countdown
+ sys.log "[Yvaine] Timer to start task has begun. Will activate in #{Math.floor(countdown/1000/60)} minutes"
+ else
+ @_setTaskInterval interval
+
_setDailyTimer: ->
future = Date.parse @daily
countdown = future - @now
if countdown < 0
countdown = future.add(1).day() - @now
@timeout = setTimeout =>
@apply()
- @timers.push setInterval @apply, 1000 * 60 * 60 * 24 # Set interval to call daily
+ @timer = setInterval @apply, 1000 * 60 * 60 * 24
, countdown
+ # Callbacks
+
+ _setTaskInterval: (interval, from, to) =>
+ sys.log "[Yvaine] Creating task with interval of #{interval}"
+ if from? and to?
+ @timer = setInterval =>
+ if (to - Date.now()) <= 0
+ sys.log "[Yvaine] Task ending"
+ clearInterval(@timer)
+ @setTimers() # Set up for next day...
+ else
+ @apply()
+ , interval
+ else
+ @timer = setInterval @apply, interval
+
module.exports = YvaineTask
@@ -2,7 +2,9 @@ Task = require 'base/task'
GithubFeed = require 'support/githubfeed'
class PullRequests extends Task
- daily : '11:29 AM'
+ every : '2 minutes'
+ from : '9:00 AM'
+ to : '5:00 PM'
feed : null
room : null

0 comments on commit 599b9d0

Please sign in to comment.