db_lock 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -3
- data/config/database.yml +14 -0
- data/config/database_example.yml +16 -0
- data/lib/db_lock/version.rb +1 -1
- data/lib/db_lock.rb +13 -11
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b31aa49ced9bf1f6fc1ae0ebc3018a7feb1e3738
|
4
|
+
data.tar.gz: 8de321f98a12ad5da8dd5906e7a2753e0e13b7e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 701e854fdb2411b7754e2adad66f9441a16a2dcab2c0331424b2b45de3f7bea6ca8e08bdd5c67f1d18069cd5e4696b2af1cb33eb6659d7b1c70b04b682bfbd88
|
7
|
+
data.tar.gz: b686dd636d4e69708112fbac457525ecd5f692f0152d132752c8250e64d2c6bdfae5a1f71deedde790b685388f8b5019f2fb686a41aa9f3de4435a03e8fd8423
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# DBLock
|
2
2
|
|
3
|
-
Gem to obtain and release manual mysql db locks. This can be utilized for example to make sure that certain rake tasks do not run in parallel on the same database (for
|
3
|
+
Gem to obtain and release manual mysql db locks. This can be utilized for example to make sure that certain rake tasks do not run in parallel on the same database (for example when cron jobs run for too long or are accidentally started multiple times).
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,8 +18,15 @@ DBLock::Lock.get('name_of_lock', 5) do
|
|
18
18
|
end
|
19
19
|
```
|
20
20
|
|
21
|
-
Before the code block is executed, it will attempt to
|
21
|
+
Before the code block is executed, it will attempt to acquire a mysql db lock for X seconds (5 in this example). If this fails it will raise an `DBLock::AlreadyLocked` error. The lock is released after the block is executed, even if the block raised an error itself.
|
22
|
+
|
23
|
+
The current implementation uses a class variable to store lock state so it is not thread save when using multiple threads to aquire/release locks.
|
22
24
|
|
23
25
|
## Smart lock name
|
24
26
|
|
25
|
-
If you prefix the lock with a `.` in a Rails application, `.` will be automatically replaced with `YourAppName
|
27
|
+
If you prefix the lock with a `.` in a Rails application, `.` will be automatically replaced with `YourAppName.environment` (production/development/etc).
|
28
|
+
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
You will have to rename/copy `config/database_example.yml` to `config/database.yml` and adjust it to your local settings.
|
data/config/database.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# rename/copy to database.yml and adjust to your local settings
|
2
|
+
|
3
|
+
base: &base
|
4
|
+
adapter: mysql2
|
5
|
+
host: localhost
|
6
|
+
encoding: utf8
|
7
|
+
reconnect: false
|
8
|
+
pool: 5
|
9
|
+
username: root
|
10
|
+
password: dummy
|
11
|
+
|
12
|
+
development:
|
13
|
+
<<: *base
|
14
|
+
|
15
|
+
test:
|
16
|
+
<<: *base
|
data/lib/db_lock/version.rb
CHANGED
data/lib/db_lock.rb
CHANGED
@@ -6,17 +6,23 @@ module DBLock
|
|
6
6
|
def get(name, timeout=0)
|
7
7
|
timeout = timeout.to_i # catches nil
|
8
8
|
timeout = 0 if timeout < 0
|
9
|
-
raise "
|
9
|
+
raise "Invalid lock name: #{name.inspect}" if name.empty?
|
10
10
|
raise AlreadyLocked.new("Already lock in progress") if locked?
|
11
|
+
|
11
12
|
name = prefixed_lock_name(name)
|
12
|
-
|
13
|
-
|
13
|
+
|
14
|
+
sql = ActiveRecord::Base.send(:sanitize_sql_array, ["SELECT GET_LOCK(?, ?)", name, timeout])
|
15
|
+
lock = ActiveRecord::Base.connection.execute(sql)
|
16
|
+
if @locked = (lock and lock.first and lock.first[0] == 1)
|
14
17
|
yield
|
15
18
|
else
|
16
|
-
raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless
|
19
|
+
raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless locked?
|
17
20
|
end
|
18
21
|
ensure
|
19
|
-
|
22
|
+
if locked?
|
23
|
+
sql = ActiveRecord::Base.send(:sanitize_sql_array, ["SELECT RELEASE_LOCK(?)", name])
|
24
|
+
ActiveRecord::Base.connection.execute(sql)
|
25
|
+
end
|
20
26
|
@locked = false
|
21
27
|
end
|
22
28
|
|
@@ -24,14 +30,10 @@ module DBLock
|
|
24
30
|
@locked ||= false
|
25
31
|
end
|
26
32
|
|
27
|
-
|
33
|
+
private
|
28
34
|
|
29
35
|
def prefixed_lock_name(name)
|
30
|
-
|
31
|
-
name = "#{Rails.application.class.parent_name}#{name}"
|
32
|
-
else
|
33
|
-
name
|
34
|
-
end
|
36
|
+
(name[0] == "." && defined? Rails) ? "#{Rails.application.class.parent_name}.#{Rails.env}#{name}" : name
|
35
37
|
end
|
36
38
|
end
|
37
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mkon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -44,6 +44,20 @@ dependencies:
|
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '3.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: mysql2
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
47
61
|
description: Obtain manual db locks to guard blocks of code from parallel execution.
|
48
62
|
Currently only supports mysql.
|
49
63
|
email:
|
@@ -54,6 +68,8 @@ extra_rdoc_files: []
|
|
54
68
|
files:
|
55
69
|
- MIT-LICENSE
|
56
70
|
- README.md
|
71
|
+
- config/database.yml
|
72
|
+
- config/database_example.yml
|
57
73
|
- lib/db_lock.rb
|
58
74
|
- lib/db_lock/version.rb
|
59
75
|
homepage: https://github.com/mkon/db_lock
|