refactor(service): replace tracing with slog in agent sync module
Some checks are pending
CI / Frontend Lint & Type Check (push) Waiting to run
CI / Frontend Build (push) Blocked by required conditions
CI / Rust Lint & Check (push) Waiting to run
CI / Rust Tests (push) Waiting to run

All log calls in sync.rs now use slog macros:
- sync_once: uses logger passed as parameter (sourced from AppService.logs)
- sync_upstream_models (HTTP API): uses self.logs
- Remove use of tracing::warn/info/error entirely

The periodic background sync task and the HTTP API handler now
write to the same slog logger as the rest of the application.
This commit is contained in:
ZhenYi 2026-04-16 22:52:03 +08:00
parent 3a30150a41
commit 39d126d843

View File

@ -14,6 +14,7 @@
use std::time::Duration; use std::time::Duration;
use tokio::time::interval; use tokio::time::interval;
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use slog::Logger;
use crate::AppService; use crate::AppService;
use crate::error::AppError; use crate::error::AppError;
@ -506,7 +507,7 @@ impl AppService {
match upsert_version(&self.db, model_record.id).await { match upsert_version(&self.db, model_record.id).await {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
tracing::warn!("sync_upstream_models: upsert_version error: {:?}", e); slog::warn!(self.logs, "{}", format!("sync_upstream_models: upsert_version error: {:?}", e));
continue; continue;
} }
}; };
@ -515,7 +516,7 @@ impl AppService {
} }
if let Err(e) = upsert_pricing(&self.db, version_record.id, or_model.pricing.as_ref()).await { if let Err(e) = upsert_pricing(&self.db, version_record.id, or_model.pricing.as_ref()).await {
tracing::warn!("sync_upstream_models: upsert_pricing error: {:?}", e); slog::warn!(self.logs, "{}", format!("sync_upstream_models: upsert_pricing error: {:?}", e));
} else { } else {
pricing_created += 1; pricing_created += 1;
} }
@ -546,22 +547,23 @@ impl AppService {
/// Failures are logged but do not stop the task — it keeps retrying. /// Failures are logged but do not stop the task — it keeps retrying.
pub fn start_sync_task(self) -> JoinHandle<()> { pub fn start_sync_task(self) -> JoinHandle<()> {
let db = self.db.clone(); let db = self.db.clone();
let log = self.logs.clone();
tokio::spawn(async move { tokio::spawn(async move {
// Run once immediately on startup before taking traffic. // Run once immediately on startup before taking traffic.
Self::sync_once(&db).await; Self::sync_once(&db, &log).await;
let mut tick = interval(Duration::from_secs(60 * 10)); let mut tick = interval(Duration::from_secs(60 * 10));
loop { loop {
tick.tick().await; tick.tick().await;
Self::sync_once(&db).await; Self::sync_once(&db, &log).await;
} }
}) })
} }
/// Perform a single sync pass. Errors are logged and silently swallowed /// Perform a single sync pass. Errors are logged and silently swallowed
/// so the periodic task never stops. /// so the periodic task never stops.
async fn sync_once(db: &AppDatabase) { async fn sync_once(db: &AppDatabase, log: &Logger) {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let resp = match client let resp = match client
.get("https://openrouter.ai/api/v1/models") .get("https://openrouter.ai/api/v1/models")
@ -572,17 +574,17 @@ impl AppService {
Ok(resp) => match resp.json::<OpenRouterResponse>().await { Ok(resp) => match resp.json::<OpenRouterResponse>().await {
Ok(resp) => resp, Ok(resp) => resp,
Err(e) => { Err(e) => {
tracing::error!("OpenRouter model sync: failed to parse response: {}", e); slog::error!(log, "{}", format!("OpenRouter model sync: failed to parse response: {}", e));
return; return;
} }
}, },
Err(e) => { Err(e) => {
tracing::error!("OpenRouter model sync: API error: {}", e); slog::error!(log, "{}", format!("OpenRouter model sync: API error: {}", e));
return; return;
} }
}, },
Err(e) => { Err(e) => {
tracing::error!("OpenRouter model sync: request failed: {}", e); slog::error!(log, "{}", format!("OpenRouter model sync: request failed: {}", e));
return; return;
} }
}; };
@ -603,7 +605,7 @@ impl AppService {
let provider = match upsert_provider(db, provider_slug).await { let provider = match upsert_provider(db, provider_slug).await {
Ok(p) => p, Ok(p) => p,
Err(e) => { Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_provider error: {:?}", e); slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_provider error: {:?}", e));
continue; continue;
} }
}; };
@ -618,7 +620,7 @@ impl AppService {
m m
} }
Err(e) => { Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_model error: {:?}", e); slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_model error: {:?}", e));
continue; continue;
} }
}; };
@ -626,7 +628,7 @@ impl AppService {
let (version_record, version_is_new) = match upsert_version(db, model_record.id).await { let (version_record, version_is_new) = match upsert_version(db, model_record.id).await {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_version error: {:?}", e); slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_version error: {:?}", e));
continue; continue;
} }
}; };
@ -654,7 +656,8 @@ impl AppService {
} }
} }
tracing::info!( slog::info!(log, "{}",
format!(
"OpenRouter model sync complete: created={} updated={} \ "OpenRouter model sync complete: created={} updated={} \
versions={} pricing={} capabilities={} profiles={}", versions={} pricing={} capabilities={} profiles={}",
models_created, models_created,
@ -663,6 +666,7 @@ impl AppService {
pricing_created, pricing_created,
capabilities_created, capabilities_created,
profiles_created profiles_created
)
); );
} }
} }