Class: RcrewAI::Rails::CrewExecutionJob

Inherits:
ActiveJob::Base
  • Object
show all
Defined in:
app/jobs/rcrewai/rails/crew_execution_job.rb

Instance Method Summary collapse

Instance Method Details

#perform(crew, inputs = {}, batch_id: nil) ⇒ Object



8
9
10
11
12
13
14
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
# File 'app/jobs/rcrewai/rails/crew_execution_job.rb', line 8

def perform(crew, inputs = {}, batch_id: nil)
  execution = crew.executions.create!(
    status: "pending",
    inputs: inputs,
    batch_id: batch_id
  )
  collector = nil

  begin
    execution.start!
    execution.log("info", "Starting crew execution", { crew_id: crew.id, inputs: inputs })

    rcrew = crew.to_rcrew

    collector = collector_for(execution)
    collector&.start_crew_span(crew_name: crew.name)

    result = rcrew.execute(stream: collector)

    # Close agent spans as successful before finish!, which treats
    # anything still open as an aborted run.
    collector&.finish_open_agent_spans(status: "ok")
    collector&.finish_crew_span(status: "ok")
    collector&.finish!

    execution.complete!(result)
    execution.log("info", "Crew execution completed", { result: result })

    notify_completion(crew, execution, result)

    result
  rescue => e
    collector&.finish!
    execution.fail!(e)
    execution.log("error", "Crew execution failed", {
      error: e.message,
      backtrace: e.backtrace&.first(5)
    })

    raise
  end
end