1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// TODO: #![warn(missing_docs)]

#[macro_use]
pub extern crate log;
pub mod config;
pub mod info_service;
pub mod logging;

use config::*;
use futures::future::{lazy, Future, FutureExt};
pub use git_hash::crate_version_with_git_hash_short as version;

/// Init standard handlers for cli-executable.
/// Create standard logger, init integrations such as with sentry if enabled.
/// By default - init logging without extra integrations.
pub fn init(log: &LoggingOptions, integrations: &IntegrationsOptions) -> Option<impl Drop> {
    use logging::*;
    #[cfg(feature = "sentry")]
    {
        support_sentry::init(log, integrations)
    }

    #[cfg(not(feature = "sentry"))]
    {
        init_logging(log)
            .map(|_| trace!("Logging system initialized."))
            .map_err(|err| eprintln!("Attempt to init global logger once more. {:?}", err))
            .ok()
    }
}

pub fn init_sigterm_handler() -> std::sync::mpsc::Receiver<signal_notify::Signal> {
    use signal_notify::{notify, Signal};
    notify(&[Signal::TERM])
}

pub fn init_sigterm_handler_fut<F>(f: F) -> impl Future<Output = ()> + Send
where
    F: Send + FnOnce(),
{
    let rx = init_sigterm_handler();
    lazy(move |_| rx.recv()).map(|sig| {
        if let Ok(sig) = sig {
            info!("Received signal {:?}. Shutting down services", sig);

            f();

            use std::time::Duration;
            std::thread::sleep(Duration::from_secs(3));

            debug!(".");
            std::process::exit(130);
        }
    })
}