Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
use CancellationToken for graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
lostman committed May 11, 2023
1 parent a386e70 commit 9c92a65
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ fuels = { version = "0.40", default-features = false }
serde = { version = "1.0", default-features = false }
thiserror = "1.0"
tokio = "1.17"
tokio-util = "0.7.8"
tracing = "0.1"
1 change: 1 addition & 0 deletions packages/fuel-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tokio = { features = [
"sync",
"process",
], workspace = true }
tokio-util = { workspace = true }
tracing = { workspace = true }
wasmer = "2.3"
wasmer-compiler-cranelift = { version = "2.3" }
Expand Down
27 changes: 24 additions & 3 deletions packages/fuel-indexer/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {

let service_handle = tokio::spawn(service.run());

// for graceful shutdown
let token = tokio_util::sync::CancellationToken::new();
let cloned_token = token.clone();

#[cfg(feature = "api-server")]
{
let gql_handle =
Expand All @@ -117,11 +121,19 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {
};
let node_handle = tokio::spawn(FuelService::new_node(config));

let _ = tokio::join!(service_handle, node_handle, gql_handle);
tokio::spawn(async move {
let _ = tokio::join!(service_handle, node_handle, gql_handle);
token.cancel();
});

return Ok(());
}
}

tokio::spawn(async move {
let _ = tokio::join!(service_handle, gql_handle);
token.cancel();
});
}

#[cfg(not(feature = "api-server"))]
Expand All @@ -138,13 +150,19 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {
};
let node_handle = tokio::spawn(FuelService::new_node(config));

let _ = tokio::join!(service_handle, node_handle);
tokio::spawn(async move {
let _ = tokio::join!(service_handle, node_handle);
token.cancel();
});

return Ok(());
}
}

let _ = service_handle.await?;
tokio::spawn(async move {
let _ = service_handle.await?;
token.cancel();
});

Ok(())
}
Expand All @@ -165,6 +183,9 @@ pub async fn exec(args: IndexerArgs) -> anyhow::Result<()> {
_ = sigint.recv() => {
info!("Received SIGINT. Stopping services");
}
_ = cloned_token.cancelled() => {
info!("Received cancellation. Stopping services");
}
}

// stop embedded postgres
Expand Down

0 comments on commit 9c92a65

Please sign in to comment.