class Utils::ProbeServer
interactive interface for managing the server.
maintains a queue of jobs, tracks their execution status, and provides an
a distributed manner, using Unix domain sockets for communication. It
This class provides a mechanism for enqueueing and running process jobs in
sockets.
A probe server for managing and executing process jobs through Unix domain
def clear
The clear method clears the terminal screen by executing the clear
def clear system "clear" end
def cmd(job)
-
(Array
- the constructed command array ready for execution)
Parameters:
-
job
(Array
) -- the job arguments to be included in the command
def cmd(job) call = [] if ENV.key?('BUNDLE_GEMFILE') and bundle = `which bundle`.full?(:chomp) call << bundle << 'exec' end call.push($0, *job) output_message "Executing #{call.inspect} now.", type: :info call end
def env
def env LogWrapper.new(self, ENV) end
def help
This method organizes and presents the documented commands along with their
descriptions.
The help method displays a formatted list of available commands and their
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
def history_clear
-
(TrueClass)
- always returns true after clearing the history
def history_clear @history = [] true end
def history_list
This method outputs all completed jobs that have been processed by the probe server,
from the server's history.
The history_list method displays the list of previously executed jobs
def history_list output_message @history end
def initialize
-
(Utils::ProbeServer)
- a new probe server instance configured
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
def inspect
-
(String)
- a formatted string containing the probe server identifier
def inspect "#<Probe #queue=#{@jobs_queue.size}>" end
def job_enqueue(args)
-
args
(Array
) -- the command arguments to be executed by the process job
def job_enqueue(args) job = ProcessJob.new(args:, probe_server: self) output_message " → #{job.inspect} enqueued.", type: :info @jobs_queue.push job end
def job_repeat(job_id = @history.last)
-
(TrueClass, FalseClass)
- true if the job was found and re-enqueued,
Parameters:
-
job_id
(Integer, Utils::ProcessJob
) -- the identifier of the job to repeat
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
def next_job_id
-
(Integer)
- the next available job identifier in the sequence
def next_job_id @current_job_id += 1 end
def output_message(msg, type: nil)
-
(Utils::ProbeServer)
- returns self to allow for method chaining
Parameters:
-
type
(Symbol
) -- the type of message for styling (success, info, warn, failure) -
msg
(String, Array
) -- the message to be displayed
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
def receive_loop
appropriate handler methods, including enqueuing process jobs and managing
incoming job requests. It processes different job types by delegating to
This method configures a background receiver on the probe server to handle
the server.
The receive_loop method sets up and starts processing incoming jobs from
def receive_loop @server.receive_in_background do |job| case job.type when 'process_job' enqueue job.args when 'set_env' env[job.key] = job.value job.respond(env: env[job.key]) when 'get_env' job.respond(env: env[job.key]) end end end
def run_job(job)
-
job
(Utils::ProcessJob
) -- the process job to be executed
def run_job(job) output_message " → #{job.inspect} now running.", type: :info system(*cmd(job.args)) message = " → #{job.inspect} was just run" if $?.success? job.ok = true message << " successfully." output_message message, type: :success else job.ok = false message << " and failed with exit status #{$?.exitstatus}!" output_message message, type: :failure end @history += [ job.freeze ] @history.freeze nil end
def shutdown
This method outputs a warning message indicating that the server is being
The shutdown method terminates the probe server process immediately.
def shutdown output_message "Server was shutdown down manually!", type: :info exit 23 end
def start
from the queue and entering a receive loop to handle incoming requests.
This method sets up the probe server by starting a thread to process jobs
The start method initializes and begins operation of the probe server.
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