Class: Arcp::Runtime::JobManager
- Inherits:
-
Object
- Object
- Arcp::Runtime::JobManager
- Defined in:
- lib/arcp/runtime/job_manager.rb
Overview
Owns agent registry + per-job lifecycle. Submitted jobs run as child ‘Async::Task`s; cancellation propagates via `task.stop`.
Instance Attribute Summary collapse
-
#runtime ⇒ Object
readonly
Returns the value of attribute runtime.
Instance Method Summary collapse
- #agent_inventory ⇒ Object
- #cancel(job_id:, principal_id:, reason: nil) ⇒ Object
-
#initialize(runtime:, lease_manager:, subscription_manager:, event_log:, clock: Arcp::SystemClock.new) ⇒ JobManager
constructor
A new instance of JobManager.
- #list(principal_id:, filter: {}, limit: 50, cursor: nil) ⇒ Object
- #lookup(job_id) ⇒ Object
- #publish_error(job_id, error) ⇒ Object
- #publish_event(job_id, event) ⇒ Object
- #publish_result(job_id, result) ⇒ Object
- #register_agent(name:, versions:, default:, handler:) ⇒ Object
- #resolve_agent(ref_str) ⇒ Object
- #submit(submit:, principal_id:, session_id:, session_actor:) ⇒ Object
Constructor Details
#initialize(runtime:, lease_manager:, subscription_manager:, event_log:, clock: Arcp::SystemClock.new) ⇒ JobManager
Returns a new instance of JobManager.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/arcp/runtime/job_manager.rb', line 21 def initialize(runtime:, lease_manager:, subscription_manager:, event_log:, clock: Arcp::SystemClock.new) @runtime = runtime @leases = lease_manager @subs = subscription_manager @event_log = event_log @clock = clock @agents = {} # name => AgentRegistration @jobs = {} # job_id => JobRecord @event_seq = Hash.new(0) # job_id => last emitted seq @idempotency = {} # [principal, key] => job_id @mutex = Mutex.new end |
Instance Attribute Details
#runtime ⇒ Object (readonly)
Returns the value of attribute runtime.
19 20 21 |
# File 'lib/arcp/runtime/job_manager.rb', line 19 def runtime @runtime end |
Instance Method Details
#agent_inventory ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/arcp/runtime/job_manager.rb', line 43 def agent_inventory @mutex.synchronize do Arcp::Session::AgentInventory.new( entries: @agents.values.map do |reg| Arcp::Session::AgentEntry.new(name: reg.name, versions: reg.versions, default: reg.default) end.freeze ) end end |
#cancel(job_id:, principal_id:, reason: nil) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/arcp/runtime/job_manager.rb', line 114 def cancel(job_id:, principal_id:, reason: nil) record = @mutex.synchronize { @jobs[job_id] } raise Arcp::Errors::JobNotFound, "no such job: #{job_id}" unless record unless record.principal_id == principal_id raise Arcp::Errors::PermissionDenied.new( 'only the submitting principal can cancel a job', details: { 'job_id' => job_id } ) end record.task&.stop publish_error(job_id, Arcp::Job::JobError.new( job_id: job_id, final_status: 'cancelled', code: 'CANCELLED', message: reason, retryable: false, details: {} )) end |
#list(principal_id:, filter: {}, limit: 50, cursor: nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/arcp/runtime/job_manager.rb', line 132 def list(principal_id:, filter: {}, limit: 50, cursor: nil) offset = cursor ? cursor.to_i : 0 rows = @mutex.synchronize do @jobs.values .select { |r| r.principal_id == principal_id } .select { |r| filter['status'].nil? || filter['status'].include?(r.status) } .select { |r| filter['agent'].nil? || r.agent.start_with?(filter['agent']) } .sort_by(&:created_at) end page = rows[offset, limit] || [] next_cursor = (offset + page.size) < rows.size ? (offset + page.size).to_s : nil summaries = page.map do |r| lease = @leases.get(r.job_id) counter = @leases.counter(r.job_id) Arcp::Job::Summary.new( job_id: r.job_id, agent: r.agent, status: r.status, created_at: r.created_at, lease_expires_at: lease&.expires_at, budget_remaining: counter ? counter.snapshot.transform_values { |v| v.to_s('F') } : nil ) end Arcp::Session::JobsResponse.new( jobs: summaries.map(&:to_h), next_cursor: next_cursor ) end |
#lookup(job_id) ⇒ Object
160 |
# File 'lib/arcp/runtime/job_manager.rb', line 160 def lookup(job_id) = @mutex.synchronize { @jobs[job_id] } |
#publish_error(job_id, error) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/arcp/runtime/job_manager.rb', line 191 def publish_error(job_id, error) record = @mutex.synchronize do @jobs[job_id] = @jobs[job_id].with(status: error.final_status) if @jobs[job_id] @jobs[job_id] end env = Arcp::Envelope.build( type: Arcp::MessageTypes::JOB_ERROR, session_id: record&.submitter_session_id || '', job_id: job_id, payload: error.to_h ) @event_log.append(env.session_id, env) @subs.fanout(job_id, env) @subs.clear(job_id) @runtime.credential_registry&.revoke_all(job_id: job_id) @leases.revoke(job_id) end |
#publish_event(job_id, event) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/arcp/runtime/job_manager.rb', line 162 def publish_event(job_id, event) seq = @mutex.synchronize { @event_seq[job_id] += 1 } env = Arcp::Envelope.build( type: Arcp::MessageTypes::JOB_EVENT, session_id: @mutex.synchronize { @jobs[job_id]&.submitter_session_id || '' }, job_id: job_id, event_seq: seq, payload: event.to_h ) @event_log.append(env.session_id, env) @subs.fanout(job_id, env) seq end |
#publish_result(job_id, result) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/arcp/runtime/job_manager.rb', line 174 def publish_result(job_id, result) record = @mutex.synchronize do @jobs[job_id] = @jobs[job_id].with(status: 'succeeded') if @jobs[job_id] @jobs[job_id] end env = Arcp::Envelope.build( type: Arcp::MessageTypes::JOB_RESULT, session_id: record&.submitter_session_id || '', job_id: job_id, payload: result.to_h ) @event_log.append(env.session_id, env) @subs.fanout(job_id, env) @subs.clear(job_id) @runtime.credential_registry&.revoke_all(job_id: job_id) @leases.revoke(job_id) end |
#register_agent(name:, versions:, default:, handler:) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/arcp/runtime/job_manager.rb', line 34 def register_agent(name:, versions:, default:, handler:) @mutex.synchronize do @agents[name] = AgentRegistration.new( name: name, versions: Array(versions).freeze, default: default, handler: handler ) end end |
#resolve_agent(ref_str) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/arcp/runtime/job_manager.rb', line 53 def resolve_agent(ref_str) ref = Arcp::Job::AgentRef.parse(ref_str) reg = @mutex.synchronize { @agents[ref.name] } raise Arcp::Errors::AgentNotAvailable, "agent not registered: #{ref.name}" if reg.nil? version = ref.version || reg.default if version.nil? || (!reg.versions.empty? && !reg.versions.include?(version)) raise Arcp::Errors::AgentVersionNotAvailable.new( "agent #{ref.name} has no version #{version.inspect}", details: { 'agent' => ref.name, 'version' => version, 'available' => reg.versions } ) end [reg, "#{ref.name}@#{version}"] end |
#submit(submit:, principal_id:, session_id:, session_actor:) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/arcp/runtime/job_manager.rb', line 69 def submit(submit:, principal_id:, session_id:, session_actor:) reg, resolved = resolve_agent(submit.agent) if submit.idempotency_key key = [principal_id, submit.idempotency_key] if (existing = @mutex.synchronize { @idempotency[key] }) existing_record = @mutex.synchronize { @jobs[existing] } if existing_record && existing_record.agent != resolved raise Arcp::Errors::DuplicateKey.new( 'idempotency key reused with different agent', details: { 'job_id' => existing } ) end return existing end end job_id = Arcp::Ids.job_id lease = build_lease(submit, job_id) @leases.register(job_id, lease) if lease credentials = issue_credentials( job_id: job_id, lease: lease, agent: resolved, principal_id: principal_id ) record = JobRecord.new( job_id: job_id, agent: resolved, principal_id: principal_id, status: 'pending', created_at: @clock.now.iso8601, input: submit.input, submitter_session_id: session_id, task: nil ) @mutex.synchronize do @jobs[job_id] = record @idempotency[[principal_id, submit.idempotency_key]] = job_id if submit.idempotency_key end @subs.register_owner(job_id, principal_id, session_id, session_actor.outbox) task = Async do |t| run_agent(t, reg, job_id, submit, lease) end @mutex.synchronize { @jobs[job_id] = @jobs[job_id].with(task: task, status: 'running') } [job_id, resolved, lease, credentials] end |