Class: Utils::ProcessJob

Inherits:
Object show all
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

Instance Method Summary collapse

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

Parameters:

  • args (Array)

    the command arguments to be executed by the job

  • probe_server (Utils::ProbeServer, nil) (defaults to: nil)

    the probe server instance



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

#argsArray (readonly)

The args reader method provides access to the arguments stored in the instance.

Returns:

  • (Array)

    the array of arguments



41
42
43
# File 'lib/utils/probe_server.rb', line 41

def args
  @args
end

#idInteger (readonly)

Returns the unique identifier of the process job.

Returns:

  • (Integer)

    the job ID



35
36
37
# File 'lib/utils/probe_server.rb', line 35

def id
  @id
end

#okString

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

Returns:

  • (String)

    ‘y’ if the job succeeded, ‘n’ if it failed, or ‘…’ if



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_jsonHash

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.

Returns:

  • (Hash)

    a hash containing the type, id, and args of the process job



118
119
120
# File 'lib/utils/probe_server.rb', line 118

def as_json(*)
  { type:, id:, args:, }
end

#inspectString 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.

Returns:

  • (String)

    a formatted string representation of the process job including its ID, arguments, and color-coded status indicator



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.

Parameters:

  • string (String)

    the string to be colorized

Returns:

  • (String)

    the colorized string or the original string if status is unknown



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_jsonString

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.

Returns:

  • (String)

    a JSON string representation of the object



129
130
131
# File 'lib/utils/probe_server.rb', line 129

def to_json(*)
  as_json.to_json(*)
end

#typeString

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.

Returns:

  • (String)

    the string ‘process_job’ indicating the object’s type



55
56
57
# File 'lib/utils/probe_server.rb', line 55

def type
  'process_job'
end