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).

Examples:

Setting context vars (runner side)

Hatchet::ContextVars.set(
  workflow_run_id: action.workflow_run_id,
  step_run_id: action.step_run_id,
  worker_id: action.worker_id,
  action_key: action.key,
  additional_metadata: action.,
  retry_count: action.retry_count
)

Reading context vars (admin client side)

parent_id = Hatchet::ContextVars.workflow_run_id
parent_step_run_id = Hatchet::ContextVars.step_run_id

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

Class Method Details

.action_keyString?

Returns The current action key.

Returns:

  • (String, nil)

    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_metadataHash

Returns The current additional metadata.

Returns:

  • (Hash)

    The current additional metadata



83
84
85
# File 'lib/hatchet/context_vars.rb', line 83

def 
  Thread.current[:hatchet_additional_metadata] || {}
end

.clearObject

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_countInteger

Returns The current retry count.

Returns:

  • (Integer)

    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

Parameters:

  • workflow_run_id (String)

    The workflow run ID

  • step_run_id (String)

    The step run ID

  • worker_id (String)

    The worker ID

  • action_key (String)

    The action key

  • additional_metadata (Hash) (defaults to: {})

    Additional metadata

  • retry_count (Integer) (defaults to: 0)

    Retry count



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_idString?

Returns The current step run ID.

Returns:

  • (String, nil)

    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_idString?

Returns The current worker ID.

Returns:

  • (String, nil)

    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_idString?

Returns The current workflow run ID.

Returns:

  • (String, nil)

    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