lib/stalker.rb in stalker-0.6.1 vs lib/stalker.rb in stalker-0.7.0
- old
+ new
@@ -24,10 +24,15 @@
def job(j, &block)
@@handlers ||= {}
@@handlers[j] = block
end
+ def before(&block)
+ @@before_handlers ||= []
+ @@before_handlers << block
+ end
+
def error(&blk)
@@error_handler = blk
end
class NoJobsDefined < RuntimeError; end
@@ -68,10 +73,15 @@
handler = @@handlers[name]
raise(NoSuchJob, name) unless handler
begin
Timeout::timeout(job.ttr - 1) do
+ if defined? @@before_handlers and @@before_handlers.respond_to? :each
+ @@before_handlers.each do |block|
+ block.call(name)
+ end
+ end
handler.call(args)
end
rescue Timeout::Error
raise JobTimeout, "#{name} hit #{job.ttr-1}s timeout"
end
@@ -159,8 +169,9 @@
@@error_handler
end
def clear!
@@handlers = nil
+ @@before_handlers = nil
@@error_handler = nil
end
end