Class: Utils::ProbeServer
- 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
-
#clear ⇒ Object
The clear method clears the terminal screen by executing the clear command.
-
#env ⇒ Utils::ProbeServer::LogWrapper
The env method provides access to the server’s environment variables through a wrapped interface.
-
#help ⇒ Object
The help method displays a formatted list of available commands and their descriptions.
-
#history_clear ⇒ TrueClass
The history_clear method clears all entries from the server’s execution history.
-
#history_list ⇒ Object
The history_list method displays the list of previously executed jobs from the server’s history.
-
#initialize ⇒ Utils::ProbeServer
constructor
The initialize method sets up a new probe server instance.
-
#inspect ⇒ String
(also: #to_s)
The inspect method returns a string representation of the probe server instance.
-
#job_enqueue(args) ⇒ Object
(also: #enqueue)
The job_enqueue method adds a new process job to the execution queue.
-
#job_repeat(job_id = @history.last) ⇒ TrueClass, FalseClass
The job_repeat method re-executes a previously run job from the history.
-
#next_job_id ⇒ Integer
The next_job_id method increments and returns the current job identifier.
-
#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.
-
#shutdown ⇒ Object
The shutdown method terminates the probe server process immediately.
-
#start ⇒ Object
The start method initializes and begins operation of the probe server.
Constructor Details
#initialize ⇒ 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
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
#clear ⇒ Object
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 |
#env ⇒ Utils::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
469 470 471 472 |
# File 'lib/utils/probe_server.rb', line 469 memoize method: def env LogWrapper.new(self, ENV) end |
#help ⇒ Object
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" [ 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_clear ⇒ TrueClass
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.
394 395 396 397 |
# File 'lib/utils/probe_server.rb', line 394 def history_clear @history = [] true end |
#history_list ⇒ Object
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 @history end |
#inspect ⇒ String 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.
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.
332 333 334 335 336 |
# File 'lib/utils/probe_server.rb', line 332 def job_enqueue(args) job = ProcessJob.new(args:, probe_server: self) " → #{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.
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_id ⇒ Integer
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.
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.
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 (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 |
#shutdown ⇒ Object
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 "Server was shutdown down manually!", type: :info exit 23 end |
#start ⇒ Object
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 "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' %{\nEntering interactive mode.}, type: :info help begin old, $VERBOSE = $VERBOSE, nil examine(self) ensure $VERBOSE = old end @server.remove_socket_path "Quitting interactive mode, but still listening to #{@server.server_socket_path}.", type: :info retry end end |