Sha256: ff01d25fb28f722e3ed4220d8a12a5dc56d428f48769757f890f3af7e3ee3d1b

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

module Pushpop

  class Job

    class << self

      cattr_accessor :plugins
      self.plugins = {}

      def register_plugin(name, klass)
        self.plugins ||= {}
        self.plugins[name.to_s] = klass
      end

    end

    attr_accessor :name
    attr_accessor :every_duration
    attr_accessor :every_options
    attr_accessor :steps

    def initialize(name=nil, &block)
      self.name = name || Pushpop.random_name
      self.steps = []
      self.every_options = {}
      self.instance_eval(&block)
    end

    def every(duration, options={})
      self.every_duration = duration
      self.every_options = options
    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

        self.add_step(plugin_klass.new(name, plugin, &block))
      else
        self.add_step(Step.new(name, plugin, &block))
      end
    end

    def add_step(step)
      self.steps.push(step)
    end

    def schedule
      Clockwork.manager.every(every_duration, name, every_options) do
        run
      end
    end

    def run

      # track the last response, and all responses
      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

        # abort unless this step returned truthily
        return unless last_response
      end

      # log responses in debug
      Pushpop.logger.info("#{name}: #{step_responses}")

      # return the last response and all responses
      [last_response, step_responses]
    end

    def method_missing(method, *args, &block)
      plugin_class = self.class.plugins[method.to_s]

      name = args[0]
      plugin = method.to_s

      if plugin_class
        step(name, plugin, &block)
      else
        super
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pushpop-0.1.0 lib/pushpop/job.rb