-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulled out some functionality into distinct modules
- Loading branch information
Showing
6 changed files
with
136 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Mongo | ||
class Lock | ||
module Acquisition | ||
|
||
def acquire options = {}, &block | ||
options = inherit_options options | ||
i = 1 | ||
time_spent = 0 | ||
|
||
loop do | ||
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, &block | ||
|
||
# If timeout has expired | ||
if options[:timeout_in] && options[:timeout_in] < time_spent | ||
return raise_or_false options | ||
|
||
# If limit has expired | ||
elsif options[:limit] && options[:limit] < i | ||
return raise_or_false options | ||
|
||
# If there is an existing lock | ||
elsif existing_lock = driver.find_or_insert(options) | ||
# If the lock is owned by me | ||
if existing_lock['owner'] == options[:owner] | ||
self.acquired = true | ||
extend_by options[:expire_in] | ||
return true | ||
end | ||
|
||
# If the lock was acquired | ||
else | ||
self.acquired = true | ||
return call_block options, &block | ||
end | ||
end | ||
|
||
def acquire_if_acquired | ||
self.acquired = true if driver.is_acquired? | ||
end | ||
|
||
end | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
lib/mongo-lock/class_convenience_methods.rb → lib/mongo-lock/convenience_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Mongo | ||
class Lock | ||
module Extension | ||
|
||
def extend_by time, options = {} | ||
options = inherit_options options | ||
|
||
# Can't extend a lock that hasn't been acquired or expired | ||
if !acquired? || expired? | ||
return raise_or_false options, NotExtendedError | ||
|
||
else | ||
driver.find_and_update time, options | ||
true | ||
end | ||
end | ||
|
||
def extend options = {} | ||
time = configuration.to_hash.merge(options)[:expire_in] | ||
extend_by time, options | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module Mongo | ||
class Lock | ||
module Release | ||
|
||
def self.included base | ||
def base.release_all options = {} | ||
options = configuration.process_collection_options options | ||
|
||
options[:collections].each do |collection| | ||
configuration.driver.release_collection collection, options[:owner] | ||
end | ||
end | ||
end | ||
|
||
def release options = {} | ||
options = inherit_options options | ||
|
||
# If the lock has already been released | ||
if released? | ||
return true | ||
|
||
# If the lock has expired its as good as released | ||
elsif expired? | ||
self.released = true | ||
self.acquired = false | ||
return true | ||
|
||
# We must have acquired the lock to release it | ||
elsif !acquired? | ||
if acquire options.merge(should_raise: false) | ||
return release options | ||
else | ||
return raise_or_false options, NotReleasedError | ||
end | ||
|
||
else | ||
self.released = true | ||
self.acquired = false | ||
driver.remove options | ||
return true | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
2 changes: 1 addition & 1 deletion
2
lib/mongo-lock/send_with_raise_methods.rb → lib/mongo-lock/send_with_raise.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters