Class: Cybertrain::Job::Run

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/job.rb

Overview

One claimed attempt of a job, as the block given to register receives it: the row’s id, kind and payload, the attempt number (1 for the first; the claim counted it), and the claim’s label, which with the attempt number fences the outcome write. A policy hands it to retry_later or discard, which write the outcome and mark it written, so the worker writes nothing more for it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(run_id, run_kind, run_payload, run_attempts, run_claimed_by, run_now, run_request_id = "") ⇒ Run

Returns a new instance of Run.

Parameters:

  • run_id (Integer)
  • run_kind (String)
  • run_payload (String)
  • run_attempts (Integer)
  • run_claimed_by (String)
  • run_now (Integer) —

    epoch seconds

  • run_request_id (String) (defaults to: "") —

    the enqueuing request’s id, “” for none



133
134
135
136
137
138
139
140
141
142
143
144
# File 'cybertrain/job.rb', line 133

def initialize(run_id, run_kind, run_payload, run_attempts, run_claimed_by, run_now, run_request_id = "")
  @run_id = run_id
  @run_kind = run_kind
  @run_payload = run_payload
  @run_attempts = run_attempts
  @run_claimed_by = run_claimed_by
  @run_now = run_now
  @run_request_id = run_request_id
  @run_args = Cast.empty_hash
  @run_args_parsed = false
  @run_written = false
end

Instance Attribute Details

#run_attempts ⇒ Integer (readonly)

Returns this attempt’s number, 1 for the first.

Returns:

  • (Integer) —

    this attempt’s number, 1 for the first



109
110
111
# File 'cybertrain/job.rb', line 109

def run_attempts
  @run_attempts
end

#run_claimed_by ⇒ String (readonly)

Returns the claiming process’s Cybertrain::Job.process_label.

Returns:



112
113
114
# File 'cybertrain/job.rb', line 112

def run_claimed_by
  @run_claimed_by
end

#run_id ⇒ Integer (readonly)

Returns the job’s id.

Returns:



100
101
102
# File 'cybertrain/job.rb', line 100

def run_id
  @run_id
end

#run_kind ⇒ String (readonly)

Returns the kind the job was enqueued under.

Returns:

  • (String) —

    the kind the job was enqueued under



103
104
105
# File 'cybertrain/job.rb', line 103

def run_kind
  @run_kind
end

#run_now ⇒ Integer (readonly)

Returns the epoch seconds the attempt was claimed at (the now of Cybertrain::Job.work_one); its outcome writes are timed from it.

Returns:



116
117
118
# File 'cybertrain/job.rb', line 116

def run_now
  @run_now
end

#run_payload ⇒ String (readonly)

Returns the payload given to Cybertrain::Job.enqueue, as it is.

Returns:



106
107
108
# File 'cybertrain/job.rb', line 106

def run_payload
  @run_payload
end

#run_request_id ⇒ String (readonly)

Returns the id of the request the job was enqueued in (recorded in the row’s request_id), or, for a job enqueued by another job, the request id that job carries; “” when it was enqueued outside both (a scheduled task, a feed, a command); the job’s log lines carry it.

Returns:

  • (String) —

    the id of the request the job was enqueued in (recorded in the row’s request_id), or, for a job enqueued by another job, the request id that job carries; “” when it was enqueued outside both (a scheduled task, a feed, a command); the job’s log lines carry it



123
124
125
# File 'cybertrain/job.rb', line 123

def run_request_id
  @run_request_id
end

Instance Method Details

#run_args ⇒ Hash{String => Object}

The payload parsed as a JSON object, computed once. A payload that is not a JSON object (plain text, an Array, invalid JSON, “”) gives an empty Hash; it never raises.

Returns:

  • (Hash{String => Object})


151
152
153
154
155
156
157
# File 'cybertrain/job.rb', line 151

def run_args
  unless @run_args_parsed
    @run_args = Cast.json_hash(@run_payload)
    @run_args_parsed = true
  end
  @run_args
end

#run_outcome_written! ⇒ nil

Marks the outcome written: the worker then writes nothing for this attempt when the block returns or raises.

Returns:

  • (nil)


170
171
172
173
# File 'cybertrain/job.rb', line 170

def run_outcome_written!
  @run_written = true
  nil
end

#run_outcome_written? ⇒ Boolean

Returns true once Cybertrain::Job.retry_later or Cybertrain::Job.discard wrote, or tried to write, this attempt’s outcome.

Returns:



162
163
164
# File 'cybertrain/job.rb', line 162

def run_outcome_written?
  @run_written
end