Module: Wamp::Worker::BackgroundHandler

Defined in:
lib/wamp/worker/handler.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/wamp/worker/handler.rb', line 105

def self.included(base)
  base.class_eval do
    include BaseHandler

    # Use Sidekiq
    require 'sidekiq'
    include ::Sidekiq::Worker
  end
end

Instance Method Details

#invoke(method) ⇒ Object

Override the invoke method to push the process to the background



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wamp/worker/handler.rb', line 124

def invoke(method)

  # Also need to remove the session since it is not serializable.
  # Will add a new one in the background handler
  self.details.delete(:session)

  # Send the task to Sidekiq
  #
  # Note: We are explicitly serializing the args, kwargs, details
  # so that we can deserialize and have them appear as symbols in
  # the handler.
  self.class.perform_async(
      method,
      self.proxy.name,
      self.proxy.background_res_queue,
      self.command,
      self.args.to_json,
      self.kwargs.to_json,
      details.to_json)

  # If it is a procedure, return a defer
  if self.command.to_sym == :procedure
    Wamp::Client::Response::CallDefer.new
  else
    nil
  end

end

#perform(method, proxy_name, proxy_handle, command, args, kwargs, details) ⇒ Object

Method that is run when the process is invoked on the worker

Parameters:

  • method (Symbol)
    • The name of the method to execute

  • command (Symbol)
    • The command that is being backgrounded

  • args (Array)
    • The arguments for the handler

  • kwargs (Hash)
    • The keyword arguments for the handler

  • details (Hash)
    • Other details about the call



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/wamp/worker/handler.rb', line 160

def perform(method, proxy_name, proxy_handle, command, args, kwargs, details)

  # Create a proxy to act like the session.  Use a backgrounder so we also
  # get the "yield" method
  proxy = Proxy::Backgrounder.new(proxy_name, proxy_handle)

  # Deserialize the arguments as symbols
  args = JSON.parse(args, :symbolize_names => true)
  kwargs = JSON.parse(kwargs, :symbolize_names => true)
  details = JSON.parse(details, :symbolize_names => true)

  # Get the request ID
  request = details[:request]

  # Add the proxy to the details as a "session"
  details[:session] = self.session

  # Configure the handler
  self.configure(proxy, command, args, kwargs, details, true)

  # Call the user code and make sure to catch exceptions
  result = Wamp::Client::Response.invoke_handler do
    self.send(method)
  end

  # Only return the response if it is a procedure
  if command.to_sym == :procedure

    # Send the data back to the
    self.session.yield request, result, {}, true

  end
end

#sessionWamp::Client::Session, Wamp::Worker::Proxy::Requestor

Returns the session for the call

Returns:



118
119
120
# File 'lib/wamp/worker/handler.rb', line 118

def session
  self.proxy
end