Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown #66

Open
wants to merge 21 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8a43508
Added initial code for graceful shutdown (Issue #45)
jo-asplin-met-no Feb 6, 2025
ffabee8
Typo (a bit pedantic, but still)
jo-asplin-met-no Feb 6, 2025
cb0d6cc
Refactored shutdown_signal to common package
jo-asplin-met-no Feb 10, 2025
df49f79
Moved shutdown_signal from common to util
jo-asplin-met-no Feb 11, 2025
8b01706
Removed superfluous signals
jo-asplin-met-no Feb 11, 2025
bb46d2f
Assumed Unix + added comments
jo-asplin-met-no Feb 11, 2025
3eff24a
Gracefully shut down kvkafka reader
jo-asplin-met-no Feb 12, 2025
d8ca2b6
Improved signal catching and task joining
jo-asplin-met-no Feb 12, 2025
dfa7bfb
Detected shutdown signal during end-to-end testing
jo-asplin-met-no Feb 12, 2025
ad49595
Don't generally await/join signal catcher task
jo-asplin-met-no Feb 12, 2025
6d050b4
Another go at cancelling kvkafka reader from caught signal
jo-asplin-met-no Feb 12, 2025
239b7c1
Remove `zero_to_none` function
Lun4m Feb 12, 2025
dc1a4c8
Create timeseries if label does not exist instead of returning Err
Lun4m Feb 12, 2025
7b46af6
Merge branch 'trunk' into graceful_shutdown
jo-asplin-met-no Feb 12, 2025
a63842e
Fixed lint issue
jo-asplin-met-no Feb 12, 2025
d4ef671
Fixed comment
jo-asplin-met-no Feb 13, 2025
eb9bd05
Used block to have attribute apply to multiple statements
jo-asplin-met-no Feb 13, 2025
ce4fee5
Fixed typos
jo-asplin-met-no Feb 13, 2025
a42a035
Yet another go at cancelling kafka reader, but essentially back to or…
jo-asplin-met-no Feb 13, 2025
decdd6b
Added debug printout
jo-asplin-met-no Feb 13, 2025
0c21b6c
Yet another go at cancelling kafka reader
jo-asplin-met-no Feb 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ members = [
"api",
"ingestion",
"integration_tests",
"rove_connector", "util",
"rove_connector",
"util",
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ axum.workspace = true
bb8.workspace = true
bb8-postgres.workspace = true
chrono.workspace = true
util = { path = "../util" }
postgres-types.workspace = true
serde.workspace = true
tokio.workspace = true
Expand Down
5 changes: 4 additions & 1 deletion api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ pub async fn run(pool: PgConnectionPool) {

// run it with hyper on localhost:3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
axum::serve(listener, app)
.with_graceful_shutdown(::util::shutdown_signal())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leading scoping operator before util looks a little odd here, is it correct?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it compiles. But I'll take a look ...

Copy link
Collaborator Author

@jo-asplin-met-no jo-asplin-met-no Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The leading scoping operator needs to be there for the code to compile. But maybe there's a better idiom to use in this case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is a namespace confict with the util module in this crate. We should maybe consider renaming one of them to avoid confusion. Or perhaps we can just move the contents of the util module into the util crate instead.

.await
.unwrap();
}
1 change: 1 addition & 0 deletions ingestion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bb8-postgres.workspace = true
bytes.workspace = true
chrono.workspace = true
chronoutil.workspace = true
util = { path = "../util" }
csv.workspace = true
futures.workspace = true
kafka.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion ingestion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,9 @@ pub async fn run(

// run our app with hyper, listening globally on port 3001
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await?;
axum::serve(listener, app).await?;
axum::serve(listener, app)
.with_graceful_shutdown(util::shutdown_signal())
.await?;

Ok(())
}
1 change: 1 addition & 0 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ name = "generate_partition_queries"

[dependencies]
chrono.workspace = true
tokio.workspace = true
26 changes: 26 additions & 0 deletions util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
use tokio::signal;

/// Returns a Future that completes once a signal to shutdown the service is caught.
pub async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c() // aka. SIGINT on Unix
.await
.expect("failed to install Ctrl+C (SIGINT) handler");
};

#[cfg(unix)]
let sigterm = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler for SIGTERM")
.recv()
.await;
};
#[cfg(not(unix))]
let sigterm = std::future::pending::<()>();
Lun4m marked this conversation as resolved.
Show resolved Hide resolved

// TODO: add more signals that should result in a shutdown?

tokio::select! {
_ = ctrl_c => {},
_ = sigterm => {},
}
}