Module: BackgroundLite

Defined in:
lib/background_lite/background.rb,
lib/background_lite/core_ext/handlers/drb_handler.rb,
lib/background_lite/core_ext/handlers/disk_handler.rb,
lib/background_lite/core_ext/handlers/fork_handler.rb,
lib/background_lite/core_ext/handlers/test_handler.rb,
lib/background_lite/core_ext/handlers/forget_handler.rb,
lib/background_lite/core_ext/handlers/resque_handler.rb,
lib/background_lite/core_ext/handlers/runner_handler.rb,
lib/background_lite/core_ext/handlers/in_process_handler.rb,
lib/background_lite/core_ext/handlers/active_messaging_handler.rb,
lib/background_lite/core_ext/error_reporters/test_error_reporter.rb,
lib/background_lite/core_ext/error_reporters/silent_error_reporter.rb,
lib/background_lite/core_ext/error_reporters/stderr_error_reporter.rb,
lib/background_lite/core_ext/error_reporters/stdout_error_reporter.rb,
lib/background_lite/core_ext/error_reporters/exception_notification_error_reporter.rb

Overview

This module holds methods for background handling

Defined Under Namespace

Classes: ActiveMessagingHandler, Config, DiskHandler, DrbHandler, ExceptionNotificationErrorReporter, ForgetHandler, ForkHandler, InProcessHandler, ResqueHandler, RunnerHandler, SilentNotificationErrorReporter, StderrErrorReporter, StdoutErrorReporter, TestErrorReporter, TestHandler

Class Method Summary collapse

Class Method Details

.disable(&block) ⇒ Object

Disables background handling for the given block.



81
82
83
84
85
86
87
88
89
# File 'lib/background_lite/background.rb', line 81

def self.disable(&block)
  value = BackgroundLite.disabled
  begin
    BackgroundLite.disable!
    yield
  ensure
    BackgroundLite.disabled = value
  end
end

.disable!Object

Disables background handling.



71
72
73
# File 'lib/background_lite/background.rb', line 71

def self.disable!
  BackgroundLite.disabled = true
end

.enable!Object

Enables background handling.



76
77
78
# File 'lib/background_lite/background.rb', line 76

def self.enable!
  BackgroundLite.disabled = false
end

.send_to_background(object, method, args = [], options = {}) ⇒ Object

Sends a message to the background. The message contains an object, a method, and the methods arguments. The object and the arguments will be cloned for background handling.

The options hash lets you choose the background handler(s) and their configuration, if available.

You should rarely need to use this method directly. Rather use Class#background_method to mark a method to be executed in the background.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/background_lite/background.rb', line 100

def self.send_to_background(object, method, args = [], options = {})
  object = object.clone_for_background
  args = args.collect { |a| a.clone_for_background }
  
  config = (BackgroundLite::Config.load(options[:config].to_s) || {})
  handler = if BackgroundLite.disabled
    [:in_process]
  else
    [options.delete(:handler) || config[:handler] || BackgroundLite::Config.default_handler].flatten
  end
  reporter = options.delete(:reporter) || config[:reporter] || BackgroundLite::Config.default_error_reporter
  logger = options.delete(:logger) || config[:logger] || BackgroundLite::Config.default_logger
  
  handler.each do |hand|
    if hand.is_a? Hash
      raise "Malformed handler options Hash" if hand.keys.size != 1
      options = hand.values.first
      hand = hand.keys.first
    end
    options ||= {}
    
    begin
      BackgroundLite.disable do
        if logger.debug?
          # Transaction ID is currently only used for debugging to find corresponding
          # messages on both sides of the queue. It is optional and should be expected 
          # to be nil
          options[:transaction_id] = Digest::SHA1.hexdigest(object.to_s + method.to_s + args.inspect + Time.now.to_s)
          logger.debug("Sending to background: Object: #{object.inspect} Method: #{method} Args: #{args.inspect} Options: #{options.inspect}")      
        end
        
        time = Benchmark.realtime do
          "BackgroundLite::#{hand.to_s.camelize}Handler".constantize.handle(object, method, args, options)
        end

        if BackgroundLite::Config.slow_threshold > 0 && time > BackgroundLite::Config.slow_threshold
          logger.fatal("Slow background job (#{time}s): #{object.class.name}##{method}(#{args.inspect}) on object #{object.inspect}")
        end
      end
      
      return hand
    rescue Exception => e
      "BackgroundLite::#{reporter.to_s.camelize}ErrorReporter".constantize.report(e)
    end
  end
  return nil
end