Class: WebConsole::ConsoleSession

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/web_console/console_session.rb

Overview

Manage and persist (in memory) WebConsole::Slave instances.

Defined Under Namespace

Classes: Error

Constant Summary collapse

INMEMORY_STORAGE =

In-memory storage for the console sessions. Session preservation is troubled on servers with multiple workers and threads.

{}
Unavailable =

Raised when trying to find a session that is no longer in the in-memory session storage or when the slave process exited.

Class.new(Error)
Invalid =

Raised when an operation transition to an invalid state.

Class.new(Error)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModel::Model

included

Constructor Details

#initialize(attributes = {}) ⇒ ConsoleSession

Returns a new instance of ConsoleSession.



44
45
46
# File 'app/models/web_console/console_session.rb', line 44

def initialize(attributes = {})
  @slave = WebConsole::Slave.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



84
85
86
87
88
89
90
# File 'app/models/web_console/console_session.rb', line 84

def method_missing(name, *args, &block)
  if @slave.respond_to?(name)
    delegate_and_call_slave_method(name, *args, &block)
  else
    super
  end
end

Class Method Details

.createObject

Creates an already persisted consolse session.

Use this method if you need to persist a session, without providing it any input.



39
40
41
# File 'app/models/web_console/console_session.rb', line 39

def create
  new.persist
end

.find(pid) ⇒ Object

Finds a session by its pid.

Raises WebConsole::ConsoleSession::Expired if there is no such session.



31
32
33
# File 'app/models/web_console/console_session.rb', line 31

def find(pid)
  INMEMORY_STORAGE[pid.to_i] or raise Unavailable, 'Session unavailable'
end

Instance Method Details

#persistObject

Explicitly persist the model in the in-memory storage.



49
50
51
# File 'app/models/web_console/console_session.rb', line 49

def persist
  INMEMORY_STORAGE[pid] = self
end

#persisted?Boolean

Returns true if the current session is persisted in the in-memory storage.

Returns:

  • (Boolean)


54
55
56
# File 'app/models/web_console/console_session.rb', line 54

def persisted?
  self == INMEMORY_STORAGE[pid]
end

#to_keyObject

Returns an Enumerable of all key attributes if any is set, regardless if the object is persisted or not.



60
61
62
# File 'app/models/web_console/console_session.rb', line 60

def to_key
  [pid] if persisted?
end