Class: Collavre::Comments::WorkflowExecutor

Inherits:
Object
  • Object
show all
Includes:
Concerns::WorkflowSupport
Defined in:
app/services/collavre/comments/workflow_executor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_task) ⇒ WorkflowExecutor

Returns a new instance of WorkflowExecutor.



10
11
12
13
# File 'app/services/collavre/comments/workflow_executor.rb', line 10

def initialize(parent_task)
  @parent_task = parent_task
  @state = parent_task.workflow_state || {}
end

Class Method Details

.advance!(parent_task) ⇒ Object



6
7
8
# File 'app/services/collavre/comments/workflow_executor.rb', line 6

def self.advance!(parent_task)
  new(parent_task).advance!
end

Instance Method Details

#advance!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/collavre/comments/workflow_executor.rb', line 15

def advance!
  loop do
    return if @parent_task.status == "cancelled"

    pending = @state["pending_creative_ids"] || []

    if pending.empty?
      complete_workflow!
      return
    end

    next_creative_id = pending.first
    next_creative = Creative.find_by(id: next_creative_id)

    unless next_creative
      skip_current!
      next
    end

    @state["current_creative_id"] = next_creative_id
    @parent_task.update!(workflow_state: @state)

    sub_task_context = build_subtask_context(next_creative)

    sub_task = Task.create!(
      name: "Work on: #{next_creative.description&.truncate(50)}",
      status: "pending",
      agent: @parent_task.agent,
      creative: next_creative,
      parent_task: @parent_task,
      workflow_context: @parent_task.workflow_context,
      trigger_event_name: "workflow_subtask",
      trigger_event_payload: sub_task_context,
      topic_id: nil
    )

    post_progress_notice(next_creative)
    AiAgentJob.perform_later(sub_task)
    return
  end
end

#complete_subtask!(sub_task) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/services/collavre/comments/workflow_executor.rb', line 57

def complete_subtask!(sub_task)
  completed = @state["completed_creative_ids"] || []
  pending = @state["pending_creative_ids"] || []

  Rails.logger.info(
    "[WorkflowExecutor] complete_subtask! task=#{sub_task.id} creative=#{sub_task.creative_id} " \
    "pending=#{pending.inspect} completed=#{completed.inspect}"
  )

  completed << sub_task.creative_id
  pending.delete(sub_task.creative_id)

  @state["completed_creative_ids"] = completed
  @state["pending_creative_ids"] = pending
  @state["current_creative_id"] = nil
  @parent_task.update!(workflow_state: @state)

  total = @state["total"] || 1
  progress = completed.size.to_f / total
  @parent_task.creative&.update!(progress: progress.clamp(0.0, 1.0))

  Rails.logger.info(
    "[WorkflowExecutor] Progress updated: #{completed.size}/#{total} (#{(progress * 100).round}%)"
  )

  post_subtask_completed_notice(sub_task, completed.size, total)

  advance!
end

#fail_subtask!(sub_task, error_message: nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/collavre/comments/workflow_executor.rb', line 87

def fail_subtask!(sub_task, error_message: nil)
  @state["current_creative_id"] = nil
  @parent_task.update!(
    status: "failed",
    workflow_state: @state.merge(
      "failed_creative_id" => sub_task.creative_id,
      "failure_reason" => error_message
    )
  )

  post_failure_notice(sub_task, error_message)
end

#resume!Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/services/collavre/comments/workflow_executor.rb', line 119

def resume!
  # Re-check pending creatives — some may have been completed manually
  pending = @state["pending_creative_ids"] || []
  pending = refilter_pending(pending)
  @state["pending_creative_ids"] = pending
  @state["current_creative_id"] = nil

  # Clear failure state
  @state.delete("failed_creative_id")
  @state.delete("failure_reason")

  @parent_task.update!(
    status: "running",
    workflow_state: @state
  )

  post_notice(
    I18n.t("collavre.comments.work_command.workflow_resumed",
           agent: @parent_task.agent.display_name,
           remaining: pending.size)
  )

  advance!
end

#stop!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/services/collavre/comments/workflow_executor.rb', line 100

def stop!
  # Cancel running sub-task if any
  current_sub = @parent_task.sub_tasks.where(status: %w[running queued pending]).first
  current_sub&.update!(status: "cancelled")

  @state["current_creative_id"] = nil
  @parent_task.update!(
    status: "cancelled",
    workflow_state: @state
  )

  post_notice(
    I18n.t("collavre.comments.work_command.workflow_stopped",
           agent: @parent_task.agent.display_name,
           completed: (@state["completed_creative_ids"] || []).size,
           remaining: (@state["pending_creative_ids"] || []).size)
  )
end