lib/lusnoc/session.rb in lusnoc-0.0.2 vs lib/lusnoc/session.rb in lusnoc-0.1.0

- old
+ new

@@ -3,11 +3,11 @@ module Lusnoc class Session include Helper - attr_reader :id, :name, :ttl, :live, :expired_at + attr_reader :id, :name, :ttl, :alive, :expired_at def initialize(name, ttl: 20) @name = name @ttl = ttl @@ -16,31 +16,31 @@ ensure destroy_session(@id) end def expired? - !live? + !alive? end def time_to_expiration @expired_at && @expired_at - Time.now end def need_renew? time_to_expiration && time_to_expiration < (@ttl / 2.0) end - def live? - @live + def alive? + @alive end - def live!(exception_class = ExpiredError) - live? || (raise exception_class.new("Session #{id} expired")) + def alive!(exception_class = ExpiredError) + alive? || (raise exception_class.new("Session #{id} expired")) end def renew - live! + 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}" end @@ -55,31 +55,31 @@ { 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}" - @live = true + @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}]" - @live = false + @alive = false @expired_at = nil 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" - @live = false + @alive = false @expired_at = nil @session_die_cb&.call(self) else logger.unknown 'Something is wrong with thread logic' end