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.2"
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
- #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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mysql_getlock.rb', line 29 def lock if !multiple_lockable? and (current_session_key and current_session_key != key) raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'" end logger.info { "#{log_head}Wait #{timeout < -1 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array) case results.to_a.first.first when 1 logger.info { "#{log_head}Acquired a mysql lock '#{key}'" } if logger set_current_session_key(key) true when 0 logger.info { "#{log_head}Timeout to acquire a mysql lock '#{key}'" } if logger release_current_session_key false else # nil logger.info { "#{log_head}Unknown Error to acquire a mysql lock '#{key}'" } if logger release_current_session_key false end end |
#locked? ⇒ Boolean
72 73 74 75 |
# File 'lib/mysql_getlock.rb', line 72 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
78 79 80 81 82 83 84 85 |
# File 'lib/mysql_getlock.rb', line 78 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
87 88 89 90 91 92 93 94 |
# File 'lib/mysql_getlock.rb', line 87 def synchronize(&block) raise LockError unless lock begin yield ensure unlock end end |
#unlock ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mysql_getlock.rb', line 52 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 |