Class: MysqlGetlock
- Inherits:
-
Object
- Object
- MysqlGetlock
- Defined in:
- lib/mysql_getlock.rb,
lib/mysql_getlock/version.rb
Defined Under Namespace
Constant Summary collapse
- TIMEOUT =
inifinity
-1 # inifinity
- PSEUDO_INFINITE_TIMEOUT =
for < 5.5.8
4294967295
- VERSION =
"0.2.3"
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#mysql2 ⇒ Object
Returns the value of attribute mysql2.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT) ⇒ MysqlGetlock
constructor
A new instance of MysqlGetlock.
- #lock ⇒ Object
- #locked? ⇒ Boolean
-
#self_locked? ⇒ Boolean
return true if locked by myself.
- #synchronize(&block) ⇒ Object
- #try_lock ⇒ Object
- #unlock ⇒ Object
Constructor Details
#initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT) ⇒ MysqlGetlock
Returns a new instance of MysqlGetlock.
13 14 15 16 17 18 |
# File 'lib/mysql_getlock.rb', line 13 def initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT) self.mysql2 = mysql2 set_key(key) set_logger(logger) set_timeout(timeout) end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/mysql_getlock.rb', line 5 def key @key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/mysql_getlock.rb', line 5 def logger @logger end |
#mysql2 ⇒ Object
Returns the value of attribute mysql2.
5 6 7 |
# File 'lib/mysql_getlock.rb', line 5 def mysql2 @mysql2 end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
5 6 7 |
# File 'lib/mysql_getlock.rb', line 5 def timeout @timeout end |
Instance Method Details
#lock ⇒ Object
29 30 31 |
# File 'lib/mysql_getlock.rb', line 29 def lock _lock(@timeout) end |
#locked? ⇒ Boolean
57 58 59 60 |
# File 'lib/mysql_getlock.rb', line 57 def locked? results = mysql2.query(%Q[select is_used_lock('#{key}')], as: :array) !!results.to_a.first.first end |
#self_locked? ⇒ Boolean
return true if locked by myself
63 64 65 66 67 68 69 70 |
# File 'lib/mysql_getlock.rb', line 63 def self_locked? results = mysql2.query(%Q[select is_used_lock('#{key}')], as: :array) lock_id = results.to_a.first.first return nil if lock_id.nil? results = mysql2.query(%Q[select connection_id()], as: :array) self_id = results.to_a.first.first self_id == lock_id end |
#synchronize(&block) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/mysql_getlock.rb', line 72 def synchronize(&block) raise LockError unless lock begin yield ensure unlock end end |
#try_lock ⇒ Object
33 34 35 |
# File 'lib/mysql_getlock.rb', line 33 def try_lock _lock(0) end |
#unlock ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mysql_getlock.rb', line 37 def unlock if !multiple_lockable? and (current_session_key and current_session_key != key) raise Error, "get_lock() was issued for another key '#{current_session_key}', please unlock it beforehand" end results = mysql2.query(%Q[select release_lock('#{key}')], as: :array) release_current_session_key case results.to_a.first.first when 1 logger.info { "#{log_head}Released a mysql lock '#{key}'" } if logger true when 0 logger.info { "#{log_head}Failed to release a mysql lock since somebody else locked '#{key}'" } if logger false else # nil logger.info { "#{log_head}Mysql lock did not exist '#{key}'" } if logger true end end |