Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Server Manager - Next-Gen Media Server Orchestrator 🚀

![Server Manager Banner](https://img.shields.io/badge/Status-Tested-brightgreen) ![Version](https://img.shields.io/badge/Version-1.0.7-blue) ![Rust](https://img.shields.io/badge/Built%20With-Rust-orange) ![Docker](https://img.shields.io/badge/Powered%20By-Docker-blue)
![Server Manager Banner](https://img.shields.io/badge/Status-Tested-brightgreen) ![Version](https://img.shields.io/badge/Version-1.0.8-blue) ![Rust](https://img.shields.io/badge/Built%20With-Rust-orange) ![Docker](https://img.shields.io/badge/Powered%20By-Docker-blue)

**Server Manager** is a powerful and intelligent tool written in Rust to deploy, manage, and optimize a complete personal media and cloud server stack. It detects your hardware and automatically configures 28 Docker services for optimal performance.

Expand Down
2 changes: 1 addition & 1 deletion server_manager/Cargo.lock

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

2 changes: 1 addition & 1 deletion server_manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "server_manager"
version = "1.0.7"
version = "1.0.8"
edition = "2021"

[dependencies]
Expand Down
87 changes: 78 additions & 9 deletions server_manager/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::collections::HashSet;
use std::fs;
use std::path::Path;
use std::sync::OnceLock;
use std::time::SystemTime;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;

#[derive(Debug, Clone)]
struct CachedConfig {
config: Config,
last_mtime: Option<SystemTime>,
last_check: Option<SystemTime>,
}

static CONFIG_CACHE: OnceLock<RwLock<CachedConfig>> = OnceLock::new();
Expand Down Expand Up @@ -42,19 +43,17 @@ impl Config {
RwLock::new(CachedConfig {
config: Config::default(),
last_mtime: None,
last_check: None,
})
});

// Fast path: Optimistic read
// Fast path: Optimistic read with throttle
{
let guard = cache.read().await;
if let Some(cached_mtime) = guard.last_mtime {
// Check if file still matches
if let Ok(metadata) = tokio::fs::metadata("config.yaml").await {
if let Ok(modified) = metadata.modified() {
if modified == cached_mtime {
return Ok(guard.config.clone());
}
if let Some(last_check) = guard.last_check {
if let Ok(elapsed) = SystemTime::now().duration_since(last_check) {
if elapsed < Duration::from_millis(500) {
return Ok(guard.config.clone());
}
}
}
Expand All @@ -63,8 +62,18 @@ impl Config {
// Slow path: Update cache
let mut guard = cache.write().await;

// Check throttle again under write lock
if let Some(last_check) = guard.last_check {
if let Ok(elapsed) = SystemTime::now().duration_since(last_check) {
if elapsed < Duration::from_millis(500) {
return Ok(guard.config.clone());
}
}
}

// Check metadata again (double-checked locking pattern)
let metadata_res = tokio::fs::metadata("config.yaml").await;
guard.last_check = Some(SystemTime::now());

match metadata_res {
Ok(metadata) => {
Expand Down Expand Up @@ -124,4 +133,64 @@ impl Config {
info!("Disabled service: {}", service_name);
}
}

pub async fn enable_service_async(service_name: &str) -> Result<()> {
Self::update_service_async(service_name, true).await
}

pub async fn disable_service_async(service_name: &str) -> Result<()> {
Self::update_service_async(service_name, false).await
}

async fn update_service_async(service_name: &str, enable: bool) -> Result<()> {
let cache = CONFIG_CACHE.get_or_init(|| {
RwLock::new(CachedConfig {
config: Config::default(),
last_mtime: None,
last_check: None,
})
});

let mut guard = cache.write().await;

// Sync with disk first
let metadata_res = tokio::fs::metadata("config.yaml").await;
if let Ok(metadata) = metadata_res {
let modified = metadata.modified().unwrap_or(SystemTime::now());
let reload = match guard.last_mtime {
Some(cached) => modified != cached,
None => true,
};

if reload {
if let Ok(content) = tokio::fs::read_to_string("config.yaml").await {
if !content.trim().is_empty() {
if let Ok(cfg) = serde_yaml_ng::from_str::<Config>(&content) {
guard.config = cfg;
}
}
}
}
}

// Modify
if enable {
guard.config.enable_service(service_name);
} else {
guard.config.disable_service(service_name);
}

// Save (Async)
let content = serde_yaml_ng::to_string(&guard.config)?;
tokio::fs::write("config.yaml", content)
.await
.context("Failed to write config.yaml")?;

// Update mtime
if let Ok(metadata) = tokio::fs::metadata("config.yaml").await {
guard.last_mtime = metadata.modified().ok();
}

Ok(())
}
}
1 change: 1 addition & 0 deletions server_manager/src/core/hardware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl HardwareInfo {
sys.refresh_memory();
sys.refresh_cpu();
sys.refresh_disks_list();
sys.refresh_disks();

let total_memory = sys.total_memory(); // Bytes
let ram_gb = total_memory / 1024 / 1024 / 1024;
Expand Down
Loading