lib/qu/backend/mongoid.rb in qu-mongoid-0.2.0 vs lib/qu/backend/mongoid.rb in qu-mongoid-0.2.1
- old
+ new
@@ -10,29 +10,42 @@
# Seconds to wait before try to reconnect after connection failure (default: 1)
attr_accessor :retry_frequency
# Seconds to wait before looking for more jobs when the queue is empty (default: 5)
attr_accessor :poll_frequency
+
+ attr_accessor :session
def initialize
self.max_retries = 5
self.retry_frequency = 1
self.poll_frequency = 5
+ self.session = :default
end
def connection
- @connection ||= begin
- unless ::Mongoid.sessions[:default]
+ Thread.current[self.to_s] ||= begin
+ unless ::Mongoid.sessions[@session]
if (uri = (ENV['MONGOHQ_URL'] || ENV['MONGOLAB_URI']).to_s) && !uri.empty?
::Mongoid.sessions = {:default => {:uri => uri, :max_retries_on_connection_failure => 4}}
else
::Mongoid.connect_to('qu')
end
end
- ::Mongoid::Sessions.default
+ ::Mongoid::Sessions.with_name(@session)
end
end
alias_method :database, :connection
+
+ def connection=(conn)
+ warn %q(If you are using threads, setting the Qu::Mongoid connection will not work. Configure the session instead.
+Example:
+ Qu.configure do |c|
+ c.backend.session = :qu
+ end
+)
+ Thread.current[self.to_s] = conn
+ end
def clear(queue = nil)
queue ||= queues + ['failed']
logger.info { "Clearing queues: #{queue.inspect}" }
Array(queue).each do |q|