db_lock 0.7 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -5
- data/lib/db_lock.rb +7 -12
- data/lib/db_lock/adapter.rb +3 -3
- data/lib/db_lock/adapter/mysql.rb +3 -3
- data/lib/db_lock/adapter/sqlserver.rb +11 -5
- data/lib/db_lock/version.rb +1 -1
- metadata +37 -12
- data/config/database_mssql.yml +0 -17
- data/config/database_mysql.yml +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1503d17317cf5228fe01da6a352ec350c2a4cbe0818ffbb92fd9cef52e0358f1
|
4
|
+
data.tar.gz: bee5dee60e1e58f3fb6b70d0d9f28383ac8dd4b915989bae98ed4db91d8389a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aac77d25cf15fb0e7bc1c334cc2ceb677f06d7b905e32b349ef1442bd12361264fe737b8da5bfeb409a44b64e589e826a794b732962f9c157afddbc84b97f847
|
7
|
+
data.tar.gz: bea10e12220d6a98aab6a98731116a2f3b113ffe5a6087f58c8561ba7e9fdbe9e9b0b206da1d25f079c672f27dd18c8708d56f6a8ca57f56f623cd49397ee671
|
data/README.md
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# DBLock
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/db_lock.svg)](https://badge.fury.io/rb/db_lock)
|
4
|
+
[![Build Status](https://travis-ci.org/mkon/db_lock.svg?branch=master)](https://travis-ci.org/mkon/db_lock)
|
5
|
+
|
3
6
|
Gem to obtain and release manual 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). Currently only supports:
|
4
7
|
|
5
8
|
- MySQL
|
@@ -23,7 +26,7 @@ end
|
|
23
26
|
|
24
27
|
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.
|
25
28
|
|
26
|
-
The current implementation uses a class variable to store lock state so it is not thread
|
29
|
+
The current implementation uses a class variable to store lock state so it is not thread-safe when using multiple threads to acquire/release locks.
|
27
30
|
|
28
31
|
## Smart lock name
|
29
32
|
|
@@ -35,13 +38,13 @@ If the lock name exceeds 64 characters, it will be replaced with a lock name of
|
|
35
38
|
|
36
39
|
Bundle with the adapter you want to use, for example
|
37
40
|
|
38
|
-
```
|
39
|
-
bundle --with
|
41
|
+
```bash
|
42
|
+
$ bundle --with mysql
|
40
43
|
```
|
41
44
|
|
42
45
|
Run rspec with the database url env variables set. It will only run the specs it can run and skip the others.
|
43
46
|
|
44
47
|
For example
|
45
|
-
```
|
46
|
-
MYSQL_URL=mysql2://root:dummy@localhost/test SQLSERVER_URL=sqlserver://root:dummy@localhost/test rspec
|
48
|
+
```bash
|
49
|
+
$ MYSQL_URL=mysql2://root:dummy@localhost/test SQLSERVER_URL=sqlserver://root:dummy@localhost/test rspec
|
47
50
|
```
|
data/lib/db_lock.rb
CHANGED
@@ -3,7 +3,7 @@ require 'digest/md5'
|
|
3
3
|
module DBLock
|
4
4
|
extend self
|
5
5
|
|
6
|
-
autoload :Adapter,
|
6
|
+
autoload :Adapter, 'db_lock/adapter'
|
7
7
|
|
8
8
|
class AlreadyLocked < StandardError; end
|
9
9
|
|
@@ -16,12 +16,11 @@ module DBLock
|
|
16
16
|
|
17
17
|
module Lock
|
18
18
|
extend self
|
19
|
-
|
20
|
-
def get(name, timeout=0)
|
19
|
+
def get(name, timeout = 0)
|
21
20
|
timeout = timeout.to_f # catches nil
|
22
|
-
timeout = 0 if timeout
|
21
|
+
timeout = 0 if timeout.negative?
|
23
22
|
raise "Invalid lock name: #{name.inspect}" if name.empty?
|
24
|
-
raise AlreadyLocked
|
23
|
+
raise AlreadyLocked, 'Already lock in progress' if locked?
|
25
24
|
|
26
25
|
name = generate_lock_name(name)
|
27
26
|
|
@@ -29,12 +28,10 @@ module DBLock
|
|
29
28
|
@locked = true
|
30
29
|
yield
|
31
30
|
else
|
32
|
-
raise AlreadyLocked
|
31
|
+
raise AlreadyLocked, "Unable to obtain lock '#{name}' within #{timeout} seconds" unless locked?
|
33
32
|
end
|
34
33
|
ensure
|
35
|
-
if locked?
|
36
|
-
Adapter.release(name)
|
37
|
-
end
|
34
|
+
Adapter.release(name) if locked?
|
38
35
|
@locked = false
|
39
36
|
end
|
40
37
|
|
@@ -45,9 +42,7 @@ module DBLock
|
|
45
42
|
private
|
46
43
|
|
47
44
|
def generate_lock_name(name)
|
48
|
-
if
|
49
|
-
name = "#{Rails.application.class.parent_name}.#{Rails.env}#{name}"
|
50
|
-
end
|
45
|
+
name = "#{Rails.application.class.parent_name}.#{Rails.env}#{name}" if name[0] == '.' && defined? Rails
|
51
46
|
# reduce lock names of > 64 chars in size
|
52
47
|
# MySQL 5.7 only supports 64 chars max, there might be similar limitations elsewhere
|
53
48
|
if name.length > 64
|
data/lib/db_lock/adapter.rb
CHANGED
@@ -2,9 +2,9 @@ module DBLock
|
|
2
2
|
module Adapter
|
3
3
|
extend self
|
4
4
|
|
5
|
-
autoload :Base,
|
6
|
-
autoload :MYSQL,
|
7
|
-
autoload :Sqlserver,
|
5
|
+
autoload :Base, 'db_lock/adapter/base'
|
6
|
+
autoload :MYSQL, 'db_lock/adapter/mysql'
|
7
|
+
autoload :Sqlserver, 'db_lock/adapter/sqlserver'
|
8
8
|
|
9
9
|
delegate :lock, :release, to: :implementation
|
10
10
|
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module DBLock
|
2
2
|
module Adapter
|
3
3
|
class MYSQL < Base
|
4
|
-
def lock(name, timeout=0)
|
5
|
-
sql = sanitize_sql_array
|
4
|
+
def lock(name, timeout = 0)
|
5
|
+
sql = sanitize_sql_array 'SELECT GET_LOCK(?, ?)', name, timeout
|
6
6
|
res = connection.select_one sql
|
7
7
|
(res && res.values.first == 1)
|
8
8
|
end
|
9
9
|
|
10
10
|
def release(name)
|
11
|
-
sql = sanitize_sql_array
|
11
|
+
sql = sanitize_sql_array 'SELECT RELEASE_LOCK(?)', name
|
12
12
|
res = connection.select_one sql
|
13
13
|
(res && res.values.first == 1)
|
14
14
|
end
|
@@ -1,16 +1,22 @@
|
|
1
1
|
module DBLock
|
2
2
|
module Adapter
|
3
3
|
class Sqlserver < Base
|
4
|
-
def lock(name, timeout=0)
|
5
|
-
connection.execute_procedure 'sp_getapplock', Resource: name,
|
4
|
+
def lock(name, timeout = 0)
|
5
|
+
connection.execute_procedure 'sp_getapplock', Resource: name,
|
6
|
+
LockMode: 'Exclusive',
|
7
|
+
LockOwner: 'Session',
|
8
|
+
LockTimeout: (timeout * 1000).to_i,
|
9
|
+
DbPrincipal: 'public'
|
6
10
|
lock = connection.raw_connection.return_code
|
7
|
-
lock
|
11
|
+
lock.zero?
|
8
12
|
end
|
9
13
|
|
10
14
|
def release(name)
|
11
|
-
connection.execute_procedure 'sp_releaseapplock', Resource: name,
|
15
|
+
connection.execute_procedure 'sp_releaseapplock', Resource: name,
|
16
|
+
LockOwner: 'Session',
|
17
|
+
DbPrincipal: 'public'
|
12
18
|
lock = connection.raw_connection.return_code
|
13
|
-
lock
|
19
|
+
lock.zero?
|
14
20
|
end
|
15
21
|
end
|
16
22
|
end
|
data/lib/db_lock/version.rb
CHANGED
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:
|
4
|
+
version: 0.8.0
|
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: 2020-04-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,20 +16,20 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
29
|
+
version: '4.2'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rspec
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,8 +44,36 @@ dependencies:
|
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '3.7'
|
47
|
-
|
48
|
-
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rubocop
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.78.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.78.0
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rubocop-rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.37.1
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.37.1
|
75
|
+
description: Obtain manual db locks to guard blocks of code from parallel execution.Currently
|
76
|
+
only supports mysql and ms-sql-server.
|
49
77
|
email:
|
50
78
|
- konstantin@munteanu.de
|
51
79
|
executables: []
|
@@ -54,9 +82,7 @@ extra_rdoc_files: []
|
|
54
82
|
files:
|
55
83
|
- MIT-LICENSE
|
56
84
|
- README.md
|
57
|
-
- config/database_mssql.yml
|
58
85
|
- config/database_mssql_example.yml
|
59
|
-
- config/database_mysql.yml
|
60
86
|
- config/database_mysql_example.yml
|
61
87
|
- lib/db_lock.rb
|
62
88
|
- lib/db_lock/adapter.rb
|
@@ -83,8 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
109
|
- !ruby/object:Gem::Version
|
84
110
|
version: '0'
|
85
111
|
requirements: []
|
86
|
-
|
87
|
-
rubygems_version: 2.7.5
|
112
|
+
rubygems_version: 3.1.2
|
88
113
|
signing_key:
|
89
114
|
specification_version: 4
|
90
115
|
summary: Obtain manual db/mysql locks
|
data/config/database_mssql.yml
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# rename/copy to database.yml and adjust to your local settings
|
2
|
-
|
3
|
-
base: &base
|
4
|
-
adapter: sqlserver
|
5
|
-
host: 192.168.99.100
|
6
|
-
port: 50012
|
7
|
-
encoding: utf8
|
8
|
-
reconnect: false
|
9
|
-
pool: 5
|
10
|
-
username: root
|
11
|
-
password: dummy
|
12
|
-
|
13
|
-
development:
|
14
|
-
<<: *base
|
15
|
-
|
16
|
-
test:
|
17
|
-
<<: *base
|