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 tokio::time::interval;
use tokio::task::JoinHandle;
use slog::Logger;
use crate::AppService;
use crate::error::AppError;
@ -506,7 +507,7 @@ impl AppService {
match upsert_version(&self.db, model_record.id).await {
Ok(v) => v,
Err(e) => {
tracing::warn!("sync_upstream_models: upsert_version error: {:?}", e);
slog::warn!(self.logs, "{}", format!("sync_upstream_models: upsert_version error: {:?}", e));
continue;
}
};
@ -515,7 +516,7 @@ impl AppService {
}
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 {
pricing_created += 1;
}
@ -546,22 +547,23 @@ impl AppService {
/// Failures are logged but do not stop the task — it keeps retrying.
pub fn start_sync_task(self) -> JoinHandle<()> {
let db = self.db.clone();
let log = self.logs.clone();
tokio::spawn(async move {
// 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));
loop {
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
/// 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 resp = match client
.get("https://openrouter.ai/api/v1/models")
@ -572,17 +574,17 @@ impl AppService {
Ok(resp) => match resp.json::<OpenRouterResponse>().await {
Ok(resp) => resp,
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;
}
},
Err(e) => {
tracing::error!("OpenRouter model sync: API error: {}", e);
slog::error!(log, "{}", format!("OpenRouter model sync: API error: {}", e));
return;
}
},
Err(e) => {
tracing::error!("OpenRouter model sync: request failed: {}", e);
slog::error!(log, "{}", format!("OpenRouter model sync: request failed: {}", e));
return;
}
};
@ -603,7 +605,7 @@ impl AppService {
let provider = match upsert_provider(db, provider_slug).await {
Ok(p) => p,
Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_provider error: {:?}", e);
slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_provider error: {:?}", e));
continue;
}
};
@ -618,7 +620,7 @@ impl AppService {
m
}
Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_model error: {:?}", e);
slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_model error: {:?}", e));
continue;
}
};
@ -626,7 +628,7 @@ impl AppService {
let (version_record, version_is_new) = match upsert_version(db, model_record.id).await {
Ok(v) => v,
Err(e) => {
tracing::warn!("OpenRouter model sync: upsert_version error: {:?}", e);
slog::warn!(log, "{}", format!("OpenRouter model sync: upsert_version error: {:?}", e));
continue;
}
};
@ -654,15 +656,17 @@ impl AppService {
}
}
tracing::info!(
"OpenRouter model sync complete: created={} updated={} \
versions={} pricing={} capabilities={} profiles={}",
models_created,
models_updated,
versions_created,
pricing_created,
capabilities_created,
profiles_created
slog::info!(log, "{}",
format!(
"OpenRouter model sync complete: created={} updated={} \
versions={} pricing={} capabilities={} profiles={}",
models_created,
models_updated,
versions_created,
pricing_created,
capabilities_created,
profiles_created
)
);
}
}