-
Notifications
You must be signed in to change notification settings - Fork 2
feat(router): added a simple wrapper around background tasks #452
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use async_trait::async_trait; | ||
use futures::future::join_all; | ||
use std::sync::Arc; | ||
use tokio::task::JoinHandle; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::{debug, info}; | ||
|
||
#[async_trait] | ||
pub trait BackgroundTask: Send + Sync { | ||
Check failure on line 9 in bin/router/src/background_tasks/mod.rs
|
||
fn id(&self) -> &str; | ||
async fn run(&self, token: CancellationToken); | ||
} | ||
|
||
pub struct BackgroundTasksManager { | ||
cancellation_token: CancellationToken, | ||
task_handles: Vec<JoinHandle<()>>, | ||
} | ||
|
||
impl BackgroundTasksManager { | ||
pub fn new() -> Self { | ||
Self { | ||
cancellation_token: CancellationToken::new(), | ||
task_handles: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn register_task<T>(&mut self, task: T) | ||
Check failure on line 27 in bin/router/src/background_tasks/mod.rs
|
||
where | ||
T: BackgroundTask + 'static, | ||
{ | ||
info!("registering background task: {}", task.id()); | ||
let child_token = self.cancellation_token.clone(); | ||
let task_arc = Arc::new(task); | ||
|
||
let handle = tokio::spawn(async move { | ||
task_arc.run(child_token).await; | ||
}); | ||
|
||
self.task_handles.push(handle); | ||
} | ||
|
||
pub async fn shutdown(self) { | ||
info!("shutdown triggered, stopping all background tasks..."); | ||
self.cancellation_token.cancel(); | ||
|
||
debug!("waiting for background tasks to finish..."); | ||
join_all(self.task_handles).await; | ||
|
||
println!("all background tasks have been shut down gracefully."); | ||
Comment on lines
+47
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple of improvements that can be made here:
This suggestion addresses both points. let join_results = join_all(self.task_handles).await;
for result in join_results {
if let Err(err) = result {
error!("A background task panicked during shutdown: {:?}", err);
}
}
info!("all background tasks have been shut down gracefully."); |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod background_tasks; | ||
mod http_utils; | ||
mod logger; | ||
mod pipeline; | ||
|
@@ -6,6 +7,7 @@ | |
use std::sync::Arc; | ||
|
||
use crate::{ | ||
background_tasks::BackgroundTasksManager, | ||
http_utils::{health::health_check_handler, landing_page::landing_page_handler}, | ||
logger::configure_logging, | ||
pipeline::graphql_request_handler, | ||
|
@@ -19,6 +21,7 @@ | |
}; | ||
|
||
use hive_router_query_planner::utils::parsing::parse_schema; | ||
use tracing::info; | ||
|
||
async fn graphql_endpoint_handler( | ||
mut request: HttpRequest, | ||
|
@@ -37,8 +40,9 @@ | |
let parsed_schema = parse_schema(&supergraph_sdl); | ||
let addr = router_config.http.address(); | ||
let shared_state = RouterSharedState::new(parsed_schema, router_config); | ||
let mut bg_tasks_manager = BackgroundTasksManager::new(); | ||
Check failure on line 43 in bin/router/src/lib.rs
|
||
|
||
web::HttpServer::new(move || { | ||
let maybe_error = web::HttpServer::new(move || { | ||
web::App::new() | ||
.state(shared_state.clone()) | ||
.route("/graphql", web::to(graphql_endpoint_handler)) | ||
|
@@ -48,5 +52,10 @@ | |
.bind(addr)? | ||
.run() | ||
.await | ||
.map_err(|err| err.into()) | ||
.map_err(|err| err.into()); | ||
|
||
info!("server stopped, clearning background tasks"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
bg_tasks_manager.shutdown().await; | ||
|
||
maybe_error | ||
} |
Uh oh!
There was an error while loading. Please reload this page.