Class: Utils::ProcessJob

Inherits:
Object show all
Includes:
Term::ANSIColor
Defined in:
lib/utils/probe_server.rb

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



21
22
23
24
# File 'lib/utils/probe_server.rb', line 21

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



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

def args
  @args
end

#idInteger (readonly)

Returns the unique identifier of the process job.

Returns:

  • (Integer)

    the job ID



29
30
31
# File 'lib/utils/probe_server.rb', line 29

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



63
64
65
66
67
68
69
# File 'lib/utils/probe_server.rb', line 63

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



112
113
114
# File 'lib/utils/probe_server.rb', line 112

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



99
100
101
# File 'lib/utils/probe_server.rb', line 99

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



82
83
84
85
86
87
88
# File 'lib/utils/probe_server.rb', line 82

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



123
124
125
# File 'lib/utils/probe_server.rb', line 123

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



49
50
51
# File 'lib/utils/probe_server.rb', line 49

def type
  'process_job'
end