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: 1) ⇒ Transaction
constructor
A new instance of Transaction.
- #start ⇒ Object
- #start_thread ⇒ Object
- #stop_thread ⇒ Object
Constructor Details
#initialize(resource_name:, client:, renew_interval: 1) ⇒ Transaction
Returns a new instance of Transaction.
3 4 5 6 7 8 |
# File 'lib/joumae/transaction.rb', line 3 def initialize(resource_name:, client:, renew_interval: 1) @resource_name = resource_name @client = client @renew_interval = renew_interval @finished = false end |
Class Method Details
.run!(resource_name:, client:, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/joumae/transaction.rb', line 43 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
34 35 36 37 |
# File 'lib/joumae/transaction.rb', line 34 def finish stop_thread @client.release(@resource_name) end |
#finished? ⇒ Boolean
39 40 41 |
# File 'lib/joumae/transaction.rb', line 39 def finished? @finished end |
#start ⇒ Object
10 11 12 13 14 |
# File 'lib/joumae/transaction.rb', line 10 def start @client.acquire(@resource_name) start_thread end |
#start_thread ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/joumae/transaction.rb', line 16 def start_thread fail "Thread already started" if @thread @thread = Thread.start { loop do sleep @renew_interval @thread.exit if finished? @client.renew(@resource_name) end } end |
#stop_thread ⇒ Object
28 29 30 31 32 |
# File 'lib/joumae/transaction.rb', line 28 def stop_thread @finished = true @thread.join end |