Skip to content

Commit

Permalink
feat: added daemon mode
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan committed Oct 7, 2024
1 parent 3c6dc3b commit 354e3df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/bin/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,30 @@ async fn main() -> Result<()> {

let config = Config::new()?;

let schedule = fabric::drivers::usage::schedule(config.clone().into());
let subscribe = fabric::drivers::monitor::subscribe(config.clone().into());
match config.mode {
Some(Mode::Usage) => {
fabric::drivers::usage::schedule(config.clone().into()).await?;
}
Some(Mode::Monitor) => {
fabric::drivers::monitor::subscribe(config.clone().into()).await?;
}
None => {
let schedule = fabric::drivers::usage::schedule(config.clone().into());
let subscribe = fabric::drivers::monitor::subscribe(config.clone().into());

try_join!(schedule, subscribe)?;
try_join!(schedule, subscribe)?;
}
};

Ok(())
}

#[derive(Debug, Deserialize, Clone)]
enum Mode {
Usage,
Monitor,
}

#[derive(Debug, Deserialize, Clone)]
struct Prometheus {
url: String,
Expand All @@ -47,6 +63,7 @@ struct Config {
delay: Duration,
topic: String,
kafka: HashMap<String, String>,
mode: Option<Mode>,
}
impl Config {
pub fn new() -> Result<Self> {
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn subscribe(config: MonitorConfig) -> Result<()> {
let consumer: StreamConsumer = client_config.create()?;
consumer.subscribe(&[&config.topic])?;

info!("Subscriber running");
info!("Monitor subscribe running");
loop {
match consumer.recv().await {
Err(error) => error!(?error, "kafka subscribe error"),
Expand Down
1 change: 1 addition & 0 deletions src/drivers/usage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub async fn schedule(config: UsageConfig) -> Result<()> {

let mut cursor = Utc::now();

info!("Usage schedule running");
loop {
sleep(config.delay).await;

Expand Down

0 comments on commit 354e3df

Please sign in to comment.