Listen for responses of supervisors

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2025-09-04 16:24:15 +02:00
parent c6077623b0
commit 059d5131e7
6 changed files with 423 additions and 8 deletions

View File

@@ -1,5 +1,7 @@
use std::{collections::HashSet, sync::Arc};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
use serde_json::{Value, json};
use tokio::sync::Semaphore;
@@ -151,21 +153,25 @@ async fn deliver_one(
// Build supervisor method and params from Message
let method = msg.message.clone();
let params = build_params(&msg)?;
// Send
// If this is a job.run and we have a secret configured on the client,
// prefer the typed wrapper that injects the secret into inner supervisor params.
let out_id = if method == "job.run" {
// prefer the typed wrapper that injects the secret into inner supervisor params,
// and also capture the inner supervisor JSON-RPC id for correlation.
let (out_id, inner_id_opt) = if method == "job.run" {
if let Some(j) = msg.job.first() {
let jv = job_to_json(j)?;
// This uses SupervisorClient::job_run, which sets {"secret": "...", "job": <job>}
client.job_run(jv).await?
// Returns (outbound message id, inner supervisor JSON-RPC id)
let (out, inner) = client.job_run_with_ids(jv).await?;
(out, Some(inner))
} else {
// Fallback: no embedded job, use the generic call
client.call(&method, params).await?
let out = client.call(&method, params).await?;
(out, None)
}
} else {
client.call(&method, params).await?
let out = client.call(&method, params).await?;
(out, None)
};
// Store transport id and initial Sent status
@@ -184,6 +190,13 @@ async fn deliver_one(
.update_message_status(context_id, caller_id, id, MessageStatus::Acknowledged)
.await?;
// Record correlation (inner supervisor JSON-RPC id -> job/message) for inbound popMessage handling
if let (Some(inner_id), Some(job_id)) = (inner_id_opt, job_id_opt) {
let _ = service
.supcorr_set(inner_id, context_id, caller_id, job_id, id)
.await;
}
// Spawn transport-status poller
{
let service_poll = service.clone();
@@ -487,3 +500,156 @@ pub fn start_router_auto(service: AppService, cfg: RouterConfig) -> tokio::task:
}
})
}
/// Start a single global inbound listener that reads Mycelium popMessage with topic filter,
/// decodes supervisor JSON-RPC replies, and updates correlated jobs/messages.
/// This listens for async replies like {"result":{"job_queued":...}} carrying the same inner JSON-RPC id.
pub fn start_inbound_listener(
service: AppService,
cfg: RouterConfig,
) -> tokio::task::JoinHandle<()> {
tokio::spawn(async move {
// Initialize Mycelium client (retry loop)
let mycelium = loop {
match MyceliumClient::new(cfg.base_url.clone()) {
Ok(c) => break c,
Err(e) => {
error!(error=%e, "MyceliumClient init error (inbound listener)");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
};
loop {
// Poll for inbound supervisor messages on the configured topic
match mycelium
.pop_message(Some(false), Some(20), Some(cfg.topic.as_str()))
.await
{
Ok(Some(inb)) => {
// Expect InboundMessage with base64 "payload"
let Some(payload_b64) = inb.get("payload").and_then(|v| v.as_str()) else {
// Not a payload-bearing message; ignore
continue;
};
let Ok(raw) = BASE64_STANDARD.decode(payload_b64.as_bytes()) else {
let _ = service
.append_message_logs(
0, // unknown context yet
0,
0,
vec![
"Inbound payload base64 decode error (supervisor reply)".into(),
],
)
.await;
continue;
};
let Ok(rpc): Result<Value, _> = serde_json::from_slice(&raw) else {
// Invalid JSON payload
continue;
};
// Extract inner supervisor JSON-RPC id (number preferred; string fallback)
let inner_id_u64 = match rpc.get("id") {
Some(Value::Number(n)) => n.as_u64(),
Some(Value::String(s)) => s.parse::<u64>().ok(),
_ => None,
};
let Some(inner_id) = inner_id_u64 else {
// Cannot correlate without id
continue;
};
// Lookup correlation mapping
match service.supcorr_get(inner_id).await {
Ok(Some((context_id, caller_id, job_id, message_id))) => {
// Determine success/error from supervisor JSON-RPC envelope
let is_success = rpc
.get("result")
.map(|res| {
res.get("job_queued").is_some()
|| res.as_str().map(|s| s == "job_queued").unwrap_or(false)
})
.unwrap_or(false);
if is_success {
// Set to Dispatched (idempotent) per spec choice, and append log
let _ = service
.update_job_status_unchecked(
context_id,
caller_id,
job_id,
JobStatus::Dispatched,
)
.await;
let _ = service
.append_message_logs(
context_id,
caller_id,
message_id,
vec![format!(
"Supervisor reply for job {}: job_queued",
job_id
)],
)
.await;
let _ = service.supcorr_del(inner_id).await;
} else if let Some(err_obj) = rpc.get("error") {
// Error path: set job Error and log details
let _ = service
.update_job_status_unchecked(
context_id,
caller_id,
job_id,
JobStatus::Error,
)
.await;
let _ = service
.append_message_logs(
context_id,
caller_id,
message_id,
vec![format!(
"Supervisor error for job {}: {}",
job_id, err_obj
)],
)
.await;
let _ = service.supcorr_del(inner_id).await;
} else {
// Unknown result; keep correlation for a later, clearer reply
let _ = service
.append_message_logs(
context_id,
caller_id,
message_id,
vec![
"Supervisor reply did not contain job_queued or error"
.to_string(),
],
)
.await;
}
}
Ok(None) => {
// No correlation found; ignore or log once
}
Err(e) => {
error!(error=%e, "supcorr_get error");
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
}
}
}
Ok(None) => {
// No message; continue polling
continue;
}
Err(e) => {
error!(error=%e, "popMessage error");
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
}
}
}
})
}