mysql_getlock 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +5 -2
- data/lib/mysql_getlock.rb +12 -2
- data/lib/mysql_getlock/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13472db42b56d8508600971446847ed1eca62d47
|
4
|
+
data.tar.gz: 3f29aaa3f8507db4ad7a4c5bb2785ee961cb5a49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 55970b8a826d111753244739c70c5ab97b5f12135e9813a78089ebc756a05eafb649e2d400b57ddbf650ba8d0f1188364d916807f9633c0d4641d7d1c1ed8c8a
|
7
|
+
data.tar.gz: c2cf83488fa66076b885a966e73eaba7c893a53ffe47283e81b21d9773a7ee0180bb69f5fe9d590e3c6339e5136b8e5fbfc5ea01acd4d117976281f5632b7e26
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,8 @@ MySQL `get_lock()` has a characteristic that the lock is implicitly released whe
|
|
24
24
|
|
25
25
|
Note that
|
26
26
|
|
27
|
-
1. Before 5.7.5, only a single simultaneous lock can be acquired in a session, and `
|
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
|
+
* 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.
|
28
29
|
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`
|
29
30
|
|
30
31
|
## Installation
|
@@ -50,11 +51,13 @@ Similarly with ruby standard library [mutex](https://ruby-doc.org/core-2.2.0/Mut
|
|
50
51
|
* lock
|
51
52
|
* Attempts to grab the lock and waits if it isn’t availabl. Returns true if successfully acquird a lock.
|
52
53
|
* locked?
|
53
|
-
* Returns true if this lock is currently held by some
|
54
|
+
* Returns true if this lock is currently held by some (including myself)
|
54
55
|
* synchronize {}
|
55
56
|
* Obtains a lock, runs the block, and releases the lock when the block completes.
|
56
57
|
* unlock
|
57
58
|
* Releases the lock. Returns true if successfully released a lock.
|
59
|
+
* self_locked?
|
60
|
+
* Returns true if this lock is currently held by myself.
|
58
61
|
|
59
62
|
Options of `MysqlGetlock.new` are:
|
60
63
|
|
data/lib/mysql_getlock.rb
CHANGED
@@ -66,8 +66,18 @@ class MysqlGetlock
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def locked?
|
69
|
-
results = mysql2.query(%Q[select
|
70
|
-
results.to_a.first.first
|
69
|
+
results = mysql2.query(%Q[select is_used_lock('#{key}')], as: :array)
|
70
|
+
!!results.to_a.first.first
|
71
|
+
end
|
72
|
+
|
73
|
+
# return true if locked by myself
|
74
|
+
def self_locked?
|
75
|
+
results = mysql2.query(%Q[select is_used_lock('#{key}')], as: :array)
|
76
|
+
lock_id = results.to_a.first.first
|
77
|
+
return nil if lock_id.nil?
|
78
|
+
results = mysql2.query(%Q[select connection_id()], as: :array)
|
79
|
+
self_id = results.to_a.first.first
|
80
|
+
self_id == lock_id
|
71
81
|
end
|
72
82
|
|
73
83
|
def synchronize(&block)
|