Class: Wurk::Status::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/status/record.rb

Overview

Read-only view of one status:<jid> HASH. Redis hands back strings for everything; this is where they become the types a caller expects, so no consumer (dashboard, HTTP API, a chain deciding what to pipe onward) has to know the row is stringly-typed on the wire.

A field the writer never set reads as nil rather than 0 or '' — "this job reported no progress" and "this job is at 0" are different answers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jid, data) ⇒ Record

Returns a new instance of Record.



17
18
19
20
# File 'lib/wurk/status/record.rb', line 17

def initialize(jid, data)
  @jid  = jid
  @data = data.freeze
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



15
16
17
# File 'lib/wurk/status/record.rb', line 15

def data
  @data
end

#jidObject (readonly)

Returns the value of attribute jid.



15
16
17
# File 'lib/wurk/status/record.rb', line 15

def jid
  @jid
end

Instance Method Details

#[](field) ⇒ Object

Raw field access for anything without a reader.



67
# File 'lib/wurk/status/record.rb', line 67

def [](field) = @data[field.to_s]

#attemptObject



34
# File 'lib/wurk/status/record.rb', line 34

def attempt  = @data['attempt']&.to_i

#enqueued_atObject



36
# File 'lib/wurk/status/record.rb', line 36

def enqueued_at = @data['enqueued_at']&.to_f

#error_classObject



25
# File 'lib/wurk/status/record.rb', line 25

def error_class   = @data['error_class']

#error_messageObject



26
# File 'lib/wurk/status/record.rb', line 26

def error_message = @data['error_message']

#finished_atObject



38
# File 'lib/wurk/status/record.rb', line 38

def finished_at = @data['finished_at']&.to_f

#job_classObject

class is the wire field name — it matches the job hash — but it is not a legal reader name.



30
# File 'lib/wurk/status/record.rb', line 30

def job_class = @data['class']

#messageObject



24
# File 'lib/wurk/status/record.rb', line 24

def message       = @data['message']

#progressObject



32
# File 'lib/wurk/status/record.rb', line 32

def progress = @data['progress']&.to_i

#queueObject



23
# File 'lib/wurk/status/record.rb', line 23

def queue         = @data['queue']

#resultObject

The stored return value of perform, decoded. JSON only — see CLAUDE.md; a result that wouldn't serialize never reached Redis.

A truncated result is a JSON fragment, so there is nothing to decode: it comes back as the raw head string, which is still the most useful thing anyone can be handed — the alternative is nothing at all.



58
59
60
61
62
63
64
# File 'lib/wurk/status/record.rb', line 58

def result
  raw = @data['result']
  return nil if raw.nil?
  return raw if result_truncated?

  ::JSON.parse(raw)
end

#result_truncated?Boolean

True when the return value was bigger than Middleware::Status::MAX_RESULT_BYTES and only its head was kept. Check this before treating #result as data.

Returns:

  • (Boolean)


43
# File 'lib/wurk/status/record.rb', line 43

def result_truncated? = @data['result_truncated'] == '1'

#result_withheld?Boolean

True when the job's class set encrypt: true, so its return value was deliberately never stored — see Middleware::Status#withhold_result?. Separates "the job returned nothing" from "the job returned something Wurk refused to keep beside the encrypted args in plaintext".

Returns:

  • (Boolean)


50
# File 'lib/wurk/status/record.rb', line 50

def result_withheld? = @data['result_withheld'] == '1'

#started_atObject



37
# File 'lib/wurk/status/record.rb', line 37

def started_at  = @data['started_at']&.to_f

#stateObject



22
# File 'lib/wurk/status/record.rb', line 22

def state         = @data['state']

#to_hObject

Wire field names, not reader names: this hash is what the dashboard and the HTTP API serve, and class is the name every other Wurk JSON surface uses for a job's class.



72
73
74
75
76
77
78
# File 'lib/wurk/status/record.rb', line 72

def to_h
  { 'jid' => jid, 'state' => state, 'queue' => queue, 'class' => job_class,
    'enqueued_at' => enqueued_at, 'started_at' => started_at, 'finished_at' => finished_at,
    'progress' => progress, 'total' => total, 'message' => message, 'result' => result,
    'result_truncated' => result_truncated?, 'result_withheld' => result_withheld?,
    'error_class' => error_class, 'error_message' => error_message, 'attempt' => attempt }
end

#totalObject



33
# File 'lib/wurk/status/record.rb', line 33

def total    = @data['total']&.to_i