lib/sucker_punch/backgroundable/job.rb in sucker_punch-backgroundable-0.1.0 vs lib/sucker_punch/backgroundable/job.rb in sucker_punch-backgroundable-0.2.0
- old
+ new
@@ -1,36 +1,70 @@
module SuckerPunch
module Backgroundable
+ module CallMethod
+ private
+
+ def instantiate?(options)
+ return true if SuckerPunch::Backgroundable.configuration.reload && !(!options[:reload].nil? && options[:reload] == false)
+ options[:reload]
+ end
+
+ def load(receiver)
+ receiver.respond_to?(:id) ? receiver.class.find(receiver.id) : receiver
+ end
+
+ def call(receiver, method, *args)
+ if defined?(ActiveRecord)
+ begin
+ ActiveRecord::Base.connection_pool.with_connection do
+ receiver.send(method, *args)
+ end
+ ensure
+ ActiveRecord::Base.connection_handler.clear_active_connections!
+ end
+ else
+ receiver.send(method, *args)
+ end
+ end
+ end
+
class Job
include SuckerPunch::Job
workers SuckerPunch::Backgroundable.configuration.workers
+ include CallMethod
def perform(receiver, method, args, options)
receiver = load(receiver) if instantiate?(options)
- if defined?(ActiveRecord)
- ActiveRecord::Base.connection_pool.with_connection do
- receiver.send(method, *args)
- end
- else
- receiver.send(method, *args)
- end
+ call(receiver, method, *args)
end
def later(sec, *args)
after(sec) { perform(*args) }
end
+ end
+
+ class JobRunner
+ include CallMethod
- private
+ def initialize(receiver, method, args, options)
+ @receiver, @method, @args, @options = receiver, method, args, options
+ end
- def instantiate?(options)
- return true if SuckerPunch::Backgroundable.configuration.reload && !(!options[:reload].nil? && options[:reload] == false)
- options[:reload]
+ def run(seconds = 0)
+ if SuckerPunch::Backgroundable.configuration.enabled
+ # run as SuckerPunch Job
+ if seconds > 0
+ Job.new.async.later(seconds, @receiver, @method, @args, @options)
+ else
+ Job.new.async.perform(@receiver, @method, @args, @options)
+ end
+ else
+ # run without SuckerPunch or Celluloid
+ @receiver = load(@receiver) if instantiate?(@options)
+ call(@receiver, @method, *@args)
end
-
- def load(receiver)
- receiver.respond_to?(:id) ? receiver.class.find(receiver.id) : receiver
- end
+ end
end
end
end
\ No newline at end of file