Files
asepharyana-hub-tools/backend/gateway/src/nats/publisher.rs
T
asepharyanaandKilo d2bfcb83a0 fix: audit perbaikan NATS subject, worker spawn, frontend options, pipeline PDF output
Bug fixes:
1. NATS subject doubled prefix — gateway publish ke tools.tools.scan.*
   padahal workers subscribe ke tools.scan.*. Fix: pakai group() method
2. Worker subscriptions pake tokio::select! — cuma satu subscription
   yang diproses, sisanya di-cancel. Fix: ganti ke tokio::spawn + loop
3. WebSocket URL hardcoded ke localhost:3001 — gak jalan di prod.
   Fix: infer dari window.location atau env var
4. Frontend scan options hardcoded — user gak bisa atur OCR/enhance/format.
   Fix: interactive toggles, selects, slider
5. Frontend compress quality hardcoded — user gak bisa atur kualitas.
   Fix: quality range slider
6. Pipeline cuma output PNG — gak ada PDF/OCR.
   Fix: generate searchable PDF kalo tesseract feature enabled

Enhancements:
- Tool::group() method added — returns 'scan', 'image', 'pdf' dll
- Dockerfile: --features tesseract pas cargo build
- leptess OCR: proper API usage (set_image_from_mem, recognize, etc.)

Co-Authored-By: Kilo <kilo@kilo.ai>
2026-07-24 18:24:22 +07:00

64 lines
2.0 KiB
Rust

use async_nats::Client;
use tools_common::error::NatsError;
use tools_common::nats;
use tools_common::types::{Job, JobProgress, Tool};
/// NATS publisher for job and progress messages.
pub struct NatsPublisher;
impl NatsPublisher {
/// Connect to NATS server.
pub async fn connect(url: &str) -> Result<Client, NatsError> {
async_nats::connect(url)
.await
.map_err(|e| NatsError::Connection(e.to_string()))
}
/// Publish a job to the appropriate NATS subject.
pub async fn publish_job(nats: &Client, tool: &Tool, job: &Job) -> Result<(), NatsError> {
let group = tool.group();
let subject = nats::job_subject(group, &job.id.to_string());
let payload = serde_json::to_vec(job)
.map_err(|e| NatsError::Publish(e.to_string()))?;
nats.publish(subject, payload.into())
.await
.map_err(|e| NatsError::Publish(e.to_string()))?;
tracing::debug!(
job_id = %job.id,
tool = %tool.as_str(),
"Published job to NATS"
);
Ok(())
}
/// Publish a progress update to the NATS progress subject.
pub async fn publish_progress(
nats: &Client,
progress: &JobProgress,
) -> Result<(), NatsError> {
let tool_prefix = ""; // We need the tool from somewhere — stored in progress
let subject = format!("tools.*.progress.{}", progress.job_id);
let payload = serde_json::to_vec(progress)
.map_err(|e| NatsError::Publish(e.to_string()))?;
nats.publish(subject, payload.into())
.await
.map_err(|e| NatsError::Publish(e.to_string()))?;
Ok(())
}
/// Subscribe to NATS progress updates for a specific job.
pub async fn subscribe_progress(
nats: &Client,
job_id: &str,
) -> Result<async_nats::Subscriber, NatsError> {
let subject = format!("tools.*.progress.{}", job_id);
nats.subscribe(subject)
.await
.map_err(|e| NatsError::Subscribe(e.to_string()))
}
}