db_lock 0.4 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/config/database_mssql.yml +17 -0
- data/config/database_mssql_example.yml +17 -0
- data/config/{database.yml → database_mysql.yml} +0 -0
- data/config/{database_example.yml → database_mysql_example.yml} +0 -0
- data/lib/db_lock/adapter/base.rb +17 -0
- data/lib/db_lock/adapter/mssql.rb +17 -0
- data/lib/db_lock/adapter/mysql.rb +17 -0
- data/lib/db_lock/adapter.rb +22 -0
- data/lib/db_lock/version.rb +1 -1
- data/lib/db_lock.rb +14 -5
- metadata +48 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19334766edb61a395f630243e0bec6c53827ddbf
|
4
|
+
data.tar.gz: 325f88fddbc51047c70be06820e398f5c01ea935
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10f2acba08e807842a1ec99187f21619b1c2a051bdec0eb92e035ea5034e309fe5081297ac4adde3a9cf221168d9014931859dac75e6dac0660e25bbaf3cc11e
|
7
|
+
data.tar.gz: 40463f4ec1e2765239f583d73a3d00ef5516a616fcc4526dd9fc9cc9be278f4347face79d1bfe5a0681d03918aa75bf7f27cf8df031f2fb89dbb1623162cfcc7
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# DBLock
|
2
2
|
|
3
|
-
Gem to obtain and release manual
|
3
|
+
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
|
+
|
5
|
+
- MySQL
|
6
|
+
- Microsoft SQL Server
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -29,4 +32,4 @@ If you prefix the lock with a `.` in a Rails application, `.` will be automatica
|
|
29
32
|
|
30
33
|
## Development
|
31
34
|
|
32
|
-
You will have to rename/copy `config/
|
35
|
+
You will have to rename/copy `config/database_mysql_example.yml` to `config/database_mysql.yml` and adjust it to your local settings. Same with the MS SQL configuration file.
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,17 @@
|
|
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
|
File without changes
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DBLock
|
2
|
+
module Adapter
|
3
|
+
class Base
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
def connection
|
9
|
+
DBLock.db_handler.connection
|
10
|
+
end
|
11
|
+
|
12
|
+
def sanitize_sql_array(*args)
|
13
|
+
DBLock.db_handler.send(:sanitize_sql_array, args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DBLock
|
2
|
+
module Adapter
|
3
|
+
class MSSQL < Base
|
4
|
+
def lock(name, timeout=0)
|
5
|
+
connection.execute_procedure 'sp_getapplock', Resource: name, LockMode: 'Exclusive', LockOwner: 'Session', LockTimeout: (timeout*1000).to_i, DbPrincipal: 'public'
|
6
|
+
lock = connection.raw_connection.return_code
|
7
|
+
lock == 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def release(name)
|
11
|
+
connection.execute_procedure 'sp_releaseapplock', Resource: name, LockOwner: 'Session', DbPrincipal: 'public'
|
12
|
+
lock = connection.raw_connection.return_code
|
13
|
+
lock == 0
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DBLock
|
2
|
+
module Adapter
|
3
|
+
class MYSQL < Base
|
4
|
+
def lock(name, timeout=0)
|
5
|
+
sql = sanitize_sql_array "SELECT GET_LOCK(?, ?)", name, timeout
|
6
|
+
res = connection.select_one sql
|
7
|
+
(res && res.values.first == 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
def release(name)
|
11
|
+
sql = sanitize_sql_array "SELECT RELEASE_LOCK(?)", name
|
12
|
+
res = connection.select_one sql
|
13
|
+
(res && res.values.first == 1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module DBLock
|
2
|
+
module Adapter
|
3
|
+
extend self
|
4
|
+
|
5
|
+
autoload :Base, "db_lock/adapter/base"
|
6
|
+
autoload :MSSQL, "db_lock/adapter/mssql"
|
7
|
+
autoload :MYSQL, "db_lock/adapter/mysql"
|
8
|
+
|
9
|
+
delegate :lock, :release, to: :implementation
|
10
|
+
|
11
|
+
def implementation
|
12
|
+
case DBLock.db_handler.connection.adapter_name.downcase
|
13
|
+
when 'mysql2'
|
14
|
+
MYSQL.instance
|
15
|
+
when 'sqlserver'
|
16
|
+
MSSQL.instance
|
17
|
+
else
|
18
|
+
raise "#{DBLock.db_handler.connection.adapter_name} is not implemented"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/db_lock/version.rb
CHANGED
data/lib/db_lock.rb
CHANGED
@@ -1,6 +1,17 @@
|
|
1
1
|
module DBLock
|
2
|
+
extend self
|
3
|
+
|
4
|
+
autoload :Adapter, "db_lock/adapter"
|
5
|
+
|
2
6
|
class AlreadyLocked < StandardError; end
|
3
7
|
|
8
|
+
attr_accessor :db_handler
|
9
|
+
|
10
|
+
def db_handler
|
11
|
+
# this must be an active record base object or subclass
|
12
|
+
@db_handler || ActiveRecord::Base
|
13
|
+
end
|
14
|
+
|
4
15
|
class Lock
|
5
16
|
class << self
|
6
17
|
def get(name, timeout=0)
|
@@ -11,17 +22,15 @@ module DBLock
|
|
11
22
|
|
12
23
|
name = prefixed_lock_name(name)
|
13
24
|
|
14
|
-
|
15
|
-
|
16
|
-
if @locked = (lock and lock.first and lock.first[0] == 1)
|
25
|
+
if Adapter.lock(name, timeout)
|
26
|
+
@locked = true
|
17
27
|
yield
|
18
28
|
else
|
19
29
|
raise AlreadyLocked.new("Unable to obtain lock '#{name}' within #{timeout} seconds") unless locked?
|
20
30
|
end
|
21
31
|
ensure
|
22
32
|
if locked?
|
23
|
-
|
24
|
-
ActiveRecord::Base.connection.execute(sql)
|
33
|
+
Adapter.release(name)
|
25
34
|
end
|
26
35
|
@locked = false
|
27
36
|
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:
|
4
|
+
version: 0.5.1
|
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-06-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -50,16 +50,50 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '3'
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '5'
|
54
57
|
type: :development
|
55
58
|
prerelease: false
|
56
59
|
version_requirements: !ruby/object:Gem::Requirement
|
57
60
|
requirements:
|
58
61
|
- - ">="
|
59
62
|
- !ruby/object:Gem::Version
|
60
|
-
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'
|
61
95
|
description: Obtain manual db locks to guard blocks of code from parallel execution.
|
62
|
-
Currently only supports mysql.
|
96
|
+
Currently only supports mysql and ms-sql-server.
|
63
97
|
email:
|
64
98
|
- konstantin@munteanu.de
|
65
99
|
executables: []
|
@@ -68,9 +102,15 @@ extra_rdoc_files: []
|
|
68
102
|
files:
|
69
103
|
- MIT-LICENSE
|
70
104
|
- README.md
|
71
|
-
- config/
|
72
|
-
- config/
|
105
|
+
- config/database_mssql.yml
|
106
|
+
- config/database_mssql_example.yml
|
107
|
+
- config/database_mysql.yml
|
108
|
+
- config/database_mysql_example.yml
|
73
109
|
- lib/db_lock.rb
|
110
|
+
- lib/db_lock/adapter.rb
|
111
|
+
- lib/db_lock/adapter/base.rb
|
112
|
+
- lib/db_lock/adapter/mssql.rb
|
113
|
+
- lib/db_lock/adapter/mysql.rb
|
74
114
|
- lib/db_lock/version.rb
|
75
115
|
homepage: https://github.com/mkon/db_lock
|
76
116
|
licenses:
|
@@ -92,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
132
|
version: '0'
|
93
133
|
requirements: []
|
94
134
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
135
|
+
rubygems_version: 2.5.1
|
96
136
|
signing_key:
|
97
137
|
specification_version: 4
|
98
138
|
summary: Obtain manual db/mysql locks
|