Class: Zeridion::Flare::JobContext

Inherits:
Object
  • Object
show all
Defined in:
lib/zeridion_flare/worker/job_context.rb

Overview

Context handed to every job's #perform during execution. Constructed by the worker runtime — never built by application code.

Lives at Zeridion::Flare::JobContext (not under ::Worker) to mirror the top-level JobContext in the reference SDK, so a ctx parameter reads the same across languages.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id:, job_type:, attempt:, max_attempts:, enqueued_at:, logger:, cancellation:, progress_slot:) ⇒ JobContext

Returns a new instance of JobContext.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zeridion_flare/worker/job_context.rb', line 38

def initialize(job_id:, job_type:, attempt:, max_attempts:, enqueued_at:, logger:, cancellation:,
               progress_slot:)
  @job_id = job_id
  @job_type = job_type
  @attempt = attempt
  @max_attempts = max_attempts
  @enqueued_at = enqueued_at
  @logger = logger
  @cancellation = cancellation
  @progress_slot = progress_slot
end

Instance Attribute Details

#attemptInteger (readonly)

Returns 1-based attempt counter.

Returns:

  • (Integer)

    1-based attempt counter.



21
22
23
# File 'lib/zeridion_flare/worker/job_context.rb', line 21

def attempt
  @attempt
end

#cancellationWorker::CancellationToken (readonly)

Returns the cooperative cancellation handle.

Returns:



36
37
38
# File 'lib/zeridion_flare/worker/job_context.rb', line 36

def cancellation
  @cancellation
end

#enqueued_atTime? (readonly)

Returns when the job was originally enqueued (offset-aware). nil if the server sent an unparseable timestamp — parsing never raises into dispatch.

Returns:

  • (Time, nil)

    when the job was originally enqueued (offset-aware). nil if the server sent an unparseable timestamp — parsing never raises into dispatch.



29
30
31
# File 'lib/zeridion_flare/worker/job_context.rb', line 29

def enqueued_at
  @enqueued_at
end

#job_idString (readonly)

Returns unique id of this job execution.

Returns:

  • (String)

    unique id of this job execution.



15
16
17
# File 'lib/zeridion_flare/worker/job_context.rb', line 15

def job_id
  @job_id
end

#job_typeString (readonly)

Returns the routing job_type for this execution.

Returns:

  • (String)

    the routing job_type for this execution.



18
19
20
# File 'lib/zeridion_flare/worker/job_context.rb', line 18

def job_type
  @job_type
end

#loggerLogger (readonly)

Returns logger pre-bound with this job's fields so handler logs auto-correlate.

Returns:

  • (Logger)

    logger pre-bound with this job's fields so handler logs auto-correlate.



33
34
35
# File 'lib/zeridion_flare/worker/job_context.rb', line 33

def logger
  @logger
end

#max_attemptsInteger (readonly)

Returns max attempts configured for this job.

Returns:

  • (Integer)

    max attempts configured for this job.



24
25
26
# File 'lib/zeridion_flare/worker/job_context.rb', line 24

def max_attempts
  @max_attempts
end

Class Method Details

.parse_timestamp(raw) ⇒ Time?

Parse a server RFC-3339 timestamp defensively. Accepts both Z and a numeric offset. Returns nil (never raises) on a malformed value so a bad timestamp can't kill dispatch.

Parameters:

  • raw (String, nil)

Returns:

  • (Time, nil)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/zeridion_flare/worker/job_context.rb', line 78

def self.parse_timestamp(raw)
  return nil if raw.nil? || raw.to_s.empty?

  Time.iso8601(raw.to_s)
rescue ArgumentError, TypeError
  begin
    Time.parse(raw.to_s)
  rescue ArgumentError, TypeError
    nil
  end
end

Instance Method Details

#cancelled?Boolean

Returns true once cancellation has been requested (server "cancel", host shutdown, or timeout). Check this in long handlers.

Returns:

  • (Boolean)

    true once cancellation has been requested (server "cancel", host shutdown, or timeout). Check this in long handlers.



52
53
54
# File 'lib/zeridion_flare/worker/job_context.rb', line 52

def cancelled?
  @cancellation.cancelled?
end

#check_cancellation!Object

Raise Worker::JobCancelledError if cancellation has been requested.



57
58
59
# File 'lib/zeridion_flare/worker/job_context.rb', line 57

def check_cancellation!
  @cancellation.check!
end

#report_progress(fraction) ⇒ void

This method returns an undefined value.

Report progress (0.0–1.0) for the dashboard. Clamp-only last-write-wins: the latest value rides the next heartbeat. nil / NaN / <= 0 are dropped (no beat carries them); > 1 is clamped to 1.0. Never raises.

Parameters:

  • fraction (Numeric)


67
68
69
70
# File 'lib/zeridion_flare/worker/job_context.rb', line 67

def report_progress(fraction)
  @progress_slot.set(fraction)
  nil
end