pg_lock 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/check_changelog.yml +13 -0
- data/CHANGELOG.md +14 -1
- data/CODEOWNERS +2 -0
- data/README.md +2 -2
- data/lib/pg_lock/locket.rb +16 -7
- data/lib/pg_lock/version.rb +1 -1
- data/lib/pg_lock.rb +26 -15
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b5dc5c381b308116620ac170d1fe5f54d61d9cf73b0f7acbcb3d60b1cdc1222
|
4
|
+
data.tar.gz: fed6d8ba96c3afe22ff4cbe30b805f107e71452f133e8800b977e2aaf80c6756
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c58e6e372c93b10afce883726ac72f6ca77a08bbb1e4971714ea076a3f5b4b5afb808d0c75a6a9d22d62d7f9a6fa373ff9dcc36cb428f922c10078d352616ffe
|
7
|
+
data.tar.gz: 3c77cdb4e29e139a2763062d6a9957225c3fe050cd6f376deb5c60cb47211edc86a7d11b03d84d543c645defc64d441718f3209591330e40d5c247acd3acce42
|
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
name: Check Changelog
|
3
|
+
|
4
|
+
on: [pull_request]
|
5
|
+
|
6
|
+
jobs:
|
7
|
+
build:
|
8
|
+
runs-on: ubuntu-latest
|
9
|
+
steps:
|
10
|
+
- uses: actions/checkout@v1
|
11
|
+
- name: Check that CHANGELOG is touched
|
12
|
+
run: |
|
13
|
+
cat $GITHUB_EVENT_PATH | jq .pull_request.title | grep -i '\[\(\(changelog skip\)\|\(ci skip\)\)\]' || git diff remotes/origin/${{ github.base_ref }} --name-only | grep CHANGELOG.md
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
# A Log of Changes!
|
2
2
|
|
3
|
-
##
|
3
|
+
## Main - unreleased
|
4
|
+
|
5
|
+
## 1.0.0
|
6
|
+
|
7
|
+
- Fixed: Lock is now unlocked when an exception is raised in a log https://github.com/heroku/pg_lock/pull/17
|
8
|
+
- Changed: Removed mis-spelling of "aquired" https://github.com/heroku/pg_lock/pull/17
|
9
|
+
|
10
|
+
## 0.3.0
|
11
|
+
|
12
|
+
- Fix method spelling (https://github.com/heroku/pg_lock/pull/16)
|
13
|
+
|
14
|
+
## 0.2.1
|
15
|
+
|
16
|
+
- 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)
|
4
17
|
|
5
18
|
## 0.2.0
|
6
19
|
|
data/CODEOWNERS
ADDED
data/README.md
CHANGED
@@ -119,13 +119,13 @@ ensure
|
|
119
119
|
end
|
120
120
|
```
|
121
121
|
|
122
|
-
You can check on the status of a lock with the `
|
122
|
+
You can check on the status of a lock with the `acquired?` method:
|
123
123
|
|
124
124
|
```ruby
|
125
125
|
begin
|
126
126
|
lock = PgLock.new(name: "all_your_base")
|
127
127
|
lock.create
|
128
|
-
if lock.
|
128
|
+
if lock.acquired?
|
129
129
|
# do stuff
|
130
130
|
end
|
131
131
|
ensure
|
data/lib/pg_lock/locket.rb
CHANGED
@@ -11,7 +11,7 @@ class PgLock
|
|
11
11
|
|
12
12
|
def lock
|
13
13
|
@lock = connection.exec("select pg_try_advisory_lock($1,$2)", args)
|
14
|
-
return
|
14
|
+
return acquired?
|
15
15
|
end
|
16
16
|
|
17
17
|
def unlock
|
@@ -19,23 +19,32 @@ class PgLock
|
|
19
19
|
@lock = false
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
22
|
+
def acquired?
|
23
23
|
TRUE_VALUES.include?(@lock[0]["pg_try_advisory_lock"])
|
24
24
|
rescue
|
25
25
|
false
|
26
26
|
end
|
27
27
|
|
28
|
+
# Left the misspelled version of this method for backwards compatibility
|
29
|
+
def aquired?
|
30
|
+
acquired?
|
31
|
+
end
|
32
|
+
|
28
33
|
def active?
|
29
|
-
|
34
|
+
query = <<-eos
|
30
35
|
SELECT granted
|
31
36
|
FROM pg_locks
|
32
37
|
WHERE locktype = 'advisory' AND
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
38
|
+
pid = pg_backend_pid() AND
|
39
|
+
mode = 'ExclusiveLock' AND
|
40
|
+
classid = $1 AND
|
41
|
+
objid = $2
|
37
42
|
eos
|
38
43
|
|
44
|
+
result = connection.exec(query, args)
|
45
|
+
return false if result.ntuples == 0
|
46
|
+
|
47
|
+
active = result.getvalue(0,0)
|
39
48
|
TRUE_VALUES.include?(active)
|
40
49
|
end
|
41
50
|
end
|
data/lib/pg_lock/version.rb
CHANGED
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
|
@@ -86,10 +79,28 @@ class PgLock
|
|
86
79
|
end
|
87
80
|
end
|
88
81
|
|
82
|
+
# Left the misspelled version of this method for backwards compatibility
|
89
83
|
def aquired?
|
84
|
+
acquired?
|
85
|
+
end
|
86
|
+
|
87
|
+
def acquired?
|
90
88
|
locket.active?
|
91
89
|
end
|
92
|
-
|
90
|
+
|
91
|
+
alias :has_lock? :acquired?
|
92
|
+
|
93
|
+
private def internal_lock(&block)
|
94
|
+
if create
|
95
|
+
result = nil
|
96
|
+
result = Timeout::timeout(ttl, &block) if block_given?
|
97
|
+
return_result ? result : true
|
98
|
+
else
|
99
|
+
return NO_LOCK
|
100
|
+
end
|
101
|
+
ensure
|
102
|
+
delete if locket.acquired?
|
103
|
+
end
|
93
104
|
|
94
105
|
private
|
95
106
|
attr_accessor :max_attempts
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mikehale
|
8
8
|
- schneems
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-03-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|
@@ -89,10 +89,12 @@ executables: []
|
|
89
89
|
extensions: []
|
90
90
|
extra_rdoc_files: []
|
91
91
|
files:
|
92
|
+
- ".github/workflows/check_changelog.yml"
|
92
93
|
- ".gitignore"
|
93
94
|
- ".rspec"
|
94
95
|
- ".travis.yml"
|
95
96
|
- CHANGELOG.md
|
97
|
+
- CODEOWNERS
|
96
98
|
- CODE_OF_CONDUCT.md
|
97
99
|
- Gemfile
|
98
100
|
- LICENSE.txt
|
@@ -108,7 +110,7 @@ homepage: http://github.com/heroku/pg_lock
|
|
108
110
|
licenses:
|
109
111
|
- MIT
|
110
112
|
metadata: {}
|
111
|
-
post_install_message:
|
113
|
+
post_install_message:
|
112
114
|
rdoc_options: []
|
113
115
|
require_paths:
|
114
116
|
- lib
|
@@ -123,8 +125,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
125
|
- !ruby/object:Gem::Version
|
124
126
|
version: '0'
|
125
127
|
requirements: []
|
126
|
-
rubygems_version: 3.
|
127
|
-
signing_key:
|
128
|
+
rubygems_version: 3.3.26
|
129
|
+
signing_key:
|
128
130
|
specification_version: 4
|
129
131
|
summary: Use Postgres advisory lock to isolate code execution across machines
|
130
132
|
test_files: []
|