lib/clockwork.rb in clockwork-0.1.1 vs lib/clockwork.rb in clockwork-0.2.0
- old
+ new
@@ -1,24 +1,35 @@
module Clockwork
class Event
- def initialize(span, options={}, &block)
- @secs = parse_span(span)
+ attr_accessor :job, :last
+
+ def initialize(period, job, block, options={})
+ @period = period
+ @job = job
@at = parse_at(options[:at])
@last = nil
@block = block
end
+ def to_s
+ @job
+ end
+
def time?(t)
- ellapsed_ready = (@last.nil? or (t - @last).to_i >= @secs)
+ ellapsed_ready = (@last.nil? or (t - @last).to_i >= @period)
time_ready = (@at.nil? or (t.hour == @at[0] and t.min == @at[1]))
ellapsed_ready and time_ready
end
def run(t)
- @block.call
@last = t
+ @block.call(@job)
rescue => e
+ log_error(e)
+ end
+
+ def log_error(e)
STDERR.puts exception_message(e)
end
def exception_message(e)
msg = [ "Exception #{e.class} -> #{e.message}" ]
@@ -29,28 +40,10 @@
end
msg.join("\n")
end
- class FailedToParse < RuntimeError; end
-
- def parse_span(span)
- m = span.match(/^(\d+)([smhd])$/)
- raise FailedToParse, span unless m
- ordinal, magnitude = m[1].to_i, m[2]
- ordinal * magnitude_multiplier[magnitude]
- end
-
- def magnitude_multiplier
- {
- 's' => 1,
- 'm' => 60,
- 'h' => 60*60,
- 'd' => 24*60*60
- }
- end
-
def parse_at(at)
return unless at
m = at.match(/^(\d\d):(\d\d)$/)
raise FailedToParse, at unless m
hour, min = m[1].to_i, m[2].to_i
@@ -59,35 +52,67 @@
end
end
extend self
- def every(span, options={}, &block)
- event = Event.new(span, options, &block)
+ def handler(&block)
+ @@handler = block
+ end
+
+ class NoHandlerDefined < RuntimeError; end
+
+ def get_handler
+ raise NoHandlerDefined unless (defined?(@@handler) and @@handler)
+ @@handler
+ end
+
+ def every(period, job, options={}, &block)
+ event = Event.new(period, job, block || get_handler, options)
@@events ||= []
@@events << event
event
end
def run
+ log "Starting clock for #{@@events.size} events: [ " + @@events.map { |e| e.to_s }.join(' ') + " ]"
loop do
tick
sleep 1
end
end
+ def log(msg)
+ puts "[#{Time.now}] #{msg}"
+ end
+
def tick(t=Time.now)
to_run = @@events.select do |event|
event.time?(t)
end
to_run.each do |event|
+ log "-> #{event}"
event.run(t)
end
to_run
end
def clear!
@@events = []
+ @@handler = nil
end
+end
+
+class Numeric
+ def seconds; self; end
+ alias :second :seconds
+
+ def minutes; self * 60; end
+ alias :minute :minutes
+
+ def hours; self * 3600; end
+ alias :hour :hours
+
+ def days; self * 86400; end
+ alias :day :days
end