pg_lock 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/pg_lock.rb +21 -14
- data/lib/pg_lock/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e644648b686652e6f40413bd18485cc8ecc9dc255782ad3b26793230ff0730b
|
4
|
+
data.tar.gz: de7e5592063b90ef9b3a5779b7877095fb31c0776749d5508cbec8f5a2f31291
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 321829a4a295725340247266fb2f869461f9d9e549cce0b36f9f736775e3a0a601f3224bbc23de7c15be89266e6156eb605d924f5d6d4ffafd156e05f700ea9d
|
7
|
+
data.tar.gz: 155e96f2a5b5aedc38da672c5ea45d4c176d72188e17ab55614d6c7fce54bc171aa1e7e9ec8035e57225a48e5bf4bc2583998124cb5a158824a631f355346cbb
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
## Master - unreleased
|
4
4
|
|
5
|
+
## 0.2.1
|
6
|
+
|
7
|
+
- Fix regression where a block that returned a `false` would cause the `lock!` method to incorrectly raise an error (https://github.com/heroku/pg_lock/pull/15)
|
8
|
+
|
5
9
|
## 0.2.0
|
6
10
|
|
7
11
|
- Return the result evaluated inside the block (https://github.com/heroku/pg_lock/pull/10)
|
data/lib/pg_lock.rb
CHANGED
@@ -26,6 +26,7 @@ class PgLock
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
UnableToLock = UnableToLockError
|
29
|
+
NO_LOCK = Object.new
|
29
30
|
|
30
31
|
def initialize(name:, attempts: 3, attempt_interval: 1, ttl: 60, connection: DEFAULT_CONNECTION_CONNECTOR.call, log: DEFAULT_LOGGER.call, return_result: true)
|
31
32
|
self.name = name
|
@@ -41,26 +42,18 @@ class PgLock
|
|
41
42
|
|
42
43
|
# Runs the given block if an advisory lock is able to be acquired.
|
43
44
|
def lock(&block)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
result = Timeout::timeout(ttl, &block) if block_given?
|
48
|
-
ensure
|
49
|
-
delete
|
50
|
-
end
|
51
|
-
return_result ? result : true
|
52
|
-
else
|
53
|
-
return false
|
54
|
-
end
|
45
|
+
result = internal_lock(&block)
|
46
|
+
return false if result == NO_LOCK
|
47
|
+
result
|
55
48
|
end
|
56
49
|
|
57
50
|
# A PgLock::UnableToLock is raised if the lock is not acquired.
|
58
51
|
def lock!(exception_klass = PgLock::UnableToLockError)
|
59
|
-
|
60
|
-
|
61
|
-
else
|
52
|
+
result = internal_lock { yield self if block_given? }
|
53
|
+
if result == NO_LOCK
|
62
54
|
raise exception_klass.new(name: name, attempts: max_attempts)
|
63
55
|
end
|
56
|
+
return result
|
64
57
|
end
|
65
58
|
|
66
59
|
def create
|
@@ -91,6 +84,20 @@ class PgLock
|
|
91
84
|
end
|
92
85
|
alias :has_lock? :aquired?
|
93
86
|
|
87
|
+
private def internal_lock(&block)
|
88
|
+
if create
|
89
|
+
result = nil
|
90
|
+
begin
|
91
|
+
result = Timeout::timeout(ttl, &block) if block_given?
|
92
|
+
ensure
|
93
|
+
delete
|
94
|
+
end
|
95
|
+
return_result ? result : true
|
96
|
+
else
|
97
|
+
return NO_LOCK
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
94
101
|
private
|
95
102
|
attr_accessor :max_attempts
|
96
103
|
attr_accessor :attempt_interval
|
data/lib/pg_lock/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mikehale
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|