Sha256: 43202063b9e501ef67533da5f30876255f587368c80407e01188ce54fb5646a6

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

#encoding: utf-8
module Polling
  class Engine
    attr_accessor :target, :offset
    def initialize
      @target = @offset = 0
      @init_time = 60
    end

    def increment! interval
      @init_time = interval if interval > 60
      @target += interval
    end

    def substitution! interval
      @init_time = interval if interval > 60
      @target = interval
    end

    def stime opts={}
      opts = {target: @target, init_time: @init_time}.merge opts

      case
      when target(opts[:target]) < opts[:init_time]
        stime = make_sleep(opts[:target],now_sec_to_f)
        stime += opts[:init_time] if opts[:target].eql? 0
      when target(opts[:target]) >= opts[:init_time]
        decrement!
        stime = make_sleep(opts[:target],now_sec_to_f) + opts[:init_time]
      end

      stime += @offset
      stime -= opts[:init_time] if stime > opts[:init_time]

      if stime < 0
        stime = opts[:init_time] - stime.abs
      end

      debug debug: opts[:debug], stime: stime
      stime
    rescue => ex
      $stderr.puts ex.to_s
      return opts[:init_time]
    end

    def stime_async opts={}
      stime = opts[:interval] - (opts[:after] - opts[:before])
      debug debug: opts[:debug], stime: stime
      stime
    rescue => ex
      raise ex
    end

    private

    def target target=@target, offset=@offset
      target + offset
    end

    def now_sec_to_f init_time=@init_time
      Time.now.to_f % init_time
    end

    def make_sleep target=@target, now
      target - now
    end

    def decrement! init_time=@init_time
      @target -= init_time
    end

    def start_print stime
      until_time = Time.at(Time.now.to_f + stime)
      $stdout.print "start: #{until_time}\n"
    end

    def debug opts={}
      if opts[:debug]
        until_time = Time.at(Time.now.to_f + opts[:stime])
        $stdout.print "sleep #{opts[:stime]}seconds (until #{until_time})\n"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polling-0.1.4 lib/polling/engine.rb
polling-0.1.3 lib/polling/engine.rb