dino_utils 0.1.9 → 0.1.10

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
  SHA1:
3
- metadata.gz: 44177233d63e38c158e657fa371f0d76580a8b8d
4
- data.tar.gz: c70e9cb4b081e3f62da40b34d28efb0828165d2a
3
+ metadata.gz: 8872337516c4a023f16c68fa89a08a97cced682e
4
+ data.tar.gz: d8364252a4bdc7614c47f32d93d60d7baf9d751f
5
5
  SHA512:
6
- metadata.gz: 5a23e557aab8b06e68d59175c3b9852b15a85e8d9eb7ed761fc182c07c40d7f9f9cb3222b06e74f8511007051d7bafd139b625a6b51c18425bb78bb3a64558e4
7
- data.tar.gz: 264c63e5d3efae192c95c31f2871b1f78284c33f337f8e0ac8d7f9caa386c580816fc8973e619a18e311266bc3ba5a00f07a30230cbe05e9138868eec245978f
6
+ metadata.gz: f9a202ee692e138e040f0f6522a9c8f106e2b207f088fa0e543eb78481d9981f8bfea20d66c427c5f372c5f631065abb5772b60c0f8440fc24f19094fe45727d
7
+ data.tar.gz: 039ac434917e43bfab694c301b9e826e5e6528d0e60e7c575465e2da81c2944379bf8bc2fe9b0ab5e75deafe5b66b37d38a42d84012edbce70fa8efe78ed0d93
@@ -1,3 +1,3 @@
1
1
  module DinoUtils
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
data/lib/dino_utils.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require "dino_utils/version"
2
2
  require_relative 'dino/upsert'
3
3
  require_relative 'string_monkeypatch'
4
+ require_relative 'tn'
4
5
  require_relative 'tn/http'
6
+ require_relative 'tn/advisory_lock'
@@ -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
@@ -0,0 +1,7 @@
1
+ module TN
2
+
3
+ def self.execute_sql sql, *placeholders
4
+ ActiveRecord::Base.connection.select_value(ActiveRecord::Base.send(:sanitize_sql_array, [sql, *placeholders]))
5
+ end
6
+
7
+ end
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.9
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-01-27 00:00:00.000000000 Z
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: