Skip to content

Commit

Permalink
Block on final rollbar thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
mijoharas committed Jan 22, 2021
1 parent f81e912 commit cbd682d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
50 changes: 45 additions & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use log::{debug, error, info, warn};
#[cfg(feature = "with_rollbar")]
use backtrace;
#[cfg(feature = "with_rollbar")]
use rollbar;
use rollbar::{self, ResponseStatus};

#[cfg(feature = "with_rollbar")]
lazy_static! {
static ref ROLLBAR_ACCESS_TOKEN: String =
std::env::var("ROLLBAR_ACCESS_TOKEN").expect("ROLLBAR_ACCESS_TOKEN env is not set");
static ref ROLLBAR_CLIENT: rollbar::Client =
rollbar::Client::new((*ROLLBAR_ACCESS_TOKEN).clone(), "development".to_owned());
static ref LAST_ROLLBAR_THREAD_HANDLE: std::sync::Mutex<Option<std::thread::JoinHandle<Option<ResponseStatus>>>> =
std::sync::Mutex::new(None);
}

#[cfg(feature = "with_rollbar")]
Expand All @@ -29,12 +31,37 @@ pub fn register_panic_handler() {
.with_backtrace(&backtrace)
.send()
.join();
// explicitly we don't unwrap to not panic in a panic handler
result.is_err();
match result {
Ok(..) => {}
Err(err) => {
error!("Error sending rollbar message {:?}", err);
}
}
// NOTE: on join see here https://github.com/RoxasShadow/rollbar-rs/issues/16
}))
}

#[cfg(feature = "with_rollbar")]
pub fn set_rollbar_thread_handle(thread_handle: std::thread::JoinHandle<Option<ResponseStatus>>) {
let mut unwrapped = LAST_ROLLBAR_THREAD_HANDLE.lock().unwrap();
*unwrapped = Some(thread_handle);
}

#[cfg(feature = "with_rollbar")]
pub fn block_on_last_rollbar_thread_handle() {
let mut last_join_handle_option = LAST_ROLLBAR_THREAD_HANDLE.lock().unwrap();
if last_join_handle_option.is_some() {
let last_join_handle_option = std::mem::replace(&mut *last_join_handle_option, None);
let join_thread_result = last_join_handle_option.unwrap().join();
match join_thread_result {
Ok(..) => {}
Err(err) => {
error!("error blocking on last_rollbar_thread_handle {:?}", err);
}
}
}
}

pub struct Logger {}

// get name of function, with struct e.t.c.
Expand Down Expand Up @@ -100,15 +127,28 @@ impl Logger {
let message = Self::structured_format(wal_number, table_name, tag, message);

#[cfg(feature = "with_rollbar")]
report_error_message!(ROLLBAR_CLIENT, message);
{
let thread_handle = report_error_message!(ROLLBAR_CLIENT, message);
set_rollbar_thread_handle(thread_handle);
}
error!("{}", message);
}

pub fn warning(wal_number: Option<u64>, table_name: Option<&String>, tag: &str, message: &str) {
let message = Self::structured_format(wal_number, table_name, tag, message);

// report_warning_message!(ROLLBAR_CLIENT, message);
// this macro doesn't exist so be explicit
#[cfg(feature = "with_rollbar")]
report_warning_message!(ROLLBAR_CLIENT, message);
{
let thread_handle = ROLLBAR_CLIENT
.build_report()
.from_message(&message)
.with_level(::rollbar::Level::INFO)
.send();
set_rollbar_thread_handle(thread_handle);
}

warn!("{}", message);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(str_split_once)]
// #![deny(warnings)]
#![deny(warnings)]

use clap::{App, Arg};
use either::Either;
Expand Down Expand Up @@ -61,9 +61,6 @@ async fn main() {
#[cfg(feature = "with_rollbar")]
logger::register_panic_handler();

println!("rollbar registered");
panic!("Mike test");

let mut parser = parser::Parser::new(true);
let mut collector = change_processing::ChangeProcessing::new();
// initialize our channels
Expand Down Expand Up @@ -217,6 +214,9 @@ async fn main() {
wal_file_manager.clean_up_final_wal_file();

ShutdownHandler::log_shutdown_status();

#[cfg(feature = "with_rollbar")]
logger::block_on_last_rollbar_thread_handle();
}

async fn drain_collector_and_transmit(
Expand Down

0 comments on commit cbd682d

Please sign in to comment.