Class: Aidp::Temporal::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/temporal/worker.rb

Overview

Temporal worker that runs workflows and activities Manages the worker lifecycle and task queue configuration

Constant Summary collapse

DEFAULT_TASK_QUEUE =
"aidp-workflows"
DEFAULT_MAX_CONCURRENT_ACTIVITIES =
10
DEFAULT_MAX_CONCURRENT_WORKFLOWS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection:, config: {}) ⇒ Worker

Returns a new instance of Worker.



17
18
19
20
21
22
23
24
# File 'lib/aidp/temporal/worker.rb', line 17

def initialize(connection:, config: {})
  @connection = connection
  @config = normalize_config(config)
  @task_queue = @config[:task_queue]
  @worker = nil
  @running = false
  @shutdown_requested = false
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/aidp/temporal/worker.rb', line 15

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



15
16
17
# File 'lib/aidp/temporal/worker.rb', line 15

def connection
  @connection
end

#task_queueObject (readonly)

Returns the value of attribute task_queue.



15
16
17
# File 'lib/aidp/temporal/worker.rb', line 15

def task_queue
  @task_queue
end

Instance Method Details

#register_activities(*activity_classes) ⇒ Object

Register activity classes or instances



34
35
36
37
38
# File 'lib/aidp/temporal/worker.rb', line 34

def register_activities(*activity_classes)
  @activity_classes ||= []
  @activity_classes.concat(activity_classes)
  self
end

#register_workflows(*workflow_classes) ⇒ Object

Register workflow classes



27
28
29
30
31
# File 'lib/aidp/temporal/worker.rb', line 27

def register_workflows(*workflow_classes)
  @workflow_classes ||= []
  @workflow_classes.concat(workflow_classes)
  self
end

#runObject

Start the worker (blocking)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aidp/temporal/worker.rb', line 41

def run
  return if @running

  Aidp.log_info("temporal_worker", "starting",
    task_queue: @task_queue,
    workflows: @workflow_classes&.map(&:name),
    activities: @activity_classes&.map { |a| a.is_a?(Class) ? a.name : a.class.name })

  @running = true
  @shutdown_requested = false

  begin
    @worker = create_worker
    @worker.run
  rescue => e
    Aidp.log_error("temporal_worker", "run_failed",
      error: e.message,
      error_class: e.class.name)
    raise
  ensure
    @running = false
    @worker = nil
  end
end

#running?Boolean

Check if running

Returns:

  • (Boolean)


76
77
78
# File 'lib/aidp/temporal/worker.rb', line 76

def running?
  @running
end

#shutdownObject

Request graceful shutdown



67
68
69
70
71
72
73
# File 'lib/aidp/temporal/worker.rb', line 67

def shutdown
  return unless @running

  Aidp.log_info("temporal_worker", "shutdown_requested", task_queue: @task_queue)
  @shutdown_requested = true
  @worker&.shutdown
end

#shutdown_requested?Boolean

Check if shutdown was requested

Returns:

  • (Boolean)


81
82
83
# File 'lib/aidp/temporal/worker.rb', line 81

def shutdown_requested?
  @shutdown_requested
end