Class: Utils::ProbeServer

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

Overview

A probe server for managing and executing process jobs through Unix domain sockets.

This class provides a mechanism for enqueueing and running process jobs in a distributed manner, using Unix domain sockets for communication. It maintains a queue of jobs, tracks their execution status, and provides an interactive interface for managing the server.

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



248
249
250
251
252
253
# File 'lib/utils/probe_server.rb', line 248

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.



478
479
480
# File 'lib/utils/probe_server.rb', line 478

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:



469
470
471
472
# File 'lib/utils/probe_server.rb', line 469

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

#helpObject

The help method displays a formatted list of available commands and their descriptions.

This method organizes and presents the documented commands along with their shortcuts and descriptions in a formatted table layout for easy reference.



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/utils/probe_server.rb', line 311

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_clearTrueClass

The history_clear method clears all entries from the server’s execution history.

This method resets the internal history array to an empty state, effectively removing all records of previously executed jobs from the probe server.

Returns:

  • (TrueClass)

    always returns true after clearing the history



394
395
396
397
# File 'lib/utils/probe_server.rb', line 394

def history_clear
  @history = []
  true
end

#history_listObject

The history_list method displays the list of previously executed jobs from the server’s history.

This method outputs all completed jobs that have been processed by the probe server, showing their identifiers and command arguments for review.



381
382
383
# File 'lib/utils/probe_server.rb', line 381

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



295
296
297
# File 'lib/utils/probe_server.rb', line 295

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

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

The job_enqueue method adds a new process job to the execution queue.

This method creates a process job instance with the provided arguments and enqueues it for execution by the probe server. It provides feedback about the enqueued job through output messaging.

Parameters:

  • args (Array)

    the command arguments to be executed by the process job



332
333
334
335
336
# File 'lib/utils/probe_server.rb', line 332

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) ⇒ TrueClass, FalseClass

The job_repeat method re-executes a previously run job from the history.

This method takes a job identifier and attempts to find the corresponding job in the server’s execution history. If found, it enqueues a new instance of that job for execution with the same arguments as the original.

Parameters:

  • job_id (Integer, Utils::ProcessJob) (defaults to: @history.last)

    the identifier of the job to repeat or the job object itself

Returns:

  • (TrueClass, FalseClass)

    true if the job was found and re-enqueued, false otherwise



364
365
366
367
368
369
370
371
372
# File 'lib/utils/probe_server.rb', line 364

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



492
493
494
# File 'lib/utils/probe_server.rb', line 492

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:



509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/utils/probe_server.rb', line 509

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

The shutdown method terminates the probe server process immediately.

This method outputs a warning message indicating that the server is being shut down forcefully and then exits the program with status code 23.



345
346
347
348
# File 'lib/utils/probe_server.rb', line 345

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.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/utils/probe_server.rb', line 260

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