Sha256: 0fb918812407060bab75767ac78f7c2191c5949341405762d2e959c33f52e3f9

Contents?: true

Size: 1022 Bytes

Versions: 1

Compression:

Stored size: 1022 Bytes

Contents

module Workers
  class Timer
    attr_reader :delay
    attr_reader :repeat

    def initialize(delay, options = {}, &block)
      @delay = delay
      @callback = options[:callback] || block
      @repeat = options[:repeat] || false
      @scheduler = options[:scheduler] || Workers.scheduler
      @logger = options[:logger]

      @mutex = Mutex.new

      reset

      @scheduler.schedule(self)
    end

    def <=>(other)
      return sec_remaining <=> other.sec_remaining
    end

    def sec_remaining
      @mutex.synchronize do
        diff = @fire_at.to_f - Time.now.utc.to_f

        return (diff > 0) ? diff : 0
      end
    end

    def overdue?
        return sec_remaining <= 0
    end

    def fire
      @mutex.synchronize do
        @callback.call if @callback
      end

      return nil
    end

    def cancel
      @scheduler.unschedule(self)

      return nil
    end

    def reset
      @mutex.synchronize do
        @fire_at = Time.now.utc + @delay
      end

      return nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
workers-0.0.3 lib/workers/timer.rb