-
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Check port availability before starting Tomcat (#2716)
It gave just a cryptic error before, hard to determine what to do. Closes #2648. Co-authored-by: StaNov <[email protected]>
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
backend/app/src/main/kotlin/io/tolgee/configuration/PortAvailability.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.tolgee.configuration | ||
|
||
import okhttp3.internal.closeQuietly | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.boot.web.servlet.ServletContextInitializer | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import java.io.IOException | ||
import java.net.ServerSocket | ||
import kotlin.system.exitProcess | ||
|
||
@Configuration | ||
class PortAvailability { | ||
private val logger = LoggerFactory.getLogger(PortAvailability::class.java) | ||
|
||
@Bean | ||
fun portAvailabilityChecker( | ||
@Value("\${server.port:8080}") port: Int, | ||
@Value("\${management.server.port:#{null}}") managementPort: Int?, | ||
): ServletContextInitializer { | ||
return ServletContextInitializer { | ||
checkPortAvailability(port, "server.port") | ||
managementPort?.also { checkPortAvailability(it, "management.server.port") } | ||
} | ||
} | ||
|
||
private fun checkPortAvailability( | ||
port: Int, | ||
propertyKey: String, | ||
) { | ||
try { | ||
ServerSocket(port).closeQuietly() | ||
} catch (e: IOException) { | ||
logger.error( | ||
"Port $port is already in use. " + | ||
"Please free the port or specify a different port using the `$propertyKey` property.", | ||
e, | ||
) | ||
exitProcess(1) | ||
} | ||
} | ||
} |