active_record_mutex 2.3.3 → 2.3.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60ad881c3d0757d14d5ec9c12ac3740b95ba0d32
|
4
|
+
data.tar.gz: e0f2cc67ac0407f5652f40931d17e9785757bcd3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 690678e820f5849c845fd5c992832e561cecc4e8341ede697c7cdac0cd5b34ba551f4b5f7f4911d7f948c24d3f3890b7acc36227dfa395ccc046e33fd1b54b21
|
7
|
+
data.tar.gz: 6c0feddd0d11bd30893842b129c51ca8deea17a1c8dfb19bb1ed1a732389051d27ef036ebd0ac2b4cadfb3da4db6721fc443d1323359cdba656dfa738bcd03b5
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.3.
|
1
|
+
2.3.4
|
data/active_record_mutex.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: active_record_mutex 2.3.
|
2
|
+
# stub: active_record_mutex 2.3.4 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "active_record_mutex"
|
6
|
-
s.version = "2.3.
|
6
|
+
s.version = "2.3.4"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib"]
|
10
10
|
s.authors = ["Florian Frank"]
|
11
|
-
s.date = "2016-08-
|
11
|
+
s.date = "2016-08-25"
|
12
12
|
s.description = "Mutex that can be used to synchronise ruby processes via an ActiveRecord datababase connection. (Only Mysql is supported at the moment.)"
|
13
13
|
s.email = "flori@ping.de"
|
14
14
|
s.extra_rdoc_files = ["README.md", "lib/active_record/database_mutex.rb", "lib/active_record/database_mutex/implementation.rb", "lib/active_record/database_mutex/version.rb", "lib/active_record/mutex.rb", "lib/active_record_mutex.rb"]
|
@@ -5,26 +5,21 @@ module ActiveRecord
|
|
5
5
|
module DatabaseMutex
|
6
6
|
class Implementation
|
7
7
|
|
8
|
-
class << self
|
9
|
-
def db
|
10
|
-
ActiveRecord::Base.connection
|
11
|
-
end
|
12
|
-
|
13
|
-
def check_size?
|
14
|
-
if defined? @check_size
|
15
|
-
@check_size
|
16
|
-
else
|
17
|
-
version = db.execute("SHOW VARIABLES LIKE 'version'").first.last.
|
18
|
-
delete('^0-9.').version
|
19
|
-
@check_size = version >= '5.7'.version
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
8
|
# Creates a mutex with the name given with the option :name.
|
25
9
|
def initialize(opts = {})
|
26
10
|
@name = opts[:name] or raise ArgumentError, "mutex requires a :name argument"
|
27
|
-
|
11
|
+
query %{
|
12
|
+
CREATE TEMPORARY TABLE IF NOT EXISTS mutex_counters
|
13
|
+
(
|
14
|
+
name CHAR(255) NOT NULL,
|
15
|
+
counter INT UNSIGNED NOT NULL DEFAULT 1,
|
16
|
+
PRIMARY KEY (name)
|
17
|
+
)
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def db
|
22
|
+
ActiveRecord::Base.connection
|
28
23
|
end
|
29
24
|
|
30
25
|
# Returns the name of this mutex as given as a constructor argument.
|
@@ -82,7 +77,7 @@ module ActiveRecord
|
|
82
77
|
if aquired_lock?
|
83
78
|
decrease_counter
|
84
79
|
if counter_zero?
|
85
|
-
case query
|
80
|
+
case query %{ SELECT RELEASE_LOCK(#{quote(name)}) }
|
86
81
|
when 1
|
87
82
|
true
|
88
83
|
when 0, nil
|
@@ -105,7 +100,7 @@ module ActiveRecord
|
|
105
100
|
|
106
101
|
# Returns true if this mutex is unlocked at the moment.
|
107
102
|
def unlocked?
|
108
|
-
query(
|
103
|
+
query(%{ SELECT IS_FREE_LOCK(#{quote(name)}) }).to_i == 1
|
109
104
|
end
|
110
105
|
|
111
106
|
# Returns true if this mutex is locked at the moment.
|
@@ -136,24 +131,26 @@ module ActiveRecord
|
|
136
131
|
ActiveRecord::Base.connection.quote(value)
|
137
132
|
end
|
138
133
|
|
139
|
-
def counter
|
140
|
-
encoded_name = ?$ + Base64.encode64(name).delete('^A-Za-z0-9+/').
|
141
|
-
gsub(/[+\/]/, ?+ => ?_, ?/ => ?.)
|
142
|
-
if !self.class.check_size? || encoded_name.size <= 64 # mysql 5.7 only allows size <=64 variable names
|
143
|
-
"@#{encoded_name}"
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
134
|
def increase_counter
|
148
|
-
query
|
135
|
+
query %{
|
136
|
+
INSERT INTO mutex_counters (name)
|
137
|
+
VALUES (#{quote(@name)})
|
138
|
+
ON DUPLICATE KEY UPDATE counter = counter + 1
|
139
|
+
}
|
149
140
|
end
|
150
141
|
|
151
142
|
def decrease_counter
|
152
|
-
query
|
143
|
+
query %{
|
144
|
+
UPDATE mutex_counters SET counter = counter - 1
|
145
|
+
WHERE name = #{quote(@name)}
|
146
|
+
}
|
153
147
|
end
|
154
148
|
|
155
149
|
def counter_value
|
156
|
-
query(
|
150
|
+
query(%{
|
151
|
+
SELECT counter FROM mutex_counters
|
152
|
+
WHERE name = #{quote(@name)}
|
153
|
+
}).to_i
|
157
154
|
end
|
158
155
|
|
159
156
|
def counter_zero?
|
@@ -166,7 +163,7 @@ module ActiveRecord
|
|
166
163
|
true
|
167
164
|
else
|
168
165
|
timeout = opts[:timeout] || 1
|
169
|
-
case query
|
166
|
+
case query %{ SELECT GET_LOCK(#{quote(name)}, #{timeout}) }
|
170
167
|
when 1
|
171
168
|
increase_counter
|
172
169
|
true
|
@@ -178,16 +175,13 @@ module ActiveRecord
|
|
178
175
|
end
|
179
176
|
end
|
180
177
|
|
181
|
-
def query(sql)
|
182
|
-
if
|
183
|
-
|
184
|
-
$DEBUG and warn %{query("#{sql}") = #{
|
178
|
+
def query(sql, index: 0)
|
179
|
+
if row = db.select_rows(sql).first
|
180
|
+
value = index ? row[index] : row
|
181
|
+
$DEBUG and warn %{query("#{sql}", #{index.inspect}) = #{value.inspect}}
|
182
|
+
value
|
185
183
|
end
|
186
|
-
result
|
187
|
-
rescue ActiveRecord::StatementInvalid
|
188
|
-
nil
|
189
184
|
end
|
190
185
|
end
|
191
186
|
end
|
192
187
|
end
|
193
|
-
|
data/test/database_mutex_test.rb
CHANGED
@@ -146,9 +146,4 @@ class DatabaseMutexTest < Test::Unit::TestCase
|
|
146
146
|
end
|
147
147
|
assert_nil mutex.synchronize {}
|
148
148
|
end
|
149
|
-
|
150
|
-
def test_counter_name
|
151
|
-
mutex = Implementation.new(:name => (250..255).map(&:chr) * '')
|
152
|
-
assert_equal '@$_vv8.f7.', mutex.send(:counter)
|
153
|
-
end
|
154
149
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_mutex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gem_hadar
|