mysql_getlock 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13472db42b56d8508600971446847ed1eca62d47
4
- data.tar.gz: 3f29aaa3f8507db4ad7a4c5bb2785ee961cb5a49
3
+ metadata.gz: 5e4b268a546f480cf35e54bec236f82e2ee74f25
4
+ data.tar.gz: a11b170c1fd935e9f02b258b23185d1879fc952f
5
5
  SHA512:
6
- metadata.gz: 55970b8a826d111753244739c70c5ab97b5f12135e9813a78089ebc756a05eafb649e2d400b57ddbf650ba8d0f1188364d916807f9633c0d4641d7d1c1ed8c8a
7
- data.tar.gz: c2cf83488fa66076b885a966e73eaba7c893a53ffe47283e81b21d9773a7ee0180bb69f5fe9d590e3c6339e5136b8e5fbfc5ea01acd4d117976281f5632b7e26
6
+ metadata.gz: 94050442d95798cd3a54571c8662f87b09c8190aa1709ffe64f48f0aaea0f1119724560f93497b9ee78293f1186f2fe2af63a86b9617daf59155e490015883ba
7
+ data.tar.gz: 44e6fc1dbd8202f89fd87f8cc79f61f81572afbaeadc1dfd8e5ecea17d69c92e21d14dd3c56b9aa62373b986369613e32af827c56dc79a3a79f3843ce95e965f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.2.1 (2016-08-28)
2
+
3
+ Enhancements:
4
+
5
+ * Raises `LockError` if #synchronize failed to acquore a lock
6
+
1
7
  # 0.2.0 (2016-08-28)
2
8
 
3
9
  Enhancements:
data/README.md CHANGED
@@ -26,6 +26,7 @@ Note that
26
26
 
27
27
  1. Before 5.7.5, only a single simultaneous lock can be acquired in a session, and `get_lock()` releases any existing lock.
28
28
  * This gem raises `MysqlGetlock::Error` at `#lock` if another `get_lock()` for another key is issued in a session to prevent accidental releases of existing lock.
29
+ * MEMO: lock twice for the same key in a session does not block, only one lock is held for both mysql < 5.7.5 and >= 5.7.5.
29
30
  2. The key name is global in a mysql instance. It is advised to use database-specific or application-specific lock names such as `db_name.str` or `app_name.environment.str`
30
31
 
31
32
  ## Installation
@@ -49,11 +50,11 @@ Or install it yourself as:
49
50
  Similarly with ruby standard library [mutex](https://ruby-doc.org/core-2.2.0/Mutex.html), following methods are available:
50
51
 
51
52
  * lock
52
- * Attempts to grab the lock and waits if it isn’t availabl. Returns true if successfully acquird a lock.
53
+ * Attempts to grab the lock and waits if it isn’t available. Returns true if successfully acquired a lock
53
54
  * locked?
54
55
  * Returns true if this lock is currently held by some (including myself)
55
56
  * synchronize {}
56
- * Obtains a lock, runs the block, and releases the lock when the block completes.
57
+ * Obtains a lock, runs the block, and releases the lock when the block completes. Raises `MysqlGetlock::LockError` if failed to acquire a lock
57
58
  * unlock
58
59
  * Releases the lock. Returns true if successfully released a lock.
59
60
  * self_locked?
data/bin/try CHANGED
@@ -3,8 +3,23 @@
3
3
  require "bundler/setup"
4
4
  require "mysql_getlock"
5
5
  require 'logger'
6
+ require 'optparse'
6
7
 
7
- if ARGV[0] == 'exit!'
8
+ opts = {
9
+ timeout: -1,
10
+ kill: false
11
+ }
12
+ OptionParser.new.tap {|op|
13
+ op.on('--timeout VALUE') {|v|
14
+ opts[:timeout] = Float(v)
15
+ }
16
+ op.on('--kill') {|v|
17
+ opts[:kill] = true
18
+ }
19
+ op.parse(ARGV)
20
+ }
21
+
22
+ if opts[:kill]
8
23
  trap('INT') do
9
24
  exit!
10
25
  end
@@ -12,7 +27,8 @@ end
12
27
 
13
28
  mysql2 = Mysql2::Client.new
14
29
  mutex = MysqlGetlock.new(
15
- mysql2: mysql2, key: 'mysql_getlock2', logger: Logger.new(STDOUT)
30
+ mysql2: mysql2, key: 'mysql_getlock', logger: Logger.new(STDOUT),
31
+ timeout: opts[:timeout]
16
32
  )
17
33
  mutex.synchronize do
18
34
  loop do
@@ -1,3 +1,3 @@
1
1
  class MysqlGetlock
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/mysql_getlock.rb CHANGED
@@ -7,6 +7,7 @@ class MysqlGetlock
7
7
  TIMEOUT = -1 # inifinity
8
8
 
9
9
  class Error < ::StandardError; end
10
+ class LockError < ::StandardError; end
10
11
 
11
12
  def initialize(mysql2:, key:, logger: nil, timeout: TIMEOUT)
12
13
  self.mysql2 = mysql2
@@ -27,7 +28,7 @@ class MysqlGetlock
27
28
  raise Error, "get_lock() is already issued in the same connection for '#{current_session_key}'"
28
29
  end
29
30
 
30
- logger.info { "#{log_head}Wait acquiring a mysql lock '#{key}'" } if logger
31
+ logger.info { "#{log_head}Wait #{timeout < 0 ? '' : "#{timeout} sec "}to acquire a mysql lock '#{key}'" } if logger
31
32
  results = mysql2.query(%Q[select get_lock('#{key}', #{timeout})], as: :array)
32
33
  case results.to_a.first.first
33
34
  when 1
@@ -81,7 +82,7 @@ class MysqlGetlock
81
82
  end
82
83
 
83
84
  def synchronize(&block)
84
- lock
85
+ raise LockError unless lock
85
86
  begin
86
87
  yield
87
88
  ensure
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysql_getlock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo