Class: RubyCI::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ci.rb

Constant Summary collapse

SUPPORTED_EVENTS =
%i[enq_request deq].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#connectionObject

Returns the value of attribute connection.



97
98
99
# File 'lib/ruby_ci.rb', line 97

def connection
  @connection
end

#node_indexObject (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_keyObject

Returns the value of attribute run_key.



97
98
99
# File 'lib/ruby_ci.rb', line 97

def run_key
  @run_key
end

#taskObject

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 message = connection.read
        RubyCI.debug("ws#msg_received: #{message.inspect}")

        response = message.dig(:payload, :response)

        case response&.dig(:event) || message[: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.message
      puts e.backtrace.join("\n")
      task&.stop
    end
  end
end

#connect_to_wsObject



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

#send_msg(event, payload = {}) ⇒ Object



114
115
116
117
118
# File 'lib/ruby_ci.rb', line 114

def send_msg(event, payload = {})
  RubyCI.debug("ws#send_msg: #{event} -> #{payload.inspect}")
  connection.write({ "topic": topic, "event": event, "payload": payload, "ref": ref })
  connection.flush
end