lib/lusnoc/session.rb in lusnoc-0.1.0 vs lib/lusnoc/session.rb in lusnoc-0.1.2.16548
- old
+ new
@@ -1,6 +1,7 @@
require 'lusnoc/watcher'
+require 'lusnoc/guard'
module Lusnoc
class Session
include Helper
@@ -10,13 +11,16 @@
def initialize(name, ttl: 20)
@name = name
@ttl = ttl
@id = create_session(name, ttl)
- yield(self)
+
+ prepare_guard(@id).run do
+ yield(self)
+ end
ensure
- destroy_session(@id)
+ destroy_session(@id) if @id
end
def expired?
!alive?
end
@@ -32,18 +36,18 @@
def alive?
@alive
end
def alive!(exception_class = ExpiredError)
- alive? || (raise exception_class.new("Session #{id} expired"))
+ alive? || (raise exception_class.new("Session[#{@name}:#{@id}] expired"))
end
def renew
alive!
Lusnoc.http_put(build_url("/v1/session/renew/#{@id}"), nil, timeout: 1)
@expired_at = Time.now + ttl
- logger.info "Session renewed: #{name}[#{@id}]. Next expiration: #{@expired_at}"
+ logger.info "Session[#{@name}:#{@id}] renewed. Next expiration: #{@expired_at}"
end
def on_session_die(&block)
@session_die_cb = block
end
@@ -54,52 +58,35 @@
resp = Lusnoc.http_put(build_url('/v1/session/create'),
{ Name: name, TTL: "#{ttl}s", LockDelay: '5s' },
{ timeout: 1 })
session_id = JSON.parse(resp.body)['ID']
@expired_at = Time.now + ttl
- logger.info "Session created: #{name}[#{session_id}]. TTL:#{ttl}s. Next expiration: #{@expired_at}"
+ logger.info "Session[#{name}:#{session_id}] created. TTL:#{ttl}s. Next expiration: #{@expired_at}"
@alive = true
- @th = start_watch_thread(session_id)
session_id
end
- def destroy_session(session_id)
- @th.kill rescue nil
- Lusnoc.http_put(build_url("/v1/session/destroy/#{session_id}"),
- nil,
- timeout: 1) rescue nil
- logger.info "Session destroyed: #{name}[#{session_id}]"
- @alive = false
- @expired_at = nil
- end
+ def prepare_guard(session_id)
+ Lusnoc::Guard.new(build_url("/v1/session/info/#{session_id}")) do |guard|
+ guard.condition do |body|
+ !JSON.parse(body).empty? rescue false
+ end
- def start_watch_thread(session_id)
- Thread.new do
- logger.debug "Guard thread for Session #{name}[#{session_id}] started"
-
- if wait_forever_for_session_gone(session_id)
- logger.error "Session #{name}[#{session_id}] is gone"
+ guard.then do
@alive = false
@expired_at = nil
+ logger.info "Session[#{@name}:#{session_id}] is gone"
@session_die_cb&.call(self)
- else
- logger.unknown 'Something is wrong with thread logic'
end
- ensure
- logger.debug "Guard thread for Session #{name}[#{session_id}] finihsed"
end
end
- def wait_forever_for_session_gone(session_id)
- Lusnoc::Watcher.new(build_url("/v1/session/info/#{session_id}"), timeout: 0).run do |body|
- true if JSON.parse(body).empty?
- end
- rescue StandardError => e
- logger.error "Session #{name}[#{session_id}] watch exception: #{e.inspect}"
- logger.error e.backtrace.join("\n")
- true
+ def destroy_session(session_id)
+ @alive = false
+ @expired_at = nil
+ Lusnoc.http_put(build_url("/v1/session/destroy/#{session_id}"), nil, timeout: 1) rescue nil
+ logger.info "Session[#{@name}:#{session_id}] destroyed"
end
-
end
end