lib/clockwork.rb in clockwork-0.5.0 vs lib/clockwork.rb in clockwork-0.5.1

- old
+ new

@@ -67,10 +67,14 @@ else raise ArgumentError.new(':if expects a callable object, but #{options[:if]} does not respond to call') end end + if options[:thread] + @thread = options[:thread] + end + tz = options[:tz] || Clockwork.config[:tz] @timezone = TZInfo::Timezone.get(tz) if tz end def to_s @@ -85,16 +89,37 @@ t = convert_timezone(t) elapsed_ready = (@last.nil? or (t - @last).to_i >= @period) elapsed_ready and (@at.nil? or @at.ready?(t)) and (@if.nil? or @if.call(t)) end + def thread? + @thread + end + + def thread_available? + Thread.list.count < Clockwork.config[:max_threads] + end + def run(t) t = convert_timezone(t) @last = t + + if thread? + if thread_available? + Thread.new { execute } + else + log_error "Threads exhausted; skipping #{self}" + end + else + execute + end + end + + def execute @block.call(@job) rescue => e - log_error(e) + log_error e end def log_error(e) Clockwork.config[:logger].error(e) end @@ -120,10 +145,10 @@ end extend self def default_configuration - { :sleep_timeout => 1, :logger => Logger.new(STDOUT) } + { :sleep_timeout => 1, :logger => Logger.new(STDOUT), :max_threads => 10 } end @@configuration = default_configuration def handler(&block)