db_lock 0.5.1 → 0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -1
- data/lib/db_lock.rb +37 -27
- data/lib/db_lock/adapter.rb +2 -2
- data/lib/db_lock/adapter/{mssql.rb → sqlserver.rb} +1 -1
- data/lib/db_lock/version.rb +1 -1
- metadata +5 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daaa943a03637accade87635f95c4cc52e855d67
|
4
|
+
data.tar.gz: 211899d15e6544e477cc854f535e882515246423
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 761bd9af2d1940004962179670a347dda0f5a4ed1f2a3816e977b9e548ff97a50a771c9d319117de25768520ee2ee684b5d690ac71f2fba26839e6b37a0ae938
|
7
|
+
data.tar.gz: 43e564ea5c218cd1e4c89af02caa82e05adc543eaa6d1364ddeb755a7240bc3f9f111649c68754596728fa7a2ddb29deb5e3fa5a6ec5ddcf94f2cac9b5decaa5
|
data/README.md
CHANGED
@@ -28,8 +28,20 @@ The current implementation uses a class variable to store lock state so it is no
|
|
28
28
|
## Smart lock name
|
29
29
|
|
30
30
|
If you prefix the lock with a `.` in a Rails application, `.` will be automatically replaced with `YourAppName.environment` (production/development/etc).
|
31
|
+
If the lock name exceeds 64 characters, it will be replaced with a lock name of 64 characters, that consists of a pre- and suffix from the original lock name and a middle MD5 checksum.
|
31
32
|
|
32
33
|
|
33
34
|
## Development
|
34
35
|
|
35
|
-
|
36
|
+
Bundle with the adapter you want to use, for example
|
37
|
+
|
38
|
+
```
|
39
|
+
bundle --with mysql2
|
40
|
+
```
|
41
|
+
|
42
|
+
Run rspec with the database url env variables set. It will only run the specs it can run and skip the others.
|
43
|
+
|
44
|
+
For example
|
45
|
+
```
|
46
|
+
MYSQL_URL=mysql2://root:dummy@localhost/test SQLSERVER_URL=sqlserver://root:dummy@localhost/test rspec
|
47
|
+
```
|
data/lib/db_lock.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
1
3
|
module DBLock
|
2
4
|
extend self
|
3
5
|
|
@@ -12,38 +14,46 @@ module DBLock
|
|
12
14
|
@db_handler || ActiveRecord::Base
|
13
15
|
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
name = prefixed_lock_name(name)
|
24
|
-
|
25
|
-
if Adapter.lock(name, timeout)
|
26
|
-
@locked = true
|
27
|
-
yield
|
28
|
-
else
|
29
|
-
raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless locked?
|
30
|
-
end
|
31
|
-
ensure
|
32
|
-
if locked?
|
33
|
-
Adapter.release(name)
|
34
|
-
end
|
35
|
-
@locked = false
|
36
|
-
end
|
17
|
+
module Lock
|
18
|
+
extend self
|
19
|
+
|
20
|
+
def get(name, timeout=0)
|
21
|
+
timeout = timeout.to_f # catches nil
|
22
|
+
timeout = 0 if timeout < 0
|
23
|
+
raise "Invalid lock name: #{name.inspect}" if name.empty?
|
24
|
+
raise AlreadyLocked.new("Already lock in progress") if locked?
|
37
25
|
|
38
|
-
|
39
|
-
|
26
|
+
name = generate_lock_name(name)
|
27
|
+
|
28
|
+
if Adapter.lock(name, timeout)
|
29
|
+
@locked = true
|
30
|
+
yield
|
31
|
+
else
|
32
|
+
raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless locked?
|
33
|
+
end
|
34
|
+
ensure
|
35
|
+
if locked?
|
36
|
+
Adapter.release(name)
|
40
37
|
end
|
38
|
+
@locked = false
|
39
|
+
end
|
40
|
+
|
41
|
+
def locked?
|
42
|
+
@locked ||= false
|
43
|
+
end
|
41
44
|
|
42
|
-
|
45
|
+
private
|
43
46
|
|
44
|
-
|
45
|
-
|
47
|
+
def generate_lock_name(name)
|
48
|
+
if (name[0] == "." && defined? Rails)
|
49
|
+
name = "#{Rails.application.class.parent_name}.#{Rails.env}#{name}"
|
50
|
+
end
|
51
|
+
# reduce lock names of > 64 chars in size
|
52
|
+
# MySQL 5.7 only supports 64 chars max, there might be similar limitations elsewhere
|
53
|
+
if name.length > 64
|
54
|
+
name = "#{name.chars.first(15).join}-#{Digest::MD5.hexdigest(name)}-#{name.chars.last(15).join}"
|
46
55
|
end
|
56
|
+
name
|
47
57
|
end
|
48
58
|
end
|
49
59
|
end
|
data/lib/db_lock/adapter.rb
CHANGED
@@ -3,8 +3,8 @@ module DBLock
|
|
3
3
|
extend self
|
4
4
|
|
5
5
|
autoload :Base, "db_lock/adapter/base"
|
6
|
-
autoload :MSSQL, "db_lock/adapter/mssql"
|
7
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
|
|
@@ -13,7 +13,7 @@ module DBLock
|
|
13
13
|
when 'mysql2'
|
14
14
|
MYSQL.instance
|
15
15
|
when 'sqlserver'
|
16
|
-
|
16
|
+
Sqlserver.instance
|
17
17
|
else
|
18
18
|
raise "#{DBLock.db_handler.connection.adapter_name} is not implemented"
|
19
19
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module DBLock
|
2
2
|
module Adapter
|
3
|
-
class
|
3
|
+
class Sqlserver < Base
|
4
4
|
def lock(name, timeout=0)
|
5
5
|
connection.execute_procedure 'sp_getapplock', Resource: name, LockMode: 'Exclusive', LockOwner: 'Session', LockTimeout: (timeout*1000).to_i, DbPrincipal: 'public'
|
6
6
|
lock = connection.raw_connection.return_code
|
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: 0.
|
4
|
+
version: '0.6'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mkon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '3.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5'
|
22
|
+
version: '5.1'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '3.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5'
|
32
|
+
version: '5.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: rspec
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,54 +44,6 @@ 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: '3'
|
54
|
-
- - "<"
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version: '5'
|
57
|
-
type: :development
|
58
|
-
prerelease: false
|
59
|
-
version_requirements: !ruby/object:Gem::Requirement
|
60
|
-
requirements:
|
61
|
-
- - ">="
|
62
|
-
- !ruby/object:Gem::Version
|
63
|
-
version: '3'
|
64
|
-
- - "<"
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
version: '5'
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
|
-
name: tiny_tds
|
69
|
-
requirement: !ruby/object:Gem::Requirement
|
70
|
-
requirements:
|
71
|
-
- - "~>"
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
version: '1.0'
|
74
|
-
type: :development
|
75
|
-
prerelease: false
|
76
|
-
version_requirements: !ruby/object:Gem::Requirement
|
77
|
-
requirements:
|
78
|
-
- - "~>"
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version: '1.0'
|
81
|
-
- !ruby/object:Gem::Dependency
|
82
|
-
name: activerecord-sqlserver-adapter
|
83
|
-
requirement: !ruby/object:Gem::Requirement
|
84
|
-
requirements:
|
85
|
-
- - "~>"
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
version: '4.2'
|
88
|
-
type: :development
|
89
|
-
prerelease: false
|
90
|
-
version_requirements: !ruby/object:Gem::Requirement
|
91
|
-
requirements:
|
92
|
-
- - "~>"
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
version: '4.2'
|
95
47
|
description: Obtain manual db locks to guard blocks of code from parallel execution.
|
96
48
|
Currently only supports mysql and ms-sql-server.
|
97
49
|
email:
|
@@ -109,8 +61,8 @@ files:
|
|
109
61
|
- lib/db_lock.rb
|
110
62
|
- lib/db_lock/adapter.rb
|
111
63
|
- lib/db_lock/adapter/base.rb
|
112
|
-
- lib/db_lock/adapter/mssql.rb
|
113
64
|
- lib/db_lock/adapter/mysql.rb
|
65
|
+
- lib/db_lock/adapter/sqlserver.rb
|
114
66
|
- lib/db_lock/version.rb
|
115
67
|
homepage: https://github.com/mkon/db_lock
|
116
68
|
licenses:
|