Class: RubyCI::WebSocket
- Inherits:
-
Object
- Object
- RubyCI::WebSocket
- Defined in:
- lib/ruby_ci.rb
Constant Summary collapse
- SUPPORTED_EVENTS =
%i[enq_request deq].freeze
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#node_index ⇒ Object
readonly
Returns the value of attribute node_index.
-
#run_key ⇒ Object
Returns the value of attribute run_key.
-
#task ⇒ Object
Returns the value of attribute task.
Instance Method Summary collapse
- #await(retry_count = 0) ⇒ Object
- #connect_to_ws ⇒ Object
-
#initialize(run_key) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #on(event, &block) ⇒ Object
- #send_msg(event, payload = {}) ⇒ Object
Constructor Details
#initialize(run_key) ⇒ WebSocket
Returns a new instance of WebSocket.
101 102 103 104 105 |
# File 'lib/ruby_ci.rb', line 101 def initialize(run_key) @on = {} @ref = 0 @run_key = run_key end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
97 98 99 |
# File 'lib/ruby_ci.rb', line 97 def connection @connection end |
#node_index ⇒ Object (readonly)
Returns the value of attribute node_index.
96 97 98 |
# File 'lib/ruby_ci.rb', line 96 def node_index @node_index end |
#run_key ⇒ Object
Returns the value of attribute run_key.
97 98 99 |
# File 'lib/ruby_ci.rb', line 97 def run_key @run_key end |
#task ⇒ Object
Returns the value of attribute task.
97 98 99 |
# File 'lib/ruby_ci.rb', line 97 def task @task end |
Instance Method Details
#await(retry_count = 0) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/ruby_ci.rb', line 135 def await(retry_count = 0) connect_to_ws do send_msg("phx_join") begin while = connection.read RubyCI.debug("ws#msg_received: #{.inspect}") response = .dig(:payload, :response) case response&.dig(:event) || [:event] when "phx_error" raise("[RubyCI] Unexpected error") when "join" handle_join(response) when "deq_request" handle_deq_request(response) when "deq" if (tests = response[:tests]).any? result = @on[:deq].call(tests) task.async do send_msg("deq", result) end else break end when "error" raise(response.inspect) else puts response end end rescue => e puts e. puts e.backtrace.join("\n") task&.stop end end end |
#connect_to_ws ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ruby_ci.rb', line 120 def connect_to_ws Async do |task| before_start_connection Async::WebSocket::Client.connect(endpoint) do |connection| after_start_connection self.connection = connection self.task = task yield ensure leave end end end |
#on(event, &block) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/ruby_ci.rb', line 107 def on(event, &block) raise EventNotSupportedError, event unless SUPPORTED_EVENTS.include?(event) raise EventAlreadyDefinedError, event if @on[event] @on[event] = block end |