Module: Hatchet::ContextVars
- Defined in:
- lib/hatchet/context_vars.rb
Overview
Thread-local context variables for parent-child workflow dispatch linking.
When the worker runner invokes a task block, it sets these thread-local variables from the Action object. When a child workflow is spawned from within that task (via ‘workflow.run` or `task.run`), the admin client reads these variables to auto-populate parent linkage fields.
IMPORTANT: These must be cleaned up in an ‘ensure` block after each task execution to prevent leaking into the next task on the same thread (thread pool reuse scenario).
Defined Under Namespace
Classes: SpawnIndexTracker
Constant Summary collapse
- KEYS =
%i[ hatchet_workflow_run_id hatchet_step_run_id hatchet_worker_id hatchet_action_key hatchet_additional_metadata hatchet_retry_count ].freeze
Class Method Summary collapse
-
.action_key ⇒ String?
The current action key.
-
.additional_metadata ⇒ Hash
The current additional metadata.
-
.clear ⇒ Object
Clear all context variables for the current thread.
-
.retry_count ⇒ Integer
The current retry count.
-
.set(workflow_run_id:, step_run_id:, worker_id:, action_key:, additional_metadata: {}, retry_count: 0) ⇒ Object
Set all context variables for the current thread.
-
.step_run_id ⇒ String?
The current step run ID.
-
.worker_id ⇒ String?
The current worker ID.
-
.workflow_run_id ⇒ String?
The current workflow run ID.
Class Method Details
.action_key ⇒ String?
Returns The current action key.
78 79 80 |
# File 'lib/hatchet/context_vars.rb', line 78 def action_key Thread.current[:hatchet_action_key] end |
.additional_metadata ⇒ Hash
Returns The current additional metadata.
83 84 85 |
# File 'lib/hatchet/context_vars.rb', line 83 def Thread.current[:hatchet_additional_metadata] || {} end |
.clear ⇒ Object
Clear all context variables for the current thread. MUST be called in an ensure block after task execution.
58 59 60 |
# File 'lib/hatchet/context_vars.rb', line 58 def clear KEYS.each { |key| Thread.current[key] = nil } end |
.retry_count ⇒ Integer
Returns The current retry count.
88 89 90 |
# File 'lib/hatchet/context_vars.rb', line 88 def retry_count Thread.current[:hatchet_retry_count] || 0 end |
.set(workflow_run_id:, step_run_id:, worker_id:, action_key:, additional_metadata: {}, retry_count: 0) ⇒ Object
Set all context variables for the current thread
47 48 49 50 51 52 53 54 |
# File 'lib/hatchet/context_vars.rb', line 47 def set(workflow_run_id:, step_run_id:, worker_id:, action_key:, additional_metadata: {}, retry_count: 0) Thread.current[:hatchet_workflow_run_id] = workflow_run_id Thread.current[:hatchet_step_run_id] = step_run_id Thread.current[:hatchet_worker_id] = worker_id Thread.current[:hatchet_action_key] = action_key Thread.current[:hatchet_additional_metadata] = Thread.current[:hatchet_retry_count] = retry_count end |
.step_run_id ⇒ String?
Returns The current step run ID.
68 69 70 |
# File 'lib/hatchet/context_vars.rb', line 68 def step_run_id Thread.current[:hatchet_step_run_id] end |
.worker_id ⇒ String?
Returns The current worker ID.
73 74 75 |
# File 'lib/hatchet/context_vars.rb', line 73 def worker_id Thread.current[:hatchet_worker_id] end |
.workflow_run_id ⇒ String?
Returns The current workflow run ID.
63 64 65 |
# File 'lib/hatchet/context_vars.rb', line 63 def workflow_run_id Thread.current[:hatchet_workflow_run_id] end |