lib/pushpop/job.rb in pushpop-0.2 vs lib/pushpop/job.rb in pushpop-0.3.1

- old
+ new

@@ -17,10 +17,12 @@ end attr_accessor :name attr_accessor :period + attr_accessor :webhook_url + attr_accessor :webhook_proc attr_accessor :every_options attr_accessor :steps def initialize(name=nil, &block) self.name = name || Pushpop.random_name @@ -32,10 +34,20 @@ def every(period, options={}) self.period = period self.every_options = options end + def webhook(url, &block) + raise 'Webhook is already set' if @webhook_url + raise 'Webhook must be set before steps' if self.steps.length > 0 + + self.webhook_url = url + self.webhook_proc = block + + Pushpop.web.add_route url, self + end + def step(name=nil, plugin=nil, &block) if plugin plugin_klass = self.class.plugins[plugin] raise "No plugin configured for #{plugin}" unless plugin_klass @@ -49,21 +61,19 @@ def add_step(step) self.steps.push(step) end def schedule - raise 'Set job period via "every"' unless self.period - Clockwork.manager.every(period, name, every_options) do - run + raise 'Set job period via "every"' unless self.period || @webhook_url + + if self.period + Clockwork.manager.every(period, name, every_options) do + run + end end end - def run - - # track the last response, and all responses - last_response = nil - step_responses = {} - + def run(last_response = nil, step_responses = {}) self.steps.each do |step| # track the last_response and all responses last_response = step.run(last_response, step_responses) step_responses[step.name] = last_response