Skip to content

Commit

Permalink
flushes log entries to telegram on stopping the process
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Hassan committed Jan 19, 2024
1 parent 44dcf60 commit fe8b6fd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

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

15 changes: 14 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "telelog"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

[dependencies]
Expand All @@ -12,5 +12,18 @@ regex = "1.10.2"
reqwest = "0.11.23"
secrets = "1.2.0"
serde = "1.0.195"
signal-hook = "0.3.17"
systemd = "0.10.0"
tokio = { version = "1.35.1", features = ["full"] }

[profile.release]
opt-level = 3
lto = "fat"
strip = "symbols"
debug = false
debug-assertions = false
overflow-checks = false
panic = 'unwind'
incremental = false
codegen-units = 2
rpath = false
23 changes: 22 additions & 1 deletion src/telegram.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use signal_hook::{consts::{SIGTERM,SIGINT}, iterator::Signals};
use tokio;
use tokio::sync::Mutex as AsyncMutex;
use reqwest;
Expand All @@ -14,6 +15,21 @@ lazy_static!(
pub async fn init(settings: AppSettings) {
let mut app_settings_copy = APP_SETTINGS_COPY.lock().await;
*app_settings_copy = settings;

tokio::task::spawn(async move {
let mut signals = Signals::new(&[SIGTERM, SIGINT]).unwrap();
for signal in signals.forever() {
match signal {
SIGTERM | SIGINT => {
println!("[telegram] Received stop signal");
flush_log_buffer().await;
std::process::exit(0);
},
_ => {},
};
}
});

println!("[telegram] initialised");
}

Expand Down Expand Up @@ -90,9 +106,14 @@ async fn flush_log_buffer() {

return true
});
drop(buffer); // release the lock

if message == "<code>\n" {
println!("[telegram] flush was ran, but buffer was empty");
return
}

message.push_str("</code>");
drop(buffer); // release the lock

// send the message to telegram
let settings = APP_SETTINGS_COPY.lock().await;
Expand Down

0 comments on commit fe8b6fd

Please sign in to comment.