Skip to content

Commit

Permalink
Implementing get_cpu_temps
Browse files Browse the repository at this point in the history
  • Loading branch information
elielnfinic committed Apr 22, 2024
1 parent c6e76f3 commit ce0537d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ leveldb-sys = "2.0.9"
async-trait = "0.1.77"
async-stream = "0.3.5"
sha3 = "0.10.8"
systemstat = "0.2.3"

[dev-dependencies]
test-strategy = "0.3"
Expand Down
27 changes: 27 additions & 0 deletions src/rpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::collections::HashMap;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::str::FromStr;
use systemstat::{Platform, System};
use tarpc::context;
use tokio::sync::mpsc::error::SendError;
use tracing::{error, info};
Expand Down Expand Up @@ -196,6 +197,9 @@ pub trait RPC {

/// Gracious shutdown.
async fn shutdown() -> bool;

/// Get CPU temperature.
async fn get_cpu_temps() -> Option<f64>;
}

#[derive(Clone)]
Expand Down Expand Up @@ -718,6 +722,14 @@ impl RPC for NeptuneRPCServer {
.get_all_own_coins_with_possible_timelocks()
.await
}

async fn get_cpu_temps(self, _context: tarpc::context::Context) -> Option<f64> {
let current_system = System::new();
match current_system.cpu_temp() {
Ok(temp) => Some(temp.into()),
Err(_) => None,
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1094,4 +1106,19 @@ mod rpc_server_tests {

Ok(())
}

#[allow(clippy::shadow_unrelated)]
#[traced_test]
#[tokio::test]
async fn test_can_get_server_temperature() {
let (rpc_server, state_lock) =
test_rpc_server(Network::Alpha, WalletSecret::new_random(), 2).await;
let current_server_temperature = rpc_server.get_cpu_temps(context::current()).await;
// Silicon Macs do not provide CPU temperature using systemstat
if std::env::consts::OS == "macos" {
assert!(current_server_temperature.is_none());
} else {
assert!(current_server_temperature.is_some());
}
}
}

0 comments on commit ce0537d

Please sign in to comment.