Class: Conductor::Worker::TaskContext

Inherits:
Object
  • Object
show all
Defined in:
lib/conductor/worker/task_context.rb

Overview

Provides execution context for workers Accessible from anywhere in worker code via TaskContext.current Stored in thread-local storage (Thread.current)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, task_result) ⇒ TaskContext

Initialize a new task context

Parameters:

  • task (Task)

    The task being executed

  • task_result (TaskResult)

    The task result being built



37
38
39
40
# File 'lib/conductor/worker/task_context.rb', line 37

def initialize(task, task_result)
  @task = task
  @task_result = task_result
end

Instance Attribute Details

#taskTask (readonly)

Returns The task being executed.

Returns:

  • (Task)

    The task being executed



10
11
12
# File 'lib/conductor/worker/task_context.rb', line 10

def task
  @task
end

#task_resultTaskResult (readonly)

Returns The task result being built.

Returns:

  • (TaskResult)

    The task result being built



13
14
15
# File 'lib/conductor/worker/task_context.rb', line 13

def task_result
  @task_result
end

Class Method Details

.clearvoid

This method returns an undefined value.

Clear the current task context (internal use by TaskRunner)



30
31
32
# File 'lib/conductor/worker/task_context.rb', line 30

def self.clear
  Thread.current[:conductor_task_context] = nil
end

.currentTaskContext?

Get the current task context (thread-local)

Returns:

  • (TaskContext, nil)

    Current context or nil if not in a task execution



17
18
19
# File 'lib/conductor/worker/task_context.rb', line 17

def self.current
  Thread.current[:conductor_task_context]
end

.current=(context) ⇒ void

This method returns an undefined value.

Set the current task context (internal use by TaskRunner)

Parameters:



24
25
26
# File 'lib/conductor/worker/task_context.rb', line 24

def self.current=(context)
  Thread.current[:conductor_task_context] = context
end

Instance Method Details

#add_log(message) ⇒ void

This method returns an undefined value.

Add a log message to the task result Logs are visible in the Conductor UI

Parameters:

  • message (String)

    Log message



88
89
90
# File 'lib/conductor/worker/task_context.rb', line 88

def add_log(message)
  @task_result.log(message)
end

#callback_after_secondsInteger?

Get the callback_after_seconds value

Returns:

  • (Integer, nil)


102
103
104
# File 'lib/conductor/worker/task_context.rb', line 102

def callback_after_seconds
  @task_result.callback_after_seconds
end

#inputHash

Get the task input data

Returns:

  • (Hash)


68
69
70
# File 'lib/conductor/worker/task_context.rb', line 68

def input
  @task.input_data || {}
end

#poll_countInteger

Get the poll count (how many times this task has been polled for long-running tasks)

Returns:

  • (Integer)


62
63
64
# File 'lib/conductor/worker/task_context.rb', line 62

def poll_count
  @task.poll_count || 0
end

#retry_countInteger

Get the retry count (how many times this task has been retried)

Returns:

  • (Integer)


56
57
58
# File 'lib/conductor/worker/task_context.rb', line 56

def retry_count
  @task.retry_count || 0
end

#set_callback_after(seconds) ⇒ void

This method returns an undefined value.

Set the callback_after_seconds for long-running tasks When returning TaskInProgress, this determines when Conductor will poll again

Parameters:

  • seconds (Integer)

    Seconds to wait before polling again



96
97
98
# File 'lib/conductor/worker/task_context.rb', line 96

def set_callback_after(seconds)
  @task_result.callback_after_seconds = seconds
end

#set_output(output_data) ⇒ void

This method returns an undefined value.

Set the output data on the task result

Parameters:

  • output_data (Hash)

    Output data



109
110
111
# File 'lib/conductor/worker/task_context.rb', line 109

def set_output(output_data)
  @task_result.output_data = output_data
end

#task_def_nameString

Get the task definition name

Returns:

  • (String)


74
75
76
# File 'lib/conductor/worker/task_context.rb', line 74

def task_def_name
  @task.task_def_name || @task.task_type
end

#task_idString

Get the task ID

Returns:

  • (String)


44
45
46
# File 'lib/conductor/worker/task_context.rb', line 44

def task_id
  @task.task_id
end

#workflow_instance_idString

Get the workflow instance ID

Returns:

  • (String)


50
51
52
# File 'lib/conductor/worker/task_context.rb', line 50

def workflow_instance_id
  @task.workflow_instance_id
end

#workflow_task_typeString

Get the workflow task type

Returns:

  • (String)


80
81
82
# File 'lib/conductor/worker/task_context.rb', line 80

def workflow_task_type
  @task.workflow_task&.type || @task.task_type
end