Module: TorqueBox::Messaging::Backgroundable::BackgroundableClassMethods

Defined in:
messaging/lib/torquebox/messaging/backgroundable.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) __enable_backgroundable_newrelic_tracing(method)



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 102

def __enable_backgroundable_newrelic_tracing(method)
  method = method.to_s
  if Backgroundable.newrelic_available?
    TorqueBox::Messaging::Backgroundable::MUTEX.synchronize do
      @__enabled_bg_tracing_methods ||= {}
      if !@__enabled_bg_tracing_methods[method]
        include(NewRelic::Agent::Instrumentation::ControllerInstrumentation) unless
          include?(NewRelic::Agent::Instrumentation::ControllerInstrumentation)
        begin
          add_transaction_tracer(method, :name => method.sub("__sync_", ""), :category => :task)
        rescue Exception => e
          TorqueBox::Logger.new( Backgroundable ).error "Error loading New Relic for backgrounded method #{method.sub("__sync_", "")}: #{e}"
        end
        @__enabled_bg_tracing_methods[method] = true
      end
    end
  end
end

- (Object) __enable_backgrounding(method)



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 132

def __enable_backgrounding(method)
  singleton_method = Util.singleton_methods_include?(self, method)
  singleton = (class << self; self; end)

  if singleton_method

    TorqueBox::Logger.new( self ).
      warn("always_background called for :#{method}, but :#{method} " +
           "exists as both a class and instance method. Only the " +
           "class method will be backgrounded.") if Util.instance_methods_include?(self, method)

    privatize = Util.private_singleton_methods_include?(self, method)
    protect = Util.protected_singleton_methods_include?(self, method) unless privatize
  else
    privatize = Util.private_instance_methods_include?(self, method)
    protect = Util.protected_instance_methods_include?(self, method) unless privatize
  end

  async_method = "__async_#{method}"
  sync_method = "__sync_#{method}"

  @__backgroundable_methods[method][:backgrounding] = true
  options = @__backgroundable_methods[method][:options]

  (singleton_method ? singleton : self).class_eval do
    define_method async_method do |*args|
      Util.publish_message(self, sync_method, args, options)
    end
  end
  
  code = singleton_method ? "class << self" : ""
  code << %Q{
    alias_method :#{sync_method}, :#{method}
    alias_method :#{method}, :#{async_method}
  }
  code << %Q{
    #{privatize ? "private" : "protected"} :#{method}, :#{sync_method}, :#{async_method}
  } if privatize || protect
  code << "end" if singleton_method

  class_eval code          
ensure
  @__backgroundable_methods[method][:backgrounding] = nil
end

- (Object) __method_added(method)



123
124
125
126
127
128
129
130
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 123

def __method_added(method)
  method = method.to_s
  if @__backgroundable_methods &&
      @__backgroundable_methods[method] &&
      !@__backgroundable_methods[method][:backgrounding]
    __enable_backgrounding(method)
  end
end

- (Object) always_background(*methods)



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 63

def always_background(*methods)
  options = methods.last.is_a?(Hash) ? methods.pop : {}
  @__backgroundable_methods ||= {}

  methods.each do |method|
    method = method.to_s
    if !@__backgroundable_methods[method]
      @__backgroundable_methods[method] ||= { }
      @__backgroundable_methods[method][:options] = options
      if Util.singleton_methods_include?(self, method) ||
          Util.instance_methods_include?(self, method)
        __enable_backgrounding(method)
      end
    end
  end
end

- (Object) background(options = { })



85
86
87
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 85

def background(options = { })
  BackgroundProxy.new(self, options)
end

- (Object) method_added(method)



90
91
92
93
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 90

def method_added(method)
  super
  __method_added(method)
end

- (Object) singleton_method_added(method)



96
97
98
99
# File 'messaging/lib/torquebox/messaging/backgroundable.rb', line 96

def singleton_method_added(method)
  super
  __method_added(method)
end