Sha256: 856a5a8bb8038d3a209b23b12182e35feebef3c7ed3390f5819cafb0a84921b7

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'json'
require 'webrick'

module Helpers
  class Server
    def initialize
      @queue = Queue.new
      @server = nil
      @thread = nil
    end

    def start
      @server = WEBrick::HTTPServer.new({
        Port: 0,
        Logger: WEBrick::Log.new(STDOUT),
        AccessLog: []
      })

      @server.mount_proc('/deploy') do |req, res|
        @queue.push(req.body)

        res.status = 200
        res.body = "OK\n"
      end

      @thread = Thread.new { @server.start }

      loop do
        break if @server.status == :Running

        sleep(0.1)
      end
    end

    def stop
      @queue.clear
      @server.stop
      @thread.join(5)
      @server = nil
    end

    def url
      raise "Server is not running!" if @server.nil?

      "http://localhost:" + @server.config[:Port].to_s + "/deploy"
    end

    def last_request
      retries = 0

      begin
        JSON.parse(@queue.pop(true))
      rescue ThreadError
        raise if retries >= 10

        retries += 1
        sleep(0.1)

        retry
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bugsnag-capistrano-2.1.0 spec/helpers/server.rb