Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(worker/args): add basic command-line args parser #30

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Tucano

An educational service scheduler and load balancer.

_(WIP - improve README.)_

## Running locally

Controller:

```
cargo run -p ctl
```

Worker:

```
cargo run -p worker -- --controller-addr '127.0.0.1:3000'
```
1 change: 1 addition & 0 deletions worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition.workspace = true
workspace = true

[dependencies]
clap.workspace = true
eyre.workspace = true
proto.workspace = true
sysinfo.workspace = true
Expand Down
41 changes: 41 additions & 0 deletions worker/src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{net::SocketAddr, time::Duration};

use clap::{value_parser, Parser};

pub struct WorkerArgs {
/// Controller's address.
pub controller_addr: SocketAddr,

/// Interval at which metrics are pushed to the controller.
pub metrics_report_interval: Duration,
}

impl WorkerArgs {
/// Parses the process arguments and returns a new [`Args`].
///
/// Panics if missing arguments or if arguments are invalid.
pub fn parse() -> Self {
let raw = RawWorkerArgs::parse();
WorkerArgs {
controller_addr: raw.controller_addr,
metrics_report_interval: Duration::from_secs(raw.metrics_report_interval.into()),
}
}
}

#[derive(Parser)]
struct RawWorkerArgs {
/// Controller's HTTP address.
#[arg(short, long)]
controller_addr: SocketAddr,

/// Interval at which metrics are pushed to the controller.
///
/// Time in seconds. Must be greater than 1.
#[arg(
long,
default_value = "5",
value_parser = value_parser!(u32).range(1..)
)]
metrics_report_interval: u32,
}
15 changes: 7 additions & 8 deletions worker/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use eyre::Result;
use tokio::time::{sleep, Duration};
use tokio::time::sleep;

use crate::monitor::collector::MetricsCollector;
use crate::{args::WorkerArgs, monitor::collector::MetricsCollector};

pub mod monitor;

/// `clap` crate report interval placeholder
/// The value 500 is for quick visualization purposes.
const REPORT_INTERVAL_IN_MILLIS: u64 = 500;
mod args;
mod monitor;

#[tokio::main]
async fn main() -> Result<()> {
let args = WorkerArgs::parse();

let mut metrics_report: MetricsCollector = MetricsCollector::new();

loop {
sleep(Duration::from_millis(REPORT_INTERVAL_IN_MILLIS)).await;
sleep(args.metrics_report_interval).await;
let metrics = metrics_report.get_metrics();
println!("{metrics:#?}");
}
Expand Down