Sha256: c715cb5153c4b4f30019cc364765b87bb04596f7ac9cf402ec408ec169250c78

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'timeouter'
require 'lusnoc/helper'

module Lusnoc
  class Guard

    include Helper

    def initialize(base_url)
      @base_url = base_url
      yield(self) if block_given?
    end

    def condition(&block)
      @condition = block
      self
    end

    def then(&block)
      @callback = block
      self
    end

    def run
      th = Thread.new do
        logger.info "Guard[#{@base_url.inspect}] thread started"
        watch_forever(@base_url)
        fire!
      rescue StandardError => e
        logger.error "Guard[#{@base_url.inspect}] error: #{e.inspect}"
        fire!
      ensure
        logger.info "Guard[#{@base_url.inspect}] finihsed"
      end

      yield
    ensure
      th.kill rescue nil
    end

    private

      def fire!
        @callback&.tap do |cb|
          @callback = nil
          logger.info "Guard[#{@base_url.inspect}] fired"
          cb.call
        end
      end

      def watch_forever(base_url)
        last_x_consul_index = 1

        Kernel.loop do
          resp = Lusnoc.http_get("#{base_url}?index=#{last_x_consul_index}&wait=10s", timeout: 15)
          logger.debug "Guard[#{@base_url.inspect}] response: #{resp.body}"
          return unless @condition.call(resp.body)

          index = [Integer(resp['x-consul-index']), 1].max
          last_x_consul_index = (index < last_x_consul_index ? 1 : index)
          sleep 1
        end
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lusnoc-0.1.2.16550 lib/lusnoc/guard.rb
lusnoc-0.1.2.16548 lib/lusnoc/guard.rb