dino_utils 0.1.9 → 0.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dino_utils/version.rb +1 -1
- data/lib/dino_utils.rb +2 -0
- data/lib/tn/advisory_lock.rb +53 -0
- data/lib/tn.rb +7 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8872337516c4a023f16c68fa89a08a97cced682e
|
4
|
+
data.tar.gz: d8364252a4bdc7614c47f32d93d60d7baf9d751f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9a202ee692e138e040f0f6522a9c8f106e2b207f088fa0e543eb78481d9981f8bfea20d66c427c5f372c5f631065abb5772b60c0f8440fc24f19094fe45727d
|
7
|
+
data.tar.gz: 039ac434917e43bfab694c301b9e826e5e6528d0e60e7c575465e2da81c2944379bf8bc2fe9b0ab5e75deafe5b66b37d38a42d84012edbce70fa8efe78ed0d93
|
data/lib/dino_utils/version.rb
CHANGED
data/lib/dino_utils.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
class TN::AdvisoryLock
|
4
|
+
def initialize(key, wait: false)
|
5
|
+
key = self.class.string_to_int(key) unless key.is_a?(Integer)
|
6
|
+
@key = key
|
7
|
+
@wait = wait
|
8
|
+
end
|
9
|
+
|
10
|
+
# Convert a string to a int that's needed for the advisory lock.
|
11
|
+
def self.string_to_int(string)
|
12
|
+
Digest::SHA1.hexdigest(string)[0..10].to_i(36)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.with_lock(key, wait: false)
|
16
|
+
result = nil
|
17
|
+
new(key, wait: wait).exclusive do
|
18
|
+
result = yield
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
def exclusive
|
24
|
+
if acquired?
|
25
|
+
begin
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
release!
|
29
|
+
end
|
30
|
+
else
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
true
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def acquired?
|
39
|
+
TN.execute_sql("select #{sql_function}(#{@key})") != "f"
|
40
|
+
end
|
41
|
+
|
42
|
+
def sql_function
|
43
|
+
if @wait
|
44
|
+
"pg_advisory_lock"
|
45
|
+
else
|
46
|
+
"pg_try_advisory_lock"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def release!
|
51
|
+
TN.execute_sql("select pg_advisory_unlock(#{@key})")
|
52
|
+
end
|
53
|
+
end
|
data/lib/tn.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dino_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Van Dyk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,6 +143,8 @@ files:
|
|
143
143
|
- lib/dino_utils.rb
|
144
144
|
- lib/dino_utils/version.rb
|
145
145
|
- lib/string_monkeypatch.rb
|
146
|
+
- lib/tn.rb
|
147
|
+
- lib/tn/advisory_lock.rb
|
146
148
|
- lib/tn/http.rb
|
147
149
|
homepage: https://github.com/tanga/dino_utils
|
148
150
|
licenses:
|