Skip to content

Commit

Permalink
monitor: make profiles mutable at runtime (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Jan 2, 2025
1 parent 8d0c960 commit 326cd78
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
4 changes: 2 additions & 2 deletions monitor/src/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ pub struct Dashboard {
#[derive(Clone, Debug, Template)]
#[template(path = "dashboard.html")]
struct DashboardTemplate<'monitor> {
profile_runner_counts: &'monitor BTreeMap<&'monitor str, RunnerCounts>,
profile_runner_counts: &'monitor BTreeMap<String, RunnerCounts>,
runners: &'monitor Runners,
}

impl Dashboard {
pub fn render(
profile_runner_counts: &BTreeMap<&str, RunnerCounts>,
profile_runner_counts: &BTreeMap<String, RunnerCounts>,
runners: &Runners,
) -> eyre::Result<Self> {
let json = serde_json::to_string(&json!({
Expand Down
19 changes: 10 additions & 9 deletions monitor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{
path::Path,
process::exit,
sync::{LazyLock, RwLock},
thread,
thread::{self},
time::{Duration, UNIX_EPOCH},
};

Expand Down Expand Up @@ -372,6 +372,7 @@ fn monitor_thread() -> eyre::Result<()> {
IdGen::new_empty()
});

let /* mut */ profiles = TOML.initial_profiles();
let mut registrations_cache = Cache::default();

loop {
Expand All @@ -387,9 +388,9 @@ fn monitor_thread() -> eyre::Result<()> {
);

let runners = Runners::new(registrations, guests, volumes);
let profile_runner_counts: BTreeMap<_, _> = TOML
.profiles()
.map(|(key, profile)| (key, profile.runner_counts(&runners)))
let profile_runner_counts: BTreeMap<_, _> = profiles
.iter()
.map(|(key, profile)| (key.clone(), profile.runner_counts(&runners)))
.collect();
for (
key,
Expand Down Expand Up @@ -417,7 +418,7 @@ fn monitor_thread() -> eyre::Result<()> {
warn!(?error, "Failed to unregister runner: {error}");
}
}
if let Some(profile) = TOML.profile(runner.base_vm_name()) {
if let Some(profile) = profiles.get(runner.base_vm_name()) {
if let Err(error) = profile.destroy_runner(id) {
warn!(?error, "Failed to destroy runner: {error}");
}
Expand Down Expand Up @@ -460,7 +461,7 @@ fn monitor_thread() -> eyre::Result<()> {
.flatten()
.map_or(true, |duration| duration > DOTENV.monitor_reserve_timeout)
});
let excess_idle_runners = TOML.profiles().flat_map(|(_key, profile)| {
let excess_idle_runners = profiles.iter().flat_map(|(_key, profile)| {
profile
.idle_runners(&runners)
.take(profile.excess_idle_runner_count(&runners))
Expand All @@ -474,8 +475,8 @@ fn monitor_thread() -> eyre::Result<()> {
unregister_and_destroy(id, runner);
}

let profile_wanted_counts = TOML
.profiles()
let profile_wanted_counts = profiles
.iter()
.map(|(_key, profile)| (profile, profile.wanted_runner_count(&runners)));
for (profile, wanted_count) in profile_wanted_counts {
for _ in 0..wanted_count {
Expand All @@ -491,7 +492,7 @@ fn monitor_thread() -> eyre::Result<()> {
if let Ok(mut dashboard) = DASHBOARD.write() {
*dashboard = Some(Dashboard::render(&profile_runner_counts, &runners)?);
runners.update_screenshots();
for (_key, profile) in TOML.profiles() {
for (_key, profile) in profiles.iter() {
profile.update_screenshot();
}
}
Expand Down
2 changes: 1 addition & 1 deletion monitor/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
DOTENV,
};

#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
pub struct Profile {
pub configuration_name: String,
pub base_vm_name: String,
Expand Down
10 changes: 3 additions & 7 deletions monitor/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Dotenv {
#[derive(Deserialize)]
pub struct Toml {
pub external_base_url: String,
pub profiles: BTreeMap<String, Profile>,
profiles: BTreeMap<String, Profile>,
}

impl Dotenv {
Expand Down Expand Up @@ -89,12 +89,8 @@ impl Toml {
Ok(result)
}

pub fn profiles(&self) -> impl Iterator<Item = (&str, &Profile)> {
self.profiles.iter().map(|(k, v)| (k.as_str(), v))
}

pub fn profile(&self, key: impl AsRef<str>) -> Option<&Profile> {
self.profiles.get(key.as_ref())
pub fn initial_profiles(&self) -> BTreeMap<String, Profile> {
self.profiles.clone()
}
}

Expand Down

0 comments on commit 326cd78

Please sign in to comment.