From 82b954fc71da3b025fd4d580c6e4db12d51f1323 Mon Sep 17 00:00:00 2001 From: Jannis Pohlmann Date: Thu, 26 Oct 2023 14:50:44 +0200 Subject: [PATCH] feat: add graceful shutdown to http indexer service --- .../indexer_service/http/indexer_service.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/common/src/indexer_service/http/indexer_service.rs b/common/src/indexer_service/http/indexer_service.rs index 8818361f..f1e7391a 100644 --- a/common/src/indexer_service/http/indexer_service.rs +++ b/common/src/indexer_service/http/indexer_service.rs @@ -19,6 +19,7 @@ use reqwest::StatusCode; use serde::{de::DeserializeOwned, Serialize}; use sqlx::postgres::PgPoolOptions; use thiserror::Error; +use tokio::signal; use toolshed::thegraph::DeploymentId; use tracing::info; @@ -297,3 +298,25 @@ impl IndexerService { }); } } + +pub async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("Failed to install Ctrl+C handler"); + }; + + let terminate = async { + signal::unix::signal(signal::unix::SignalKind::terminate()) + .expect("Failed to install signal handler") + .recv() + .await; + }; + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } + + info!("Signal received, starting graceful shutdown"); +}