Class: Utils::ProbeServer

Inherits:
Object show all
Includes:
Term::ANSIColor
Defined in:
lib/utils/probe_server.rb

Defined Under Namespace

Classes: LogWrapper

Instance Method Summary collapse

Constructor Details

#initializeUtils::ProbeServer

The initialize method sets up a new probe server instance.

This method creates and configures the core components of the probe server, including initializing the Unix domain socket server for communication, setting up the job queue for processing tasks, and preparing the history tracking for completed jobs.

with the specified socket name and runtime directory



223
224
225
226
227
228
# File 'lib/utils/probe_server.rb', line 223

def initialize
  @server         = UnixSocks::Server.new(socket_name: 'probe.sock', runtime_dir: Dir.pwd)
  @history        = [].freeze
  @jobs_queue     = Queue.new
  @current_job_id = 0
end

Instance Method Details

#clearObject

The clear method clears the terminal screen by executing the clear command.



446
447
448
# File 'lib/utils/probe_server.rb', line 446

def clear
  system "clear"
end

#envUtils::ProbeServer::LogWrapper

The env method provides access to the server’s environment variables through a wrapped interface.

This method returns a LogWrapper instance that allows for setting and retrieving environment variables while logging the operations. The wrapper maintains access to both the probe server for messaging and the underlying ENV object for attribute access.

for variable management

Returns:



437
438
439
440
# File 'lib/utils/probe_server.rb', line 437

memoize method:
def env
  LogWrapper.new(self, ENV)
end

#helpObject



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/utils/probe_server.rb', line 286

def help
  docs      = doc_annotations.sort_by(&:first)
  docs_size = docs.map { |a| a.first.size }.max
  format = "%-#{docs_size}s %-3s %s"
  output_message [
    on_color(20) { white { format % %w[ command sho description ] } }
  ] << docs.map { |cmd, doc|
    shortcut = shortcut_of(cmd) and shortcut = "(#{shortcut})"
    format % [ cmd, shortcut, doc ]
  }
end

#history_clearObject



369
370
371
372
# File 'lib/utils/probe_server.rb', line 369

def history_clear
  @history = []
  true
end

#history_listObject



356
357
358
# File 'lib/utils/probe_server.rb', line 356

def history_list
  output_message @history
end

#inspectString Also known as: to_s

The inspect method returns a string representation of the probe server instance.

This method provides a concise overview of the probe server’s state by displaying its type and the current size of the job queue, making it useful for debugging and monitoring purposes.

Returns:

  • (String)

    a formatted string containing the probe server identifier and the number of jobs currently in the queue



270
271
272
# File 'lib/utils/probe_server.rb', line 270

def inspect
  "#<Probe #queue=#{@jobs_queue.size}>"
end

#job_enqueue(args) ⇒ Object Also known as: enqueue



307
308
309
310
311
# File 'lib/utils/probe_server.rb', line 307

def job_enqueue(args)
  job = ProcessJob.new(args:, probe_server: self)
  output_message "#{job.inspect} enqueued.", type: :info
  @jobs_queue.push job
end

#job_repeat(job_id = @history.last) ⇒ Object



339
340
341
342
343
344
345
346
347
# File 'lib/utils/probe_server.rb', line 339

def job_repeat(job_id = @history.last)
  ProcessJob === job_id and job_id = job_id.id
  if old_job = @history.find { |job| job.id == job_id }
    job_enqueue old_job.args
    true
  else
    false
  end
end

#next_job_idInteger

The next_job_id method increments and returns the current job identifier.

This method maintains a sequential counter for job identification within the probe server, providing unique IDs for newly enqueued process jobs.

Returns:

  • (Integer)

    the next available job identifier in the sequence



460
461
462
# File 'lib/utils/probe_server.rb', line 460

def next_job_id
  @current_job_id += 1
end

#output_message(msg, type: nil) ⇒ Utils::ProbeServer

The output_message method displays a formatted message to standard output with optional styling based on the message type.

This method takes a message and an optional type parameter to determine the formatting style for the output. It handles both string and array messages, converting arrays into multi-line strings. Different message types are styled using color codes and formatting attributes to provide visual distinction.

Parameters:

  • msg (String, Array)

    the message to be displayed

  • type (Symbol) (defaults to: nil)

    the type of message for styling (success, info, warn, failure)

Returns:



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/utils/probe_server.rb', line 477

def output_message(msg, type: nil)
  msg.respond_to?(:to_a) and msg = msg.to_a * "\n"
  msg =
    case type
    when :success
      on_color(22) { white { msg } }
    when :info
      on_color(20) { white { msg } }
    when :warn
      on_color(94) { white { msg } }
    when :failure
      on_color(124) { blink { white { msg } } }
    else
      msg
    end
  STDOUT.puts msg
  STDOUT.flush
  self
end

#shutdownObject



320
321
322
323
# File 'lib/utils/probe_server.rb', line 320

def shutdown
  output_message "Server was shutdown down manually!", type: :info
  exit 23
end

#startObject

The start method initializes and begins operation of the probe server.

This method sets up the probe server by starting a thread to process jobs from the queue and entering a receive loop to handle incoming requests. It also manages interrupt signals to enter interactive mode when needed.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/utils/probe_server.rb', line 235

def start
  output_message "Starting probe server listening to #{@server.server_socket_path}.", type: :info
  Thread.new do
    loop do
      job = @jobs_queue.pop
      run_job job
    end
  end
  begin
    receive_loop.join
  rescue Interrupt
    ARGV.clear << '-f'
    output_message %{\nEntering interactive mode.}, type: :info
    help
    begin
      old, $VERBOSE = $VERBOSE, nil
      examine(self)
    ensure
      $VERBOSE = old
    end
    @server.remove_socket_path
    output_message "Quitting interactive mode, but still listening to #{@server.server_socket_path}.", type: :info
    retry
  end
end