Class: RiderServer::Session

Inherits:
Object show all
Includes:
Logger
Defined in:
lib/rider_server/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

configure_logger, log, #log

Constructor Details

#initialize(config, response_queue, history: []) ⇒ Session

Returns a new instance of Session.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rider_server/session.rb', line 50

def initialize(config, response_queue, history: [])
  @id = SecureRandom.uuid
  @config = config
  @history = history
  @workspace = Workspace.new
  @queue = Thread::Queue.new
  @exceptions = Utils::FixedArray.new(max_size: config.exception_history_size)
  @response_queue = response_queue
  @exception_queue = Thread::Queue.new
  @evaluations = {}
  @services = {}

  # XXX Side effects in initializer, :()
  start_exception_processing
end

Instance Attribute Details

#evaluationsObject (readonly)

Returns the value of attribute evaluations.



46
47
48
# File 'lib/rider_server/session.rb', line 46

def evaluations
  @evaluations
end

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



45
46
47
# File 'lib/rider_server/session.rb', line 45

def exceptions
  @exceptions
end

#historyObject (readonly)

Returns the value of attribute history.



48
49
50
# File 'lib/rider_server/session.rb', line 48

def history
  @history
end

#idObject (readonly)

Returns the value of attribute id.



43
44
45
# File 'lib/rider_server/session.rb', line 43

def id
  @id
end

#response_queueObject (readonly)

Returns the value of attribute response_queue.



47
48
49
# File 'lib/rider_server/session.rb', line 47

def response_queue
  @response_queue
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



44
45
46
# File 'lib/rider_server/session.rb', line 44

def workspace
  @workspace
end

Instance Method Details

#add_evaluation(evaluation_id, eval_thread) ⇒ Object

Historical Evaluations



162
163
164
# File 'lib/rider_server/session.rb', line 162

def add_evaluation(evaluation_id, eval_thread)
  @evaluations[evaluation_id] = eval_thread
end

#add_exception(operation_id, exception, metadata = {}) ⇒ Object

Add an exception to the exception history. operation_id is the id of the request. exception is the exception object. metadata is a Hash containing additional information about the exception.



119
120
121
122
123
# File 'lib/rider_server/session.rb', line 119

def add_exception(operation_id, exception,  = {})
  ex = wrap_exception(operation_id, exception, )
  @exception_queue.push(ex)
  ex
end

#add_result(evaluation_id, value) ⇒ Object

Add an execution result to the history. The evaluation_id is the operation id of the evaluation and the value is the result of the evaluation.



101
102
103
# File 'lib/rider_server/session.rb', line 101

def add_result(evaluation_id, value)
  result_history[evaluation_id] = value
end

#add_service(service_class) ⇒ Object

Service control



184
185
186
187
# File 'lib/rider_server/session.rb', line 184

def add_service(service_class)
  cls = service_class.new(self)
  @services[service_class.service_name] = cls
end

#add_wrapped_exception(wrapped_exception) ⇒ Object

Add an exception that has already been wrapped in metadata. wrapped_exception should be a hash, that has the keys “id”, “created_at”, “exception”, and “metadata”



128
129
130
131
# File 'lib/rider_server/session.rb', line 128

def add_wrapped_exception(wrapped_exception)
  @exception_queue.push(wrapped_exception)
  exception
end

#cloneObject



76
77
78
# File 'lib/rider_server/session.rb', line 76

def clone
  RiderServer.create_session(@responses_queue, history: @history.clone)
end

#get_exception(exception_id) ⇒ Object

Return an exception by exception_id. Raises an exception if the exception doesn’t exist.



135
136
137
138
139
# File 'lib/rider_server/session.rb', line 135

def get_exception(exception_id)
  exception = @exceptions.find { |item| item["id"] == exception_id }
  raise "Missing exception #{exception_id}." unless exception
  exception
end

#get_result(evaluation_id) ⇒ Object

Return a historical result of an evaluation by evaluation_id.



106
107
108
109
# File 'lib/rider_server/session.rb', line 106

def get_result(evaluation_id)
  raise "Missing history item #{evaluation_id}." unless result_history.key?(evaluation_id)
  result_history[evaluation_id]
end

#interrupt_evaluation(evaluation_id) ⇒ Object



174
175
176
177
178
# File 'lib/rider_server/session.rb', line 174

def interrupt_evaluation(evaluation_id)
  log.info "Interrupting eval thread #{evaluation_id}"
  # Signal the eval thread to raise an exception
  @evaluations[evaluation_id].raise EvalInterrupt.new
end

#list_servicesObject



189
190
191
192
193
194
195
196
# File 'lib/rider_server/session.rb', line 189

def list_services
  @services.map do |name, _|
    {
      "name" => name,
      "state" => service_state(name).to_s
    }
  end
end

#push_history(event) ⇒ Object

Push an event onto the history stack.



85
86
87
# File 'lib/rider_server/session.rb', line 85

def push_history(event)
  @history.push(event)
end

#remove_evaluation(evaluation_id) ⇒ Object



166
167
168
# File 'lib/rider_server/session.rb', line 166

def remove_evaluation(evaluation_id)
  @evaluations.delete(evaluation_id)
end

#result_historyObject

Return a Hash containing all historical evaluations.



94
95
96
# File 'lib/rider_server/session.rb', line 94

def result_history
  @result_history ||= {}
end

#running_evaluation?(evaluation_id) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/rider_server/session.rb', line 170

def running_evaluation?(evaluation_id)
  @evaluations[evaluation_id]
end

#send_response(response) ⇒ Object



66
67
68
69
70
# File 'lib/rider_server/session.rb', line 66

def send_response(response)
  if response
    @response_queue.push(response)
  end
end

#service_state(service_type) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/rider_server/session.rb', line 212

def service_state(service_type)
  if @services.key?(service_type)
    @services[service_type].status
  else
    :stopped
  end
end

#start_exception_processingObject

Start a thread to process exceptions from the exception queue.



142
143
144
145
146
147
148
149
150
# File 'lib/rider_server/session.rb', line 142

def start_exception_processing
  return if @exception_processing_thread

  @exception_processing_thread = Thread.new do
    loop do
      @exceptions << @exception_queue.pop
    end
  end
end

#start_service(service_type, stream_id) ⇒ Object



198
199
200
201
202
203
# File 'lib/rider_server/session.rb', line 198

def start_service(service_type, stream_id)
  raise "Service #{service_type} is already running." \
    if @services[service_type].status == :running

  @services[service_type].start(stream_id)
end

#stdinObject



72
73
74
# File 'lib/rider_server/session.rb', line 72

def stdin
  ::STDIN  # rubocop:disable Style/GlobalStdStream
end

#stop_exception_processingObject

Stop the exception processing thread.



153
154
155
156
# File 'lib/rider_server/session.rb', line 153

def stop_exception_processing
  @exception_processing_thread.exit
  @exception_processing_thread = nil
end

#stop_service(service_type) ⇒ Object



205
206
207
208
209
210
# File 'lib/rider_server/session.rb', line 205

def stop_service(service_type)
  raise "Service #{service_type} is not running." \
    if @services[service_type].status == :stopped

  @services[service_type].stop
end