class FastCI::WebSocket
Constants
- SUPPORTED_EVENTS
Attributes
node_index[R]
Public Class Methods
new()
click to toggle source
# File lib/fast_ci.rb, line 41 def initialize @on = {} @ref = 0 end
Public Instance Methods
await()
click to toggle source
# File lib/fast_ci.rb, line 59 def await before_start_connection Async do |task| Async::WebSocket::Client.connect(endpoint) do |connection| after_start_connection send_msg(connection, "phx_join") while message = connection.read FastCI.debug("ws#msg_received: #{message.inspect}") response = message.dig(:payload, :response) case response&.dig(:event) || message[:event] when "join" handle_join(connection, response) when "deq_request" handle_deq_request(connection, response) when "deq" if (tests = response[:tests]).any? result = @on[:deq].call(tests) task.async do send_msg(connection, "deq", result) end else break end when "error" raise(response.inspect) else puts response end end ensure send_msg(connection, "leave") connection.close end end end
on(event, &block)
click to toggle source
# File lib/fast_ci.rb, line 46 def on(event, &block) raise EventNotSupportedError, event unless SUPPORTED_EVENTS.include?(event) raise EventAlreadyDefinedError, event if @on[event] @on[event] = block end
send_msg(connection, event, payload = {})
click to toggle source
# File lib/fast_ci.rb, line 53 def send_msg(connection, event, payload = {}) FastCI.debug("ws#send_msg: #{event} -> #{payload.inspect}") connection.write({ "topic": topic, "event": event, "payload": payload, "ref": ref }) connection.flush end
Private Instance Methods
after_start_connection()
click to toggle source
# File lib/fast_ci.rb, line 108 def after_start_connection if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter) WebMock::HttpLibAdapters::AsyncHttpClientAdapter.enable! end end
before_start_connection()
click to toggle source
# File lib/fast_ci.rb, line 101 def before_start_connection if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter) WebMock::HttpLibAdapters::AsyncHttpClientAdapter.disable! end end
endpoint()
click to toggle source
# File lib/fast_ci.rb, line 136 def endpoint params = URI.encode_www_form({ build_id: FastCI.configuration.build_id, run_key: FastCI.configuration.run_key, secret_key: FastCI.configuration.secret_key, commit: FastCI.configuration.commit, branch: FastCI.configuration.branch }) url = "ws://#{FastCI.configuration.api_url}/test_orchestrators/socket/websocket?#{params}" Async::HTTP::Endpoint.parse(url) end
handle_deq_request(connection, _response)
click to toggle source
# File lib/fast_ci.rb, line 124 def handle_deq_request(connection, _response) send_msg(connection, "deq") end
handle_join(connection, response)
click to toggle source
# File lib/fast_ci.rb, line 114 def handle_join(connection, response) @node_index = response[:node_index] FastCI.debug("NODE_INDEX: #{@node_index}") send_msg(connection, "enq", { tests: @on[:enq_request].call }) if node_index.zero? send_msg(connection, "deq") if response[:state] == "running" end
ref()
click to toggle source
# File lib/fast_ci.rb, line 128 def ref @ref += 1 end
topic()
click to toggle source
# File lib/fast_ci.rb, line 132 def topic "test_orchestrator:#{FastCI.configuration.run_key}-#{FastCI.configuration.build_id}" end