Skip to content

Commit b4a20fc

Browse files
committed
feat: add output of downtime info
1 parent ac317ec commit b4a20fc

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

src/heartbeat.rs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use crate::{db::save_kv, model::schedule::UnixTimestamp};
1+
use std::time::Duration;
2+
3+
use crate::{
4+
db::{load_kv, save_kv},
5+
model::schedule::UnixTimestamp,
6+
};
7+
use human_time::ToHumanTimeString;
28
use tracing::{error, info};
39

410
const KEY: &str = "HEARTBEAT";
@@ -20,7 +26,39 @@ pub fn start_heartbeat(db_pool: sqlx::PgPool) {
2026
});
2127
}
2228

23-
pub fn time_since_last_heartbeat(db_pool: sqlx::PgPool) -> String {
24-
// TODO 1: Get last heartbeat from DB
25-
"First run".to_string()
29+
pub async fn last_heartbeat_info(db_pool: sqlx::PgPool) -> String {
30+
match load_kv(&db_pool, KEY).await {
31+
Some(db_value) => match UnixTimestamp::from_db_fmt(&db_value) {
32+
Ok(last_heartbeat) => {
33+
let Ok(now) = UnixTimestamp::now() else {
34+
return format!(
35+
"Last Heartbeat: {last_heartbeat} but Failed to get current timestamp"
36+
);
37+
};
38+
let seconds_since_last_heartbeat = now.0 - last_heartbeat.0;
39+
if seconds_since_last_heartbeat < 0 {
40+
return format!(
41+
"Last heartbeat in the future?! Last heartbeat: {last_heartbeat}, Now: {now}"
42+
);
43+
}
44+
let Ok(seconds_since_last_heartbeat) = seconds_since_last_heartbeat.try_into()
45+
else {
46+
// Invalid u64
47+
return format!(
48+
"Invalid u64!!! Seconds since heartbeat: {seconds_since_last_heartbeat}, Last heartbeat: {last_heartbeat}, Now: {now}"
49+
);
50+
};
51+
let downtime = Duration::from_secs(seconds_since_last_heartbeat);
52+
format!(
53+
"Downtime: {}\nLast Heartbeat: {last_heartbeat}\nNow: {now}",
54+
downtime.to_human_time_string()
55+
)
56+
}
57+
Err(err) => {
58+
error!(?err);
59+
"Error Loading Last Heartbeat".to_string()
60+
}
61+
},
62+
None => "First run".to_string(),
63+
}
2664
}

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ async fn main(
8484
error!("Development run detected but no guild ID found so slash commands NOT registered");
8585
}
8686
let connect_msg = format!(
87-
"{} is connected! Version: {} [{}]",
87+
"{} is connected! Version: {}\n{}",
8888
ready.user.name, version!(),
89-
heartbeat::time_since_last_heartbeat(db_pool.clone()),
89+
heartbeat::last_heartbeat_info(db_pool.clone()).await,
9090
);
9191
info!("{connect_msg}");
9292
if let Some(channel) = shared_config.channel_bot_status{

src/model/schedule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub mod protected_ops;
1313
pub type ScheduledTaskId = OneBasedId;
1414

1515
#[derive(Debug, serde::Serialize, serde::Deserialize, Default, Clone, Copy)]
16-
pub struct UnixTimestamp(i32);
16+
pub struct UnixTimestamp(pub i32);
1717
impl UnixTimestamp {
1818
pub fn new(value: i32) -> Self {
1919
Self(value)

0 commit comments

Comments
 (0)