Sha256: 17ba33d35e8a95597be42810a7b3fbc16e71731608c1174edf3be8b465fa186b

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

require "yaml"
require "socket"
require "timeout"
require "rainbow"

class TestConfiguration
  attr_reader :resque_redis_port, :rabbit_port
  def initialize(docker_compose_file)
    yaml = YAML.load_file(docker_compose_file)

    @resque_redis_port = (ENV["PWWKA_RESQUE_REDIS_PORT"] || yaml["services"]["resque"]["ports"].first.split(/:/)[0]).to_i
    @rabbit_port       = (ENV["PWWKA_RABBIT_PORT"]       || yaml["services"]["rabbit"]["ports"].first.split(/:/)[0]).to_i
  end

  def check_services
    redis_running = is_port_open?("localhost",@resque_redis_port)
    rabbit_running = is_port_open?("localhost",@rabbit_port)
    if !(redis_running && rabbit_port)
      OpenStruct.new(error: "Rabbit and/or Redis is not running - you need to run `docker-compose up` in the root dir",
                     up?: false)
    else
      OpenStruct.new(up?: true)
    end
  end

private

  def is_port_open?(ip, port)
    begin
      Timeout::timeout(1) do
        begin
          s = TCPSocket.new(ip, port)
          s.close
          return true
        rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
          return false
        end
      end
    rescue Timeout::Error
    end

    return false
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
pwwka-0.16.1 spec/support/test_configuration.rb
pwwka-0.16.0 spec/support/test_configuration.rb
pwwka-0.15.1 spec/support/test_configuration.rb
pwwka-0.15.0 spec/support/test_configuration.rb
pwwka-0.14.0 spec/support/test_configuration.rb
pwwka-0.13.3 spec/support/test_configuration.rb
pwwka-0.13.2 spec/support/test_configuration.rb
pwwka-0.13.1 spec/support/test_configuration.rb
pwwka-0.13.0 spec/support/test_configuration.rb
pwwka-0.13.0.RC2 spec/support/test_configuration.rb
pwwka-0.13.0.RC1 spec/support/test_configuration.rb
pwwka-0.12.0 spec/support/test_configuration.rb
pwwka-0.12.0.RC2 spec/support/test_configuration.rb
pwwka-0.12.0.RC1 spec/support/test_configuration.rb
pwwka-0.11.0 spec/support/test_configuration.rb
pwwka-0.11.0.RC1 spec/support/test_configuration.rb