Class: Joumae::Transaction
- Inherits:
-
Object
- Object
- Joumae::Transaction
- Defined in:
- lib/joumae/transaction.rb
Class Method Summary collapse
Instance Method Summary collapse
- #finish ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(resource_name:, client:, renew_interval: 20, ttl: 30) ⇒ Transaction
constructor
A new instance of Transaction.
- #start ⇒ Object
- #start_thread ⇒ Object
- #stop_thread ⇒ Object
Constructor Details
#initialize(resource_name:, client:, renew_interval: 20, ttl: 30) ⇒ Transaction
Returns a new instance of Transaction.
3 4 5 6 7 8 9 |
# File 'lib/joumae/transaction.rb', line 3 def initialize(resource_name:, client:, renew_interval: 20, ttl: 30) @resource_name = resource_name @client = client @renew_interval = renew_interval @ttl = ttl @finished = false end |
Class Method Details
.run!(resource_name:, client:, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/joumae/transaction.rb', line 47 def self.run!(resource_name:, client:, &block) t = new(resource_name: resource_name, client: client) t.start begin block.call ensure t.finish end end |
Instance Method Details
#finish ⇒ Object
38 39 40 41 |
# File 'lib/joumae/transaction.rb', line 38 def finish stop_thread @client.release(@resource_name) end |
#finished? ⇒ Boolean
43 44 45 |
# File 'lib/joumae/transaction.rb', line 43 def finished? @finished end |
#start ⇒ Object
11 12 13 14 15 |
# File 'lib/joumae/transaction.rb', line 11 def start @client.acquire(@resource_name, ttl: @ttl) start_thread end |
#start_thread ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/joumae/transaction.rb', line 17 def start_thread fail "Thread already started" if @thread @thread = Thread.start { loop do wait_started_time = Time.now while Time.now - wait_started_time < @renew_interval sleep 0.1 @thread.exit if finished? end @client.renew(@resource_name, ttl: @ttl) end } end |
#stop_thread ⇒ Object
32 33 34 35 36 |
# File 'lib/joumae/transaction.rb', line 32 def stop_thread @finished = true @thread.join end |