diff --git a/Cargo.lock b/Cargo.lock index 1d54ab34b..66c95ac38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -26,7 +26,7 @@ dependencies = [ "memchr", "pin-project-lite 0.2.9", "tokio 1.27.0", - "tokio-util 0.7.7", + "tokio-util 0.7.8", ] [[package]] @@ -61,7 +61,7 @@ dependencies = [ "sha1", "smallvec", "tokio 1.27.0", - "tokio-util 0.7.7", + "tokio-util 0.7.8", "tracing", ] @@ -512,7 +512,7 @@ dependencies = [ "futures-util", "http-body 0.4.5", "serde_json", - "tokio-util 0.7.7", + "tokio-util 0.7.8", "tower-service", ] @@ -2988,6 +2988,7 @@ dependencies = [ "sqlx", "thiserror", "tokio 1.27.0", + "tokio-util 0.7.8", "tracing", "wasmer", "wasmer-compiler-cranelift", @@ -3781,7 +3782,7 @@ dependencies = [ "indexmap", "slab", "tokio 1.27.0", - "tokio-util 0.7.7", + "tokio-util 0.7.8", "tracing", ] @@ -7269,7 +7270,7 @@ dependencies = [ "futures-core", "pin-project-lite 0.2.9", "tokio 1.27.0", - "tokio-util 0.7.7", + "tokio-util 0.7.8", ] [[package]] @@ -7300,9 +7301,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.7" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ "bytes 1.4.0", "futures-core", @@ -7374,7 +7375,7 @@ dependencies = [ "percent-encoding", "pin-project-lite 0.2.9", "tokio 1.27.0", - "tokio-util 0.7.7", + "tokio-util 0.7.8", "tower", "tower-layer", "tower-service", diff --git a/Cargo.toml b/Cargo.toml index f851ff500..c64864240 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/packages/fuel-indexer/Cargo.toml b/packages/fuel-indexer/Cargo.toml index 615af5a70..e8b179466 100644 --- a/packages/fuel-indexer/Cargo.toml +++ b/packages/fuel-indexer/Cargo.toml @@ -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" } diff --git a/packages/fuel-indexer/src/commands/run.rs b/packages/fuel-indexer/src/commands/run.rs index d23c26662..a0fa05f44 100644 --- a/packages/fuel-indexer/src/commands/run.rs +++ b/packages/fuel-indexer/src/commands/run.rs @@ -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 = @@ -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"))] @@ -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(()) } @@ -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