Module: Rhoconnect::AsyncHelpers

Defined in:
lib/rhoconnect/async.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

def self.included(klass)
  (klass.instance_methods & self.instance_methods).each do |method|
    klass.instance_eval{remove_method method.to_sym}
  end
end

Instance Method Details

#catch_allObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rhoconnect/async.rb', line 38

def catch_all
  res = nil
  begin
    res = catch(:halt) { yield }
  rescue ApiException => ae
    res = [ae.error_code, ae.message]
  rescue Exception => e
    log e.message + e.backtrace.join("\n")
    res = [500, e.message]
  end
  res
end

#execute_api_call(client_call = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rhoconnect/async.rb', line 51

def execute_api_call(client_call = false)
    f = Fiber.current
    operation = proc {
      catch_all do
        res = yield params, current_user, self
        if params.has_key? :warning
          Rhoconnect.log params[:warning]
          response.headers['Warning'] = params[:warning]
        end
        res
      end
    }
    result = nil
    callback = proc { |proc_res| result = proc_res; f.resume }

    EventMachine.defer operation, callback
    Fiber.yield
    # we can not throw exceptions across threads
    # so we analyze it in the main thread after the
    # request has been processed and if result
    # has error code - then we throw :halt here
    if Array === result and Fixnum === result.first
      throw :halt, result
    end
    result
end