Class: Utils::ProcessJob
- Includes:
- Term::ANSIColor
- Defined in:
- lib/utils/probe_server.rb
Overview
A process job representation for execution within the probe server system.
This class encapsulates the information and behavior associated with a single executable task that can be enqueued and processed by a ProbeServer. It holds command arguments, manages execution status, and provides mechanisms for serialization and display of job information.
Instance Attribute Summary collapse
-
#args ⇒ Array
readonly
The args reader method provides access to the arguments stored in the instance.
-
#id ⇒ Integer
readonly
Returns the unique identifier of the process job.
-
#ok ⇒ String
The ok method returns a character representation of the job’s success status.
Instance Method Summary collapse
-
#as_json ⇒ Hash
The as_json method converts the process job object into a JSON-serializable hash.
-
#initialize(args:, probe_server: nil) ⇒ Utils::ProcessJob
constructor
Initializes a new ProcessJob instance with the specified arguments and optional probe server.
-
#inspect ⇒ String
(also: #to_s)
The inspect method generates a colorized string representation of the process job.
-
#ok_colorize(string) ⇒ String
The ok_colorize method applies color formatting to a string based on the success status.
-
#to_json ⇒ String
The to_json method converts the object to a JSON string representation.
-
#type ⇒ String
Returns the type identifier for the process job.
Constructor Details
#initialize(args:, probe_server: nil) ⇒ Utils::ProcessJob
Initializes a new ProcessJob instance with the specified arguments and optional probe server.
This method creates a process job object that can be enqueued for execution by a probe server. It assigns a unique job ID from the probe server if provided and stores the command arguments as an array.
to use for generating job IDs
the provided arguments and server reference
27 28 29 30 |
# File 'lib/utils/probe_server.rb', line 27 def initialize(args:, probe_server: nil) @id = probe_server&.next_job_id @args = Array(args) end |
Instance Attribute Details
#args ⇒ Array (readonly)
The args reader method provides access to the arguments stored in the instance.
41 42 43 |
# File 'lib/utils/probe_server.rb', line 41 def args @args end |
#id ⇒ Integer (readonly)
Returns the unique identifier of the process job.
35 36 37 |
# File 'lib/utils/probe_server.rb', line 35 def id @id end |
#ok ⇒ String
The ok method returns a character representation of the job’s success status.
This method provides a visual indicator of whether a process job has succeeded, failed, or is still in progress. It returns ‘y’ for successful jobs, ‘n’ for failed jobs, and ‘…’ for jobs that are currently running or pending.
the status is unknown
69 70 71 72 73 74 75 |
# File 'lib/utils/probe_server.rb', line 69 def ok case @ok when false then 'n' when true then 'y' else '…' end end |
Instance Method Details
#as_json ⇒ Hash
The as_json method converts the process job object into a JSON-serializable hash.
This method creates and returns a hash representation of the process job, containing its type, unique identifier, and command arguments.
118 119 120 |
# File 'lib/utils/probe_server.rb', line 118 def as_json(*) { type:, id:, args:, } end |
#inspect ⇒ String Also known as: to_s
The inspect method generates a colorized string representation of the process job.
This method creates a formatted string that includes the job’s unique identifier and its command arguments, with the status indicator color-coded based on whether the job succeeded, failed, or is pending.
105 106 107 |
# File 'lib/utils/probe_server.rb', line 105 def inspect ok_colorize("#{id} #{args.map { |a| a.include?(' ') ? a.inspect : a } * ' '}") end |
#ok_colorize(string) ⇒ String
The ok_colorize method applies color formatting to a string based on the success status.
This method returns the input string wrapped with color codes to indicate whether the associated process job succeeded, failed, or is in progress. Successful jobs are highlighted in green, failed jobs in red, and pending jobs are returned without any color formatting.
88 89 90 91 92 93 94 |
# File 'lib/utils/probe_server.rb', line 88 def ok_colorize(string) case @ok when false then white { on_red { string } } when true then black { on_green { string } } else string end end |
#to_json ⇒ String
The to_json method converts the object to a JSON string representation.
This method delegates to the as_json method to generate a hash representation of the object, then converts that hash to a JSON string using the standard JSON library’s to_json method.
129 130 131 |
# File 'lib/utils/probe_server.rb', line 129 def to_json(*) as_json.to_json(*) end |
#type ⇒ String
Returns the type identifier for the process job.
This method provides a constant string value that identifies the object as a process job within the probe server system, facilitating type-based dispatch and handling.
55 56 57 |
# File 'lib/utils/probe_server.rb', line 55 def type 'process_job' end |