lib/lusnoc/session.rb in lusnoc-0.1.2.16550 vs lib/lusnoc/session.rb in lusnoc-0.1.2.16562

- old
+ new

@@ -6,14 +6,18 @@ include Helper attr_reader :id, :name, :ttl, :alive, :expired_at - def initialize(name, ttl: 20) + def initialize(name, ttl: 20, &block) @name = name @ttl = ttl + run(&block) if block_given? + end + + def run @id = create_session(name, ttl) prepare_guard(@id).run do yield(self) end @@ -36,11 +40,11 @@ def alive? @alive end def alive!(exception_class = ExpiredError) - alive? || (raise exception_class.new("Session[#{@name}:#{@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) @@ -48,9 +52,11 @@ logger.info "Session[#{@name}:#{@id}] renewed. Next expiration: #{@expired_at}" end def on_session_die(&block) @session_die_cb = block + @session_die_cb&.call(self) if @alive == false + self end private def create_session(name, ttl)