mysql_getlock 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88dca1f22a91ecae47c4cc919cf9ebbb7469cb7e
4
- data.tar.gz: 63f0e9adfdd7c2901f69c45fbd2518569397e461
3
+ metadata.gz: 1280092349ef7ab3f0ca7f8bc3c8a72f3d69f41b
4
+ data.tar.gz: b796df17269c811dbc37c7cb808dbb7740866201
5
5
  SHA512:
6
- metadata.gz: e6adaa01efcd040b6ae82fe31332ebf28c9558d31ec3be4d690c8858b1f5aab447e130bef82b70dd7621fcbd17913f36645e4d2b2a001be54f72033c11e527d5
7
- data.tar.gz: 374c9497d072e02722d2883acfe865cbb7eaf46f15024aba43d3e955f7471e11b5beffa304c63a2c2eb2de12cab5988cf39c9de9a66870f777c1839e6bf5616c
6
+ metadata.gz: 9d1ded0d17085cfbddb1adc5bab4d9b7c6f736a112fa38ba6a1f27d7b83df52a3cf8dc2a5e028cafdb61f5e0ffede99ff96cd5bc281923b8391f1f59692bb195
7
+ data.tar.gz: e1928bc99ee3e1a9994bdae57de7066c49e9d5642fc4f5f3cf264e544947ad40bcc6db10ecaac26d2f36e456fb51b5c4ff803109e16ce112d277e176fb6d4733
@@ -1,3 +1,9 @@
1
+ # 0.2.3 (2016-08-31)
2
+
3
+ Enhancements:
4
+
5
+ * Add #try_lock
6
+
1
7
  # 0.2.2 (2016-08-29)
2
8
 
3
9
  Enhancements:
data/README.md CHANGED
@@ -59,6 +59,8 @@ Similarly with ruby standard library [mutex](https://ruby-doc.org/core-2.2.0/Mut
59
59
  * Releases the lock. Returns true if successfully released a lock.
60
60
  * self_locked?
61
61
  * Returns true if this lock is currently held by myself.
62
+ * try_lock
63
+ * Attempts to grab the lock and returns immediately without waits. Returns true if successfully acquired a lock
62
64
 
63
65
  Options of `MysqlGetlock.new` are:
64
66
 
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mysql_getlock"
5
+ require 'logger'
6
+
7
+ mysql2 = Mysql2::Client.new
8
+ mutex = MysqlGetlock.new(
9
+ mysql2: mysql2, key: 'mysql_getlock', logger: Logger.new(STDOUT), timeout: -1,
10
+ )
11
+ if mutex.try_lock
12
+ loop do
13
+ puts mutex.locked?
14
+ sleep 1
15
+ end
16
+ else
17
+ puts 'exit'
18
+ end
@@ -27,26 +27,11 @@ class MysqlGetlock
27
27
  end
28
28
 
29
29
  def lock
30
- if !multiple_lockable? and (current_session_key and current_session_key != key)
31
- raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'"
32
- end
30
+ _lock(@timeout)
31
+ end
33
32
 
34
- logger.info { "#{log_head}Wait #{timeout < -1 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
35
- results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array)
36
- case results.to_a.first.first
37
- when 1
38
- logger.info { "#{log_head}Acquired a mysql lock '#{key}'" } if logger
39
- set_current_session_key(key)
40
- true
41
- when 0
42
- logger.info { "#{log_head}Timeout to acquire a mysql lock '#{key}'" } if logger
43
- release_current_session_key
44
- false
45
- else # nil
46
- logger.info { "#{log_head}Unknown Error to acquire a mysql lock '#{key}'" } if logger
47
- release_current_session_key
48
- false
49
- end
33
+ def try_lock
34
+ _lock(0)
50
35
  end
51
36
 
52
37
  def unlock
@@ -95,6 +80,38 @@ class MysqlGetlock
95
80
 
96
81
  private
97
82
 
83
+ def _lock(timeout)
84
+ if !multiple_lockable? and (current_session_key and current_session_key != key)
85
+ raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'"
86
+ end
87
+
88
+ if timeout == 0 # try_lock
89
+ # no wait
90
+ else
91
+ logger.info { "#{log_head}Wait #{timeout < -1 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
92
+ end
93
+
94
+ results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array)
95
+ case results.to_a.first.first
96
+ when 1
97
+ logger.info { "#{log_head}Acquired a mysql lock '#{key}'" } if logger
98
+ set_current_session_key(key)
99
+ true
100
+ when 0
101
+ if timeout == 0 # try_lock
102
+ logger.info { "#{log_head}A mysql lock is already acquired by the other session '#{key}'" } if logger
103
+ else
104
+ logger.info { "#{log_head}Timeout to acquire a mysql lock '#{key}'" } if logger
105
+ end
106
+ release_current_session_key
107
+ false
108
+ else # nil
109
+ logger.info { "#{log_head}Unknown Error to acquire a mysql lock '#{key}'" } if logger
110
+ release_current_session_key
111
+ false
112
+ end
113
+ end
114
+
98
115
  def set_timeout(timeout)
99
116
  if infinite_timeoutable?
100
117
  @timeout = timeout
@@ -1,3 +1,3 @@
1
1
  class MysqlGetlock
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_getlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-29 00:00:00.000000000 Z
11
+ date: 2016-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -85,6 +85,7 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - bin/try
88
+ - bin/try_lock
88
89
  - lib/mysql_getlock.rb
89
90
  - lib/mysql_getlock/version.rb
90
91
  - mysql_getlock.gemspec