Sha256: 703d08241d36db045e669fec8a9925431dacfed101de340d47fd3d1b43ab7622

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

require_relative "errors/stop_execution_requested"


module Ray
  class Client
    def initialize(port = 23517, host = 'localhost')
      @port = port
      @host = host
    end

    def send(request)
      req = Net::HTTP::Post.new(uri, { 'Content-Type' => 'application/json' })
      req.body = request.to_json

      begin
        Net::HTTP.start(uri.hostname, uri.port) do |http|
          http.request(req)
        end
      rescue StandardError
        # Ignore any errors
      end
    end

    def lock_exists(lockName)
      req = Net::HTTP::Get.new("/locks/#{lockName}", { 'Content-Type' => 'application/json' })

      begin
        response = Net::HTTP.start(uri.hostname, uri.port) do |http|
          http.request(req)
        end
      rescue StandardError
        return false
      end

      result = JSON.parse(response.body)

      if (result['stop_execution'])
        raise new StopExecutionRequested
      end

      return result['active'] || false
    end

    def uri
      @uri ||= URI("#{@host}:#{@port}")
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-ray-0.4.1 lib/ray/client.rb
ruby-ray-0.4.0 lib/ray/client.rb
ruby-ray-0.3.0 lib/ray/client.rb
ruby-ray-0.2.0 lib/ray/client.rb