Class: BackgroundLite::ResqueHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/background_lite/core_ext/handlers/resque_handler.rb

Overview

This background handler sends the method as well as the arguments through Resque to the background process.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.queueObject

Returns the value of attribute queue.



8
9
10
# File 'lib/background_lite/core_ext/handlers/resque_handler.rb', line 8

def queue
  @queue
end

Class Method Details

.decode(message) ⇒ Object

Decodes a marshalled message which was previously sent over Resque. Returns an array containing the object, the method name as a string, and the method arguments.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/background_lite/core_ext/handlers/resque_handler.rb', line 21

def self.decode(message)
  begin
    object, method, args, transaction_id = Marshal.load(Base64.decode64(message))
  rescue ArgumentError => e
    # Marshal.load does not trigger const_missing, so we have to do this
    # ourselves.
    e.message.split(' ').last.constantize
    retry
  end
  [object, method, args, transaction_id]
end

.handle(object, method, args, options = {}) ⇒ Object

Marshals the method and the arguments and sends it through Resque to the background process.



13
14
15
16
# File 'lib/background_lite/core_ext/handlers/resque_handler.rb', line 13

def self.handle(object, method, args, options = {})
  require 'resque'
  Resque.enqueue(self, Base64.encode64(Marshal.dump([object, method, args, options[:transaction_id]])))
end

.perform(message) ⇒ Object

Executes a marshalled message which was previously sent over Resque, in the context of the object, with all the arguments passed.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/background_lite/core_ext/handlers/resque_handler.rb', line 36

def self.perform(message)
  logger = BackgroundLite::Config.default_logger
  begin
    object, method, args, transaction_id = self.decode(message)
    if logger.debug?
      logger.debug "--- executing method: #{method}"
      logger.debug "--- with variables: #{args.inspect}"
      logger.debug "--- in object: #{object.class.name}, #{object.id}"
      logger.debug "--- Transaction ID: #{transaction_id}"
    end
    object.send(method, *args)
    logger.debug "--- it happened!" if logger.debug?
  rescue Exception => e
    logger.fatal e.message
    logger.fatal e.backtrace
    "BackgroundLite::#{BackgroundLite::Config.default_error_reporter.to_s.camelize}ErrorReporter".constantize.report(e)
    raise e
  end
end