Skip to content

Commit

Permalink
refactor: adjust background service log
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 25, 2024
1 parent 918afd3 commit b908d07
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/proxy/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ impl ServiceTask for HealthCheckTask {
None
}
fn description(&self) -> String {
"UpstreamHc".to_string()
let count = UPSTREAM_MAP.load().len();
format!("upstream health check, upstream count: {count}")
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/service/auto_restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,9 @@ impl ServiceTask for AutoRestart {
}
fn description(&self) -> String {
if self.running_hot_reload.load(Ordering::Relaxed) {
"HotReloadDetector".to_string()
"hot reload detector".to_string()
} else {
"RestartDetector".to_string()
"restart detector".to_string()
}
}
}
12 changes: 4 additions & 8 deletions src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::time::{Duration, SystemTime};
use tokio::time::interval;
use tracing::{error, info};

pub static LOG_CATEGORY: &str = "service";
pub static LOG_CATEGORY: &str = "backgroundService";

pub type SimpleServiceTaskFuture =
Box<dyn Fn(u32) -> BoxFuture<'static, Result<bool, String>> + Sync + Send>;
Expand Down Expand Up @@ -126,16 +126,13 @@ pub trait ServiceTask: Sync + Send {
pub struct CommonServiceTask {
task: Box<dyn ServiceTask>,
interval: Duration,
name: String,
}

impl CommonServiceTask {
pub fn new(interval: Duration, task: impl ServiceTask + 'static) -> Self {
let name = task.description();
Self {
task: Box::new(task),
interval,
name,
}
}
}
Expand All @@ -149,7 +146,7 @@ impl BackgroundService for CommonServiceTask {
let once = self.interval.as_millis() < 1000;

info!(
name = self.name,
category = LOG_CATEGORY,
description = self.task.description(),
interval = period_human.to_string(),
"background service is running",
Expand All @@ -164,15 +161,14 @@ impl BackgroundService for CommonServiceTask {
_ = period.tick() => {
let now = SystemTime::now();
let done = self.task.run().await.unwrap_or_default();
let description = self.task.description();
info!(
name = self.name,
category = LOG_CATEGORY,
done,
elapsed = format!(
"{}ms",
now.elapsed().unwrap_or_default().as_millis()
),
description,
description = self.task.description(),
);
if once || done {
break;
Expand Down
4 changes: 1 addition & 3 deletions src/state/prom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use prometheus::{
};
use std::sync::Arc;
use std::time::Duration;
use tracing::{error, info};
use tracing::error;
use url::Url;

static HOST_NAME_TAG: &str = "$HOSTNAME";
Expand Down Expand Up @@ -305,8 +305,6 @@ async fn do_push(
status = res.status().to_string(),
"push prometheus fail"
);
} else {
info!(name = params.name, "push prometheus success");
}
},
Err(e) => {
Expand Down

0 comments on commit b908d07

Please sign in to comment.