active_record_mutex 2.3.8 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -3
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/active_record_mutex.gemspec +7 -7
- data/lib/active_record/database_mutex/implementation.rb +36 -39
- data/lib/active_record/database_mutex/version.rb +1 -1
- data/test/database_mutex_test.rb +5 -0
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 868990d82f5cfc1f8948a56494b5264d67610031
|
4
|
+
data.tar.gz: 843d17dd76cfb98da16bce309f4b33a8595a65b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cbab372a12b534583abaede4466a91e99ec981670d73729f1282a80bd2f68f99af379781d47fb06523513af03b3a82e150da15ae6fe7c074b3032ad757628f06
|
7
|
+
data.tar.gz: 859681f8d7bbb4cf64544bf5a00adfd8007d929e028e06dd498ac8a501eef3f64637822006f548bb62da4287ca0c46a8635fff6b9a97d280aa580e13ae25997a
|
data/.travis.yml
CHANGED
data/Rakefile
CHANGED
@@ -19,7 +19,7 @@ GemHadar do
|
|
19
19
|
|
20
20
|
dependency 'mysql2', '~>0.3.0'
|
21
21
|
dependency 'activerecord', '~> 4.0'
|
22
|
-
dependency 'tins', '~> 1.12
|
22
|
+
dependency 'tins', '~> 1.12'
|
23
23
|
development_dependency 'test-unit', '~>3.0'
|
24
24
|
development_dependency 'byebug'
|
25
25
|
development_dependency 'simplecov'
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.0
|
data/active_record_mutex.gemspec
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: active_record_mutex 2.
|
2
|
+
# stub: active_record_mutex 2.4.0 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "active_record_mutex".freeze
|
6
|
-
s.version = "2.
|
6
|
+
s.version = "2.4.0"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib".freeze]
|
10
10
|
s.authors = ["Florian Frank".freeze]
|
11
|
-
s.date = "2016-
|
11
|
+
s.date = "2016-11-23"
|
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.)".freeze
|
13
13
|
s.email = "flori@ping.de".freeze
|
14
14
|
s.extra_rdoc_files = ["README.md".freeze, "lib/active_record/database_mutex.rb".freeze, "lib/active_record/database_mutex/implementation.rb".freeze, "lib/active_record/database_mutex/version.rb".freeze, "lib/active_record/mutex.rb".freeze, "lib/active_record_mutex.rb".freeze]
|
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.homepage = "http://github.com/flori/active_record_mutex".freeze
|
17
17
|
s.licenses = ["GPL-2".freeze]
|
18
18
|
s.rdoc_options = ["--title".freeze, "ActiveRecordMutex - Implementation of a Mutex for Active Record".freeze, "--main".freeze, "README.md".freeze]
|
19
|
-
s.rubygems_version = "2.6.
|
19
|
+
s.rubygems_version = "2.6.7".freeze
|
20
20
|
s.summary = "Implementation of a Mutex for Active Record".freeze
|
21
21
|
s.test_files = ["test/database_mutex_test.rb".freeze, "test/test_helper.rb".freeze]
|
22
22
|
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |s|
|
|
30
30
|
s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
|
31
31
|
s.add_runtime_dependency(%q<mysql2>.freeze, ["~> 0.3.0"])
|
32
32
|
s.add_runtime_dependency(%q<activerecord>.freeze, ["~> 4.0"])
|
33
|
-
s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.12
|
33
|
+
s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.12"])
|
34
34
|
else
|
35
35
|
s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.7.1"])
|
36
36
|
s.add_dependency(%q<test-unit>.freeze, ["~> 3.0"])
|
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
|
|
38
38
|
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
39
39
|
s.add_dependency(%q<mysql2>.freeze, ["~> 0.3.0"])
|
40
40
|
s.add_dependency(%q<activerecord>.freeze, ["~> 4.0"])
|
41
|
-
s.add_dependency(%q<tins>.freeze, ["~> 1.12
|
41
|
+
s.add_dependency(%q<tins>.freeze, ["~> 1.12"])
|
42
42
|
end
|
43
43
|
else
|
44
44
|
s.add_dependency(%q<gem_hadar>.freeze, ["~> 1.7.1"])
|
@@ -47,6 +47,6 @@ Gem::Specification.new do |s|
|
|
47
47
|
s.add_dependency(%q<simplecov>.freeze, [">= 0"])
|
48
48
|
s.add_dependency(%q<mysql2>.freeze, ["~> 0.3.0"])
|
49
49
|
s.add_dependency(%q<activerecord>.freeze, ["~> 4.0"])
|
50
|
-
s.add_dependency(%q<tins>.freeze, ["~> 1.12
|
50
|
+
s.add_dependency(%q<tins>.freeze, ["~> 1.12"])
|
51
51
|
end
|
52
52
|
end
|
@@ -6,29 +6,25 @@ module ActiveRecord
|
|
6
6
|
class Implementation
|
7
7
|
|
8
8
|
class << self
|
9
|
-
|
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
|
10
22
|
end
|
11
|
-
self.table_name = 'active_record_mutex_counters'
|
12
23
|
|
13
24
|
# Creates a mutex with the name given with the option :name.
|
14
25
|
def initialize(opts = {})
|
15
26
|
@name = opts[:name] or raise ArgumentError, "mutex requires a :name argument"
|
16
|
-
|
17
|
-
CREATE TABLE IF NOT EXISTS #{table_name}
|
18
|
-
(
|
19
|
-
name CHAR(255) NOT NULL,
|
20
|
-
counter INT UNSIGNED NOT NULL DEFAULT 1,
|
21
|
-
PRIMARY KEY (name(128))
|
22
|
-
) DEFAULT CHARSET=utf8mb4
|
23
|
-
}
|
24
|
-
end
|
25
|
-
|
26
|
-
def table_name
|
27
|
-
self.class.table_name
|
28
|
-
end
|
29
|
-
|
30
|
-
def db
|
31
|
-
ActiveRecord::Base.connection
|
27
|
+
counter or raise ArgumentError, 'argument :name is too long'
|
32
28
|
end
|
33
29
|
|
34
30
|
# Returns the name of this mutex as given as a constructor argument.
|
@@ -86,7 +82,7 @@ module ActiveRecord
|
|
86
82
|
if aquired_lock?
|
87
83
|
decrease_counter
|
88
84
|
if counter_zero?
|
89
|
-
case query
|
85
|
+
case query("SELECT RELEASE_LOCK(#{quote(name)})")
|
90
86
|
when 1
|
91
87
|
true
|
92
88
|
when 0, nil
|
@@ -109,7 +105,7 @@ module ActiveRecord
|
|
109
105
|
|
110
106
|
# Returns true if this mutex is unlocked at the moment.
|
111
107
|
def unlocked?
|
112
|
-
query(
|
108
|
+
query("SELECT IS_FREE_LOCK(#{quote(name)})") == 1
|
113
109
|
end
|
114
110
|
|
115
111
|
# Returns true if this mutex is locked at the moment.
|
@@ -140,26 +136,24 @@ module ActiveRecord
|
|
140
136
|
ActiveRecord::Base.connection.quote(value)
|
141
137
|
end
|
142
138
|
|
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
|
+
|
143
147
|
def increase_counter
|
144
|
-
query
|
145
|
-
INSERT INTO #{table_name} (name)
|
146
|
-
VALUES (#{quote(@name)})
|
147
|
-
ON DUPLICATE KEY UPDATE counter = counter + 1
|
148
|
-
}
|
148
|
+
query("SET #{counter} = IF(#{counter} IS NULL OR #{counter} = 0, 1, #{counter} + 1)")
|
149
149
|
end
|
150
150
|
|
151
151
|
def decrease_counter
|
152
|
-
query
|
153
|
-
UPDATE #{table_name} SET counter = counter - 1
|
154
|
-
WHERE name = #{quote(@name)}
|
155
|
-
}
|
152
|
+
query("SET #{counter} = #{counter} - 1")
|
156
153
|
end
|
157
154
|
|
158
155
|
def counter_value
|
159
|
-
query(
|
160
|
-
SELECT counter FROM #{table_name}
|
161
|
-
WHERE name = #{quote(@name)}
|
162
|
-
}).to_i
|
156
|
+
query("SELECT #{counter}").to_i
|
163
157
|
end
|
164
158
|
|
165
159
|
def counter_zero?
|
@@ -172,7 +166,7 @@ module ActiveRecord
|
|
172
166
|
true
|
173
167
|
else
|
174
168
|
timeout = opts[:timeout] || 1
|
175
|
-
case query
|
169
|
+
case query("SELECT GET_LOCK(#{quote(name)}, #{timeout})")
|
176
170
|
when 1
|
177
171
|
increase_counter
|
178
172
|
true
|
@@ -184,13 +178,16 @@ module ActiveRecord
|
|
184
178
|
end
|
185
179
|
end
|
186
180
|
|
187
|
-
def query(sql
|
188
|
-
if
|
189
|
-
|
190
|
-
$DEBUG and warn %{query("#{sql}"
|
191
|
-
value
|
181
|
+
def query(sql)
|
182
|
+
if result = self.class.db.execute(sql)
|
183
|
+
result = result.first.first.to_i
|
184
|
+
$DEBUG and warn %{query("#{sql}") = #{result}}
|
192
185
|
end
|
186
|
+
result
|
187
|
+
rescue ActiveRecord::StatementInvalid
|
188
|
+
nil
|
193
189
|
end
|
194
190
|
end
|
195
191
|
end
|
196
192
|
end
|
193
|
+
|
data/test/database_mutex_test.rb
CHANGED
@@ -146,4 +146,9 @@ 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
|
149
154
|
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.
|
4
|
+
version: 2.4.0
|
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-
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: gem_hadar
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 1.12
|
103
|
+
version: '1.12'
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 1.12
|
110
|
+
version: '1.12'
|
111
111
|
description: Mutex that can be used to synchronise ruby processes via an ActiveRecord
|
112
112
|
datababase connection. (Only Mysql is supported at the moment.)
|
113
113
|
email: flori@ping.de
|
@@ -160,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
160
|
version: '0'
|
161
161
|
requirements: []
|
162
162
|
rubyforge_project:
|
163
|
-
rubygems_version: 2.6.
|
163
|
+
rubygems_version: 2.6.7
|
164
164
|
signing_key:
|
165
165
|
specification_version: 4
|
166
166
|
summary: Implementation of a Mutex for Active Record
|