Initial boilerplate scaffold for continuum-ai-worker
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
mod postgres;
|
||||
mod redis_pub;
|
||||
|
||||
use crate::inference::Detection;
|
||||
use sqlx::PgPool;
|
||||
use tracing::info;
|
||||
|
||||
pub struct Reporter {
|
||||
redis: redis::aio::MultiplexedConnection,
|
||||
pg: PgPool,
|
||||
}
|
||||
|
||||
impl Reporter {
|
||||
pub async fn connect(redis_url: &str, database_url: &str) -> anyhow::Result<Self> {
|
||||
let redis_client = redis::Client::open(redis_url)?;
|
||||
let redis = redis_client.get_multiplexed_tokio_connection().await?;
|
||||
let pg = PgPool::connect(database_url).await?;
|
||||
|
||||
Ok(Self { redis, pg })
|
||||
}
|
||||
|
||||
/// Fast-paths a failure alert: publishes to Redis immediately for any
|
||||
/// live UI subscribers, then persists to Postgres for the audit trail
|
||||
/// and downstream farm-manager notifications.
|
||||
pub async fn report_failure(
|
||||
&mut self,
|
||||
printer_id: &str,
|
||||
job_id: Option<&str>,
|
||||
detections: &[Detection],
|
||||
) -> anyhow::Result<()> {
|
||||
let best = detections
|
||||
.iter()
|
||||
.max_by(|a, b| a.confidence.total_cmp(&b.confidence))
|
||||
.expect("report_failure called with no detections");
|
||||
|
||||
info!(printer_id, label = %best.label, confidence = best.confidence, "reporting print failure");
|
||||
|
||||
redis_pub::publish_alert(&mut self.redis, printer_id, best).await?;
|
||||
postgres::insert_alert(&self.pg, printer_id, job_id, detections).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user