Sha256: 6b8a74371fd3de0e434fabbcaab9dbb941ffb332a28bf2b49487343b49a23a7e

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

require 'rufus-scheduler'

module Docket
  class Repeater

    attr_accessor :frequencies, :perform_action, :perform_on, 
                  :scheduler, :scheduler_action, :scheduler_set_callback
    attr_reader :storage, :repeat

    def initialize args={}
      @frequencies            = args[:frequencies]
      @perform_action         = args[:perform_action]
      @perform_on             = args[:perform_on]
      @scheduler              = args[:scheduler] || Rufus::Scheduler.new
      @scheduler_action       = args[:scheduler_action]
      @scheduler_set_callback = args[:scheduler_set_callback] || lambda {|scheduler|}
      @storage                = args[:storage] || Docket::Storage.new('/tmp/docket.rb')
      @repeat                 = args[:repeat]
    end

    def repeated_items
      frequencies.collect { |frequency| items_for frequency }.compact.flatten
    end

    def items_for frequency
      Array(storage.read(frequency))
    end

    def set
      frequencies.each do |frequency|
        if scheduler_action
          scheduler_action.call(self, frequency)
        else
          scheduler.every(frequency, :times => repeat) do
            items_for(frequency).each { |key| perform_on.perform(key, perform_action) }
          end
        end

      end

      scheduler_set_callback.call(scheduler)
    end

    def stop force=false
      if force
        scheduler.shutdown(:kill)
      else
        scheduler.shutdown(:wait)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docket-0.1.2 lib/docket/repeater.rb
docket-0.1.1 lib/docket/repeater.rb
docket-0.0.1 lib/docket/repeater.rb