Class: Utils::Probe::ProbeServer

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor, ServerHandling
Defined in:
lib/utils/probe/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

Methods included from ServerHandling

#create_server

Constructor Details

#initialize(server_type: :unix, port: 6666) ⇒ Utils::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



22
23
24
25
26
27
# File 'lib/utils/probe/probe_server.rb', line 22

def initialize(server_type: :unix, port: 6666)
  @server         = create_server(server_type, port)
  @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.



252
253
254
# File 'lib/utils/probe/probe_server.rb', line 252

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:

  • (Utils::ProbeServer::LogWrapper)

    a wrapped environment object



243
244
245
246
# File 'lib/utils/probe/probe_server.rb', line 243

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.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/utils/probe/probe_server.rb', line 85

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



168
169
170
171
# File 'lib/utils/probe/probe_server.rb', line 168

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.



155
156
157
# File 'lib/utils/probe/probe_server.rb', line 155

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



69
70
71
# File 'lib/utils/probe/probe_server.rb', line 69

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



106
107
108
109
110
# File 'lib/utils/probe/probe_server.rb', line 106

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



138
139
140
141
142
143
144
145
146
# File 'lib/utils/probe/probe_server.rb', line 138

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



266
267
268
# File 'lib/utils/probe/probe_server.rb', line 266

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:

  • (Utils::ProbeServer)

    returns self to allow for method chaining



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/utils/probe/probe_server.rb', line 283

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.



119
120
121
122
# File 'lib/utils/probe/probe_server.rb', line 119

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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/utils/probe/probe_server.rb', line 34

def start
  output_message "Starting probe server listening to #{@server.to_url}", 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
      IRB.examine(self)
    ensure
      $VERBOSE = old
    end
    @server.remove_socket_path
    output_message "Quitting interactive mode, but still listening to #{@server.to_url}", type: :info
    retry
  end
end