From 34c66484f522a6a7d28d88a32189c4368afea782 Mon Sep 17 00:00:00 2001 From: Igor Artamonov Date: Wed, 17 Feb 2021 20:54:58 -0500 Subject: [PATCH] solution: graceful shutdown if config is not available --- src/main/kotlin/io/emeraldpay/dshackle/Config.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/emeraldpay/dshackle/Config.kt b/src/main/kotlin/io/emeraldpay/dshackle/Config.kt index 90b2148a..c012ba7c 100644 --- a/src/main/kotlin/io/emeraldpay/dshackle/Config.kt +++ b/src/main/kotlin/io/emeraldpay/dshackle/Config.kt @@ -24,6 +24,9 @@ import io.emeraldpay.dshackle.config.* import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.boot.ExitCodeGenerator +import org.springframework.boot.SpringApplication +import org.springframework.context.ApplicationContext import org.springframework.context.annotation.* import org.springframework.core.env.Environment import org.springframework.scheduling.annotation.EnableAsync @@ -34,12 +37,14 @@ import java.io.File import java.text.SimpleDateFormat import java.util.* import java.util.concurrent.Executors +import kotlin.system.exitProcess @Configuration @EnableScheduling @EnableAsync open class Config( - @Autowired private val env: Environment + @Autowired private val env: Environment, + @Autowired private val ctx: ApplicationContext ) { companion object { @@ -79,6 +84,11 @@ open class Config( open fun mainConfig(@Autowired fileResolver: FileResolver): MainConfig { val f = configFilePath ?: throw IllegalStateException("Config path is not set") log.info("Using config: ${f.absolutePath}") + if (!f.exists() || !f.isFile) { + log.error("Config doesn't exist or not a file: ${f.absolutePath}") + SpringApplication.exit(ctx, ExitCodeGenerator { 1 }) + exitProcess(1) + } val reader = MainConfigReader(fileResolver) return reader.read(f.inputStream()) ?: throw IllegalStateException("Config is not available at ${f.absolutePath}")