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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c92e38fab7329deb1a573eafce48291e38b3198baf0ce39a4500d305a1b01081
4
- data.tar.gz: 3c14d574202f72524728cc3f1ffb5af3d2dc16b6980bfa9efe130fa84a6f5c00
3
+ metadata.gz: 6e644648b686652e6f40413bd18485cc8ecc9dc255782ad3b26793230ff0730b
4
+ data.tar.gz: de7e5592063b90ef9b3a5779b7877095fb31c0776749d5508cbec8f5a2f31291
5
5
  SHA512:
6
- metadata.gz: 67be6b9ef5ff32f10cd6f8a98d6b58af010c9d03e480617372c6e43787fbdeb704d349d573d22b1e2eb7fa4c6a05c97106f6b30a7e3ff72f6247a73cba63d781
7
- data.tar.gz: 17efa281393055a6facdfb44bbca361a49f00ae20454fe3557c89d3e971aeb0e6b7c742609409d6080345814bf8bc0d8ad1c09fb465d9dcacb2d37d5cd548013
6
+ metadata.gz: 321829a4a295725340247266fb2f869461f9d9e549cce0b36f9f736775e3a0a601f3224bbc23de7c15be89266e6156eb605d924f5d6d4ffafd156e05f700ea9d
7
+ data.tar.gz: 155e96f2a5b5aedc38da672c5ea45d4c176d72188e17ab55614d6c7fce54bc171aa1e7e9ec8035e57225a48e5bf4bc2583998124cb5a158824a631f355346cbb
@@ -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)
@@ -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
- if create
45
- result = nil
46
- begin
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
- if lock { yield self if block_given? }
60
- # lock successful, do nothing
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
@@ -1,3 +1,3 @@
1
1
  class PgLock
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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-11-05 00:00:00.000000000 Z
12
+ date: 2019-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg