lib/mongo-lock.rb in mongo-lock-1.1.0 vs lib/mongo-lock.rb in mongo-lock-1.1.1
- old
+ new
@@ -1,9 +1,14 @@
require 'mongo-lock/configuration'
-require 'mongo-lock/queries'
+require 'mongo-lock/mongo_queries'
require 'mongo-lock/class_convenience_methods'
+# If we are using Rails then we will include the Mongo::Lock railtie.
+if defined?(Rails)
+ require "mongo-lock/railtie"
+end
+
module Mongo
class Lock
extend Mongo::Lock::ClassConvenienceMethods
@@ -37,65 +42,63 @@
else
@@default_configuration = configure
end
end
-
- def self.release_all options = {}
- if options.include? :collection
- Mongo::Lock::Queries.release_collection configuration.collection(options[:collection]), options[:owner]
- else
- configuration.collections.each_pair do |key,collection|
- Mongo::Lock::Queries.release_collection collection, options[:owner]
- end
- end
- end
-
def self.ensure_indexes
configuration.collections.each_pair do |key, collection|
- Mongo::Lock::Queries.ensure_indexes collection
+ Mongo::Lock::MongoQueries.ensure_indexes collection
end
end
- def self.clear_expired
- configuration.collections.each_pair do |key,collection|
- Mongo::Lock::Queries.clear_expired collection
+ def self.clear_expired options = {}
+ options = configuration.process_collection_options options
+
+ options[:collections].each do |collection|
+ Mongo::Lock::MongoQueries.clear_expired collection
end
end
+ def self.release_all options = {}
+ options = configuration.process_collection_options options
+ options[:collections].each do |collection|
+ Mongo::Lock::MongoQueries.release_collection collection, options[:owner]
+ end
+ end
+
def initialize key, options = {}
self.configuration = Configuration.new self.class.configuration.to_hash, options
- self.key = key
- self.query = Mongo::Lock::Queries.new self
+ self.key = retrieve_lock_key key
+ self.query = Mongo::Lock::MongoQueries.new self
acquire_if_acquired
end
# API
def configure options = {}, &block
self.configuration = Configuration.new self.configuration.to_hash, options
yield self.configuration if block_given?
end
- def acquire options = {}
+ def acquire options = {}, &block
options = inherit_options options
i = 1
time_spent = 0
loop do
- result = try_acquire options, i, time_spent
+ result = try_acquire options, i, time_spent, &block
return result unless result.nil?
frequency = call_if_proc options[:frequency], i
sleep frequency
time_spent += frequency
i += 1
end
end
- def try_acquire options, i, time_spent
+ def try_acquire options, i, time_spent, &block
# If timeout has expired
if options[:timeout_in] && options[:timeout_in] < time_spent
return raise_or_false options
# If limit has expired
@@ -112,14 +115,22 @@
end
# If the lock was acquired
else
self.acquired = true
- return true
+ return call_block options, &block
end
end
+ def call_block options, &block
+ if block_given?
+ yield self
+ release(options)
+ end
+ true
+ end
+
def release options = {}
options = inherit_options options
# If the lock has already been released
if released?
@@ -148,18 +159,14 @@
end
def extend_by time, options = {}
options = inherit_options options
- # Can't extend a lock that hasn't been acquired
- if !acquired?
+ # Can't extend a lock that hasn't been acquired or expired
+ if !acquired? || expired?
return raise_or_false options, NotExtendedError
- # Can't extend a lock that has started
- elsif expired?
- return raise_or_false options, NotExtendedError
-
else
query.find_and_update time, options
true
end
end
@@ -206,9 +213,17 @@
def released?
!!released
end
# Utils
+
+ def retrieve_lock_key key
+ case
+ when key.respond_to?(:lock_key) then key.lock_key
+ when key.is_a?(Array) then key.map { |element| retrieve_lock_key(element) }.to_param
+ else key.to_param
+ end.to_s
+ end
def acquire_if_acquired
self.acquired = true if query.is_acquired?
end