db_lock 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1503d17317cf5228fe01da6a352ec350c2a4cbe0818ffbb92fd9cef52e0358f1
4
- data.tar.gz: bee5dee60e1e58f3fb6b70d0d9f28383ac8dd4b915989bae98ed4db91d8389a6
3
+ metadata.gz: a421d9ec42cc7b443b198621952641959bb9726af41c3e76a7d5ce5e5e974fc0
4
+ data.tar.gz: d4b509db240d13dc43d1125b3ce57a868f8e2b812aca3b42d8939ddbf2c71503
5
5
  SHA512:
6
- metadata.gz: aac77d25cf15fb0e7bc1c334cc2ceb677f06d7b905e32b349ef1442bd12361264fe737b8da5bfeb409a44b64e589e826a794b732962f9c157afddbc84b97f847
7
- data.tar.gz: bea10e12220d6a98aab6a98731116a2f3b113ffe5a6087f58c8561ba7e9fdbe9e9b0b206da1d25f079c672f27dd18c8708d56f6a8ca57f56f623cd49397ee671
6
+ metadata.gz: 627cb66c61e7af78f1a8ace1e1eff8722561e7d8a9704ad0443ca9a7aeb774ec2cf83747598dcbca429fe9bd3e83841b6a2ff1eeb0a95834d319e35108e46fd4
7
+ data.tar.gz: 23cc2e806437a6b745391e1eedb4fb930bc8d32a5557426b5e4bb950934dd6b3229d2e2ed38f74bff4f453f457ae91ec2d73020e14867cbf04a8059eb86472ad
@@ -4,6 +4,7 @@ module DBLock
4
4
  extend self
5
5
 
6
6
  autoload :Adapter, 'db_lock/adapter'
7
+ autoload :Lock, 'db_lock/lock'
7
8
 
8
9
  class AlreadyLocked < StandardError; end
9
10
 
@@ -13,42 +14,4 @@ module DBLock
13
14
  # this must be an active record base object or subclass
14
15
  @db_handler || ActiveRecord::Base
15
16
  end
16
-
17
- module Lock
18
- extend self
19
- def get(name, timeout = 0)
20
- timeout = timeout.to_f # catches nil
21
- timeout = 0 if timeout.negative?
22
- raise "Invalid lock name: #{name.inspect}" if name.empty?
23
- raise AlreadyLocked, 'Already lock in progress' if locked?
24
-
25
- name = generate_lock_name(name)
26
-
27
- if Adapter.lock(name, timeout)
28
- @locked = true
29
- yield
30
- else
31
- raise AlreadyLocked, "Unable to obtain lock '#{name}' within #{timeout} seconds" unless locked?
32
- end
33
- ensure
34
- Adapter.release(name) if locked?
35
- @locked = false
36
- end
37
-
38
- def locked?
39
- @locked ||= false
40
- end
41
-
42
- private
43
-
44
- def generate_lock_name(name)
45
- name = "#{Rails.application.class.parent_name}.#{Rails.env}#{name}" if name[0] == '.' && defined? Rails
46
- # reduce lock names of > 64 chars in size
47
- # MySQL 5.7 only supports 64 chars max, there might be similar limitations elsewhere
48
- if name.length > 64
49
- name = "#{name.chars.first(15).join}-#{Digest::MD5.hexdigest(name)}-#{name.chars.last(15).join}"
50
- end
51
- name
52
- end
53
- end
54
17
  end
@@ -0,0 +1,49 @@
1
+ require 'digest/md5'
2
+
3
+ module DBLock
4
+ module Lock
5
+ extend self
6
+ def get(name, timeout = 0)
7
+ timeout = timeout.to_f # catches nil
8
+ timeout = 0 if timeout.negative?
9
+ raise "Invalid lock name: #{name.inspect}" if name.empty?
10
+ raise AlreadyLocked, 'Already lock in progress' if locked?
11
+
12
+ name = generate_lock_name(name)
13
+
14
+ if Adapter.lock(name, timeout)
15
+ @locked = true
16
+ yield
17
+ else
18
+ raise AlreadyLocked, "Unable to obtain lock '#{name}' within #{timeout} seconds" unless locked?
19
+ end
20
+ ensure
21
+ Adapter.release(name) if locked?
22
+ @locked = false
23
+ end
24
+
25
+ def locked?
26
+ @locked ||= false
27
+ end
28
+
29
+ private
30
+
31
+ def generate_lock_name(name)
32
+ name = "#{rails_app_name}.#{Rails.env}#{name}" if name[0] == '.' && defined? Rails
33
+ # reduce lock names of > 64 chars in size
34
+ # MySQL 5.7 only supports 64 chars max, there might be similar limitations elsewhere
35
+ if name.length > 64
36
+ name = "#{name.chars.first(15).join}-#{Digest::MD5.hexdigest(name)}-#{name.chars.last(15).join}"
37
+ end
38
+ name
39
+ end
40
+
41
+ def rails_app_name
42
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('6.0.0')
43
+ Rails.application.class.module_parent_name
44
+ else
45
+ Rails.application.class.parent_name
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module DBLock
2
- VERSION = '0.8.0'.freeze
2
+ VERSION = '0.8.1'.freeze
3
3
  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.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2020-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 0.78.0
53
+ version: 0.81.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - '='
59
59
  - !ruby/object:Gem::Version
60
- version: 0.78.0
60
+ version: 0.81.0
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rubocop-rspec
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -72,6 +72,20 @@ dependencies:
72
72
  - - '='
73
73
  - !ruby/object:Gem::Version
74
74
  version: 1.37.1
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
75
89
  description: Obtain manual db locks to guard blocks of code from parallel execution.Currently
76
90
  only supports mysql and ms-sql-server.
77
91
  email:
@@ -82,13 +96,12 @@ extra_rdoc_files: []
82
96
  files:
83
97
  - MIT-LICENSE
84
98
  - README.md
85
- - config/database_mssql_example.yml
86
- - config/database_mysql_example.yml
87
99
  - lib/db_lock.rb
88
100
  - lib/db_lock/adapter.rb
89
101
  - lib/db_lock/adapter/base.rb
90
102
  - lib/db_lock/adapter/mysql.rb
91
103
  - lib/db_lock/adapter/sqlserver.rb
104
+ - lib/db_lock/lock.rb
92
105
  - lib/db_lock/version.rb
93
106
  homepage: https://github.com/mkon/db_lock
94
107
  licenses:
@@ -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
@@ -1,16 +0,0 @@
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