istox 0.1.43.6 → 0.1.43.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/istox.rb +2 -0
- data/lib/istox/consumers/blockchain_hash_handler.rb +10 -6
- data/lib/istox/migrations/create_locks.rb +18 -0
- data/lib/istox/models/lock.rb +51 -0
- data/lib/istox/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7714a5d3bc4fdca3f2127988029cf9433e74e2cdb72c69603a0e9e98f61da712
|
4
|
+
data.tar.gz: 3862b77faeb51a1a2b565ec2d7bb8daea6833c18ae0bed91df1e9b2b0af02a95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64f3cc5b014f6689b7440796ec828136c93e319596d0d47a273aa227ab964d327a95de8ba953f6ca83cd0d6f5f73ec1702225676aefa3428917a1aca91afa5bb
|
7
|
+
data.tar.gz: b924e4cd0728b6ce65c1aa7a234a77c06ce8542386c91157b6934794109684cde458688d657c2ffd3c2eb6f2c5784db0d9f52965753bfaf5d359361854be8f58
|
data/Gemfile.lock
CHANGED
data/lib/istox.rb
CHANGED
@@ -19,7 +19,9 @@ module Istox
|
|
19
19
|
require "istox/helpers/blockchain_receipt_service"
|
20
20
|
require "istox/models/blockchain_receipt"
|
21
21
|
require "istox/models/concerns/blockchain_receipt_query"
|
22
|
+
require "istox/models/lock"
|
22
23
|
require "istox/consumers/blockchain_status_handler"
|
23
24
|
require "istox/consumers/blockchain_hash_handler"
|
24
25
|
require "istox/migrations/create_blockchain_receipts"
|
26
|
+
require "istox/migrations/create_locks"
|
25
27
|
end
|
@@ -2,14 +2,18 @@ module Istox
|
|
2
2
|
class BlockchainHashHandler
|
3
3
|
class << self
|
4
4
|
def hash_generated(data)
|
5
|
-
|
5
|
+
puts "Locking #{data.uuid}"
|
6
|
+
::Istox::Lock.acquire(data.uuid) do
|
7
|
+
receipt = ::Istox::BlockchainReceipt.where(txid: data.uuid, txhash: nil).first
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
9
|
+
if receipt.nil?
|
10
|
+
puts 'Transaction not found, skipping...'
|
11
|
+
return
|
12
|
+
end
|
13
|
+
|
14
|
+
receipt.update!(txhash: data.txnHash)
|
10
15
|
end
|
11
|
-
|
12
|
-
receipt.update!(txhash: data.txnHash)
|
16
|
+
puts "Releasing lock for #{data.uuid}"
|
13
17
|
end
|
14
18
|
|
15
19
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Istox
|
2
|
+
class CreateLocks < ActiveRecord::Migration[5.0]
|
3
|
+
|
4
|
+
def self.up
|
5
|
+
create_table :locks do |t|
|
6
|
+
t.string :name, :limit => 40
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
add_index :locks, :name, :unique => true
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
remove_index :locks, :column => :name
|
14
|
+
drop_table :locks
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Istox
|
2
|
+
class Lock < ActiveRecord::Base
|
3
|
+
|
4
|
+
def self.acquire(name)
|
5
|
+
already_acquired = definitely_acquired?(name)
|
6
|
+
|
7
|
+
if already_acquired
|
8
|
+
yield
|
9
|
+
else
|
10
|
+
begin
|
11
|
+
create(:name => name) unless find_by_name(name)
|
12
|
+
rescue ActiveRecord::StatementInvalid
|
13
|
+
# concurrent create is okay
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
result = nil
|
18
|
+
|
19
|
+
transaction do
|
20
|
+
find_by_name(name, :lock => true) # this is the call that will block
|
21
|
+
acquired_lock(name)
|
22
|
+
result = yield
|
23
|
+
end
|
24
|
+
|
25
|
+
result
|
26
|
+
ensure
|
27
|
+
maybe_released_lock(name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# if true, the lock is acquired
|
33
|
+
# if false, the lock might still be acquired, because we were in another db transaction
|
34
|
+
def self.definitely_acquired?(name)
|
35
|
+
!!Thread.current[:definitely_acquired_locks] and Thread.current[:definitely_acquired_locks].has_key?(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.acquired_lock(name)
|
39
|
+
Thread.current[:definitely_acquired_locks] ||= {}
|
40
|
+
Thread.current[:definitely_acquired_locks][name] = true
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.maybe_released_lock(name)
|
44
|
+
Thread.current[:definitely_acquired_locks] ||= {}
|
45
|
+
Thread.current[:definitely_acquired_locks].delete(name)
|
46
|
+
end
|
47
|
+
|
48
|
+
private_class_method :acquired_lock, :maybe_released_lock
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
data/lib/istox/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: istox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.43.
|
4
|
+
version: 0.1.43.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Siong Leng
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-04-
|
11
|
+
date: 2019-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -234,8 +234,10 @@ files:
|
|
234
234
|
- lib/istox/helpers/publisher.rb
|
235
235
|
- lib/istox/interfaces/chainhub/transaction.rb
|
236
236
|
- lib/istox/migrations/create_blockchain_receipts.rb
|
237
|
+
- lib/istox/migrations/create_locks.rb
|
237
238
|
- lib/istox/models/blockchain_receipt.rb
|
238
239
|
- lib/istox/models/concerns/blockchain_receipt_query.rb
|
240
|
+
- lib/istox/models/lock.rb
|
239
241
|
- lib/istox/version.rb
|
240
242
|
homepage: http://www.abc.com
|
241
243
|
licenses: []
|