module Wamp::Worker::BackgroundHandler

Public Class Methods

included(base) click to toggle source
# 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

Public Instance Methods

invoke(method) click to toggle source

Override the invoke method to push the process to the background

# 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) click to toggle source

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

@param method [Symbol] - The name of the method to execute @param command [Symbol] - The command that is being backgrounded @param args [Array] - The arguments for the handler @param kwargs [Hash] - The keyword arguments for the handler @param details [Hash] - Other details about the call

# 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
session() click to toggle source

Returns the session for the call

@return [Wamp::Client::Session, Wamp::Worker::Proxy::Requestor]

# File lib/wamp/worker/handler.rb, line 118
def session
  self.proxy
end