Class: Rhoconnect::Middleware::Async

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

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) {|_self| ... } ⇒ Async

Returns a new instance of Async.

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
# File 'lib/rhoconnect/async.rb', line 34

def initialize(app, opts={})
  @app = app
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rhoconnect/async.rb', line 39

def call(env)
  f = Fiber.current
  # making a copy is crucial here
  # otherwise 'env' will not be the same
  # in the deferred execution
  aenv = env.dup
  operation = proc {
    res = nil
    aenv['REQUEST_THREAD'] = Thread.current
    if(aenv['RHO_ABORT_PROCESS'])
      res = [500, 'Request is aborted']
    else
      res = @app.call(aenv)
    end
    res
  }
  result = nil
  callback = proc { |proc_res| result = proc_res; f.resume }

  EventMachine.defer operation, callback
  Fiber.yield
  result
end