Class: Faraday::Adapter::EMHttp::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/faraday/adapter/em_http.rb

Overview

The parallel manager is designed to start an EventMachine loop and block until all registered requests have been completed.

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



182
183
184
# File 'lib/faraday/adapter/em_http.rb', line 182

def initialize
  reset
end

Instance Method Details

#addObject



196
197
198
199
200
201
202
203
# File 'lib/faraday/adapter/em_http.rb', line 196

def add
  if running?
    perform_request { yield }
  else
    @registered_procs << Proc.new
  end
  @num_registered += 1
end

#check_finishedObject



227
228
229
230
231
# File 'lib/faraday/adapter/em_http.rb', line 227

def check_finished
  if @num_succeeded + @errors.size == @num_registered
    EventMachine.stop
  end
end

#perform_requestObject



221
222
223
224
225
# File 'lib/faraday/adapter/em_http.rb', line 221

def perform_request
  client = yield
  client.callback { @num_succeeded += 1; check_finished }
  client.errback { @errors << client.error; check_finished }
end

#resetObject



186
187
188
189
190
191
192
# File 'lib/faraday/adapter/em_http.rb', line 186

def reset
  @registered_procs = []
  @num_registered = 0
  @num_succeeded = 0
  @errors = []
  @running = false
end

#runObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/faraday/adapter/em_http.rb', line 205

def run
  if @num_registered > 0
    @running = true
    EventMachine.run do
      @registered_procs.each do |proc|
        perform_request(&proc)
      end
    end
    if @errors.size > 0
      raise Faraday::Error::ClientError, @errors.first || "connection failed"
    end
  end
ensure
  reset
end

#running?Boolean

Returns:

  • (Boolean)


194
# File 'lib/faraday/adapter/em_http.rb', line 194

def running?() @running end