Class: Utils::ProbeServer
- Includes:
- Term::ANSIColor, ServerHandling
- 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(server_type: :unix, port: 6666) ⇒ 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.
Methods included from ServerHandling
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
282 283 284 285 286 287 |
# File 'lib/utils/probe_server.rb', line 282 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
#clear ⇒ Object
The clear method clears the terminal screen by executing the clear command.
512 513 514 |
# File 'lib/utils/probe_server.rb', line 512 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
503 504 505 506 |
# File 'lib/utils/probe_server.rb', line 503 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.
345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/utils/probe_server.rb', line 345 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.
428 429 430 431 |
# File 'lib/utils/probe_server.rb', line 428 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.
415 416 417 |
# File 'lib/utils/probe_server.rb', line 415 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.
329 330 331 |
# File 'lib/utils/probe_server.rb', line 329 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.
366 367 368 369 370 |
# File 'lib/utils/probe_server.rb', line 366 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.
398 399 400 401 402 403 404 405 406 |
# File 'lib/utils/probe_server.rb', line 398 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.
526 527 528 |
# File 'lib/utils/probe_server.rb', line 526 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.
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/utils/probe_server.rb', line 543 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.
379 380 381 382 |
# File 'lib/utils/probe_server.rb', line 379 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.
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/utils/probe_server.rb', line 294 def start "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' %{\nEntering interactive mode.}, type: :info help begin old, $VERBOSE = $VERBOSE, nil IRB.examine(self) ensure $VERBOSE = old end @server.remove_socket_path "Quitting interactive mode, but still listening to #{@server.to_url}", type: :info retry end end |