-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathmain.rs
More file actions
142 lines (125 loc) · 4.49 KB
/
main.rs
File metadata and controls
142 lines (125 loc) · 4.49 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::process::exit;
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#[cfg(feature = "kafka")]
use parseable::connectors;
use parseable::{
IngestServer, ParseableServer, QueryServer, Server, banner, metrics, option::Mode,
parseable::PARSEABLE, rbac, storage,
};
use tokio::signal::ctrl_c;
use tokio::sync::oneshot;
use tracing::Level;
use tracing::{info, warn};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Registry, fmt};
// Use jemalloc as the global allocator
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[actix_web::main]
async fn main() -> anyhow::Result<()> {
init_logger();
// these are empty ptrs so mem footprint should be minimal
let server: Box<dyn ParseableServer> = match &PARSEABLE.options.mode {
Mode::Query => Box::new(QueryServer),
Mode::Ingest => Box::new(IngestServer),
Mode::Index => {
println!(
"Indexing is an enterprise feature. Check out https://www.parseable.com/pricing to know more!"
);
exit(0)
}
Mode::Prism => {
println!(
"Prism is an enterprise feature. Check out https://www.parseable.com/pricing to know more!"
);
exit(0)
}
Mode::All => Box::new(Server),
};
// load metadata from persistence
let parseable_json = server.load_metadata().await?;
let metadata = storage::resolve_parseable_metadata(&parseable_json).await?;
banner::print(&PARSEABLE, &metadata).await;
// initialize the rbac map
rbac::map::init(&metadata);
// keep metadata info in mem
metadata.set_global();
// Spawn a task to trigger graceful shutdown on appropriate signal
let (shutdown_trigger, shutdown_rx) = oneshot::channel::<()>();
tokio::spawn(async move {
block_until_shutdown_signal().await;
// Trigger graceful shutdown
warn!("Received shutdown signal, notifying server to shut down...");
shutdown_trigger.send(()).unwrap();
});
let prometheus = metrics::build_metrics_handler();
// Start servers
#[cfg(feature = "kafka")]
{
let parseable_server = server.init(&prometheus, shutdown_rx);
let connectors = connectors::init(&prometheus);
tokio::try_join!(parseable_server, connectors)?;
}
#[cfg(not(feature = "kafka"))]
{
let parseable_server = server.init(&prometheus, shutdown_rx);
parseable_server.await?;
}
Ok(())
}
pub fn init_logger() {
let filter_layer = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
let default_level = if cfg!(debug_assertions) {
Level::DEBUG
} else {
Level::WARN
};
EnvFilter::new(default_level.to_string())
});
let fmt_layer = fmt::layer()
.with_thread_names(true)
.with_thread_ids(true)
.with_line_number(true)
.with_timer(tracing_subscriber::fmt::time::UtcTime::rfc_3339())
.with_target(true)
.compact();
Registry::default()
.with(filter_layer)
.with(fmt_layer)
.init();
}
#[cfg(windows)]
/// Asynchronously blocks until a shutdown signal is received
pub async fn block_until_shutdown_signal() {
_ = ctrl_c().await;
info!("Received a CTRL+C event");
}
#[cfg(unix)]
/// Asynchronously blocks until a shutdown signal is received
pub async fn block_until_shutdown_signal() {
use tokio::signal::unix::{SignalKind, signal};
let mut sigterm =
signal(SignalKind::terminate()).expect("Failed to create SIGTERM signal handler");
tokio::select! {
_ = ctrl_c() => info!("Received SIGINT signal"),
_ = sigterm.recv() => info!("Received SIGTERM signal"),
}
}