Skip to content

Commit a16afed

Browse files
committed
Merge #136: Shutdown on unexpected task exit
Approved-by: ReinierMaas Priority: Normal Auto-deploy: false
2 parents e4e7fea + df5b911 commit a16afed

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

opsqueue/app/main.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use opentelemetry_resource_detectors::{OsResourceDetector, ProcessResourceDetect
66
use opentelemetry_sdk::trace::{RandomIdGenerator, Sampler, SdkTracerProvider};
77
use opsqueue::{common::submission::db::periodically_cleanup_old, config::Config, prometheus};
88
use std::{
9+
error::Error,
910
sync::{atomic::AtomicBool, Arc},
1011
time::Duration,
1112
};
@@ -47,9 +48,9 @@ pub async fn async_main() {
4748
.expect("Timed out while initiating the database");
4849

4950
moro_local::async_scope!(|scope| {
50-
scope.spawn(db_pool.periodically_checkpoint_wal());
51+
let checkpoint_handle = scope.spawn(db_pool.periodically_checkpoint_wal());
5152

52-
scope.spawn(opsqueue::server::serve_producer_and_consumer(
53+
let server_handle = scope.spawn(opsqueue::server::serve_producer_and_consumer(
5354
config,
5455
&server_addr,
5556
&db_pool,
@@ -60,15 +61,15 @@ pub async fn async_main() {
6061
));
6162

6263
let max_age = config.max_submission_age.into();
63-
scope.spawn(periodically_cleanup_old(db_pool.writer_pool(), max_age));
64+
let cleanup_handle = scope.spawn(periodically_cleanup_old(db_pool.writer_pool(), max_age));
6465

65-
scope.spawn(prometheus::periodically_calculate_scaling_metrics(
66+
let prometheus_handle = scope.spawn(prometheus::periodically_calculate_scaling_metrics(
6667
&db_pool,
6768
&cancellation_token,
6869
));
6970

7071
// Set up complete. Start up watchdog, which will mark app healthy when appropriate
71-
scope.spawn(opsqueue::server::app_watchdog(
72+
let watchdog_handle = scope.spawn(opsqueue::server::app_watchdog(
7273
app_healthy_flag.clone(),
7374
&db_pool,
7475
cancellation_token.clone(),
@@ -79,9 +80,29 @@ pub async fn async_main() {
7980
opsqueue::version_info()
8081
);
8182

82-
tokio::signal::ctrl_c()
83-
.await
84-
.expect("Failed to set up Ctrl+C signal handler");
83+
tokio::select! {
84+
_ = checkpoint_handle => {
85+
tracing::error!("Checkpointing task exited unexpectedly");
86+
},
87+
_ = server_handle => {
88+
tracing::error!("Server task exited unexpectedly");
89+
},
90+
_ = cleanup_handle => {
91+
tracing::error!("Cleanup task exited unexpectedly");
92+
},
93+
_ = prometheus_handle => {
94+
tracing::error!("Prometheus metrics calculation task exited unexpectedly");
95+
},
96+
_ = watchdog_handle => {
97+
tracing::error!("Watchdog task exited unexpectedly");
98+
},
99+
res = tokio::signal::ctrl_c() => {
100+
match res {
101+
Ok(_) => tracing::warn!("Received Ctrl-C signal"),
102+
Err(ref err) => tracing::error!(error = err as &dyn Error, "Error while waiting for Ctrl-C signal"),
103+
};
104+
},
105+
};
85106

86107
tracing::warn!("Opsqueue is shutting down");
87108

0 commit comments

Comments
 (0)