Class: Protege::ResponsibilityJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/protege/responsibility_job.rb

Overview

Carries out one scheduled responsibility run — the Loop layer's worker (the "L" of LOGI driving the "I"). Enqueued by Responsibility#dispatch! with the id of a pending ResponsibilityRun; it advances that run to running, runs the persona through ResponsibilityHarness, and records the outcome on the run. The run id (not the object) is passed across the async boundary so the queue adapter can serialize the job.

There is no inbound request to inherit a correlation id from, so the job mints a fresh UUID and restores it into Protege::Current at the top of perform; the run snapshots it onto its own correlation_id when it starts, tying the run record to the events it emits. Unlike the inbound jobs, a failure is recorded but not re-raised: a responsibility is cron-driven, so the next scheduled minute is its natural retry — letting ActiveJob rerun it would double-fire. (The harness still emits inference_failed, so failure hooks observe it.)

Instance Method Summary collapse

Instance Method Details

#perform(run_id:) ⇒ void

This method returns an undefined value.

Carry out one scheduled run: advance the run, run inference, record the result.

Parameters:

  • run_id (Integer)

    id of the pending ResponsibilityRun to carry out



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/jobs/protege/responsibility_job.rb', line 23

def perform(run_id:)
  responsibility_run = ResponsibilityRun.eager_load(responsibility: :persona).find(run_id)
  # Adopt the trace id minted at dispatch (Responsibility#dispatch!) so the whole run lifecycle
  # shares it; fall back to a fresh one for a run created some other way (e.g. in a test).
  restore_correlation_id(responsibility_run.correlation_id || SecureRandom.uuid)

  responsibility = responsibility_run.responsibility
  persona        = responsibility.persona

  process(responsibility_run:, responsibility:, persona:)
rescue StandardError => e
  # Record the failure without re-raising — the cron schedule, not ActiveJob, drives the retry.
  responsibility_run&.mark_failed!(e)
  emit_loop_run_failed(responsibility_run, e)
end