Class: Zeridion::Flare::JobContext
- Inherits:
-
Object
- Object
- Zeridion::Flare::JobContext
- 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
-
#attempt ⇒ Integer
readonly
1-based attempt counter.
-
#cancellation ⇒ Worker::CancellationToken
readonly
The cooperative cancellation handle.
-
#enqueued_at ⇒ Time?
readonly
When the job was originally enqueued (offset-aware).
-
#job_id ⇒ String
readonly
Unique id of this job execution.
-
#job_type ⇒ String
readonly
The routing job_type for this execution.
-
#logger ⇒ Logger
readonly
Logger pre-bound with this job's fields so handler logs auto-correlate.
-
#max_attempts ⇒ Integer
readonly
Max attempts configured for this job.
Class Method Summary collapse
-
.parse_timestamp(raw) ⇒ Time?
Parse a server RFC-3339 timestamp defensively.
Instance Method Summary collapse
-
#cancelled? ⇒ Boolean
True once cancellation has been requested (server "cancel", host shutdown, or timeout).
-
#check_cancellation! ⇒ Object
Raise Worker::JobCancelledError if cancellation has been requested.
-
#initialize(job_id:, job_type:, attempt:, max_attempts:, enqueued_at:, logger:, cancellation:, progress_slot:) ⇒ JobContext
constructor
A new instance of JobContext.
-
#report_progress(fraction) ⇒ void
Report progress (0.0–1.0) for the dashboard.
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
#attempt ⇒ Integer (readonly)
Returns 1-based attempt counter.
21 22 23 |
# File 'lib/zeridion_flare/worker/job_context.rb', line 21 def attempt @attempt end |
#cancellation ⇒ Worker::CancellationToken (readonly)
Returns the cooperative cancellation handle.
36 37 38 |
# File 'lib/zeridion_flare/worker/job_context.rb', line 36 def cancellation @cancellation end |
#enqueued_at ⇒ Time? (readonly)
Returns 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_id ⇒ String (readonly)
Returns 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_type ⇒ String (readonly)
Returns 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 |
#logger ⇒ Logger (readonly)
Returns 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_attempts ⇒ Integer (readonly)
Returns 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.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/zeridion_flare/worker/job_context.rb', line 78 def self.(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.
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.
67 68 69 70 |
# File 'lib/zeridion_flare/worker/job_context.rb', line 67 def report_progress(fraction) @progress_slot.set(fraction) nil end |