mirror of
https://github.com/librecaptcha/lc-core.git
synced 2025-02-11 18:58:06 -05:00
shutdown server gracefully
This commit is contained in:
parent
2d9fd3c68e
commit
a54947cfc2
@ -25,6 +25,15 @@ object LCFramework {
|
|||||||
playgroundEnabled = config.playgroundEnabled,
|
playgroundEnabled = config.playgroundEnabled,
|
||||||
corsHeader = config.corsHeader
|
corsHeader = config.corsHeader
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Runtime.getRuntime.addShutdownHook( new Thread {
|
||||||
|
override def run(): Unit = {
|
||||||
|
println("Shutting down gracefully...")
|
||||||
|
backgroundTask.shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
server.start()
|
server.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,18 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
|
|||||||
val requiredCountPerCombination = Math.max(1, (config.throttle * 1.01) / allCombinations.size).toInt
|
val requiredCountPerCombination = Math.max(1, (config.throttle * 1.01) / allCombinations.size).toInt
|
||||||
|
|
||||||
for (param <- allCombinations) {
|
for (param <- allCombinations) {
|
||||||
val countExisting = captchaManager.getCount(param).getOrElse(0)
|
if (!shutdownInProgress) {
|
||||||
val countRequired = requiredCountPerCombination - countExisting
|
val countExisting = captchaManager.getCount(param).getOrElse(0)
|
||||||
if (countRequired > 0) {
|
val countRequired = requiredCountPerCombination - countExisting
|
||||||
val countCreate = Math.min(1.0 + requiredCountPerCombination/10.0, countRequired).toInt
|
if (countRequired > 0) {
|
||||||
println(s"Creating $countCreate of $countRequired captchas for $param")
|
val countCreate = Math.min(1.0 + requiredCountPerCombination/10.0, countRequired).toInt
|
||||||
|
println(s"Creating $countCreate of $countRequired captchas for $param")
|
||||||
|
|
||||||
for (i <- 0 until countCreate) {
|
for (i <- 0 until countCreate) {
|
||||||
captchaManager.generateChallenge(param)
|
if (!shutdownInProgress) {
|
||||||
|
captchaManager.generateChallenge(param)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -62,9 +66,22 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
|
|||||||
list(HelperFunctions.randomNumber(list.size))
|
list(HelperFunctions.randomNumber(list.size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val ex = new ScheduledThreadPoolExecutor(1)
|
||||||
|
|
||||||
def beginThread(delay: Int): Unit = {
|
def beginThread(delay: Int): Unit = {
|
||||||
val ex = new ScheduledThreadPoolExecutor(1)
|
|
||||||
ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
|
ex.scheduleWithFixedDelay(task, 1, delay, TimeUnit.SECONDS)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@volatile var shutdownInProgress = false
|
||||||
|
|
||||||
|
def shutdown(): Unit = {
|
||||||
|
println(" Shutting down background task...")
|
||||||
|
shutdownInProgress = true
|
||||||
|
ex.shutdown()
|
||||||
|
println(" Finished Shutting background task")
|
||||||
|
println(" Shutting down DB...")
|
||||||
|
Statements.tlStmts.get.shutdown.execute()
|
||||||
|
println(" Finished shutting down db")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package lc.database
|
|||||||
import java.sql.{Connection, DriverManager, Statement}
|
import java.sql.{Connection, DriverManager, Statement}
|
||||||
|
|
||||||
class DBConn() {
|
class DBConn() {
|
||||||
val con: Connection = DriverManager.getConnection("jdbc:h2:./data/H2/captcha2", "sa", "")
|
val con: Connection = DriverManager.getConnection("jdbc:h2:./data/H2/captcha2;MAX_COMPACT_TIME=8000;DB_CLOSE_ON_EXIT=FALSE", "sa", "")
|
||||||
|
|
||||||
def getStatement(): Statement = {
|
def getStatement(): Statement = {
|
||||||
con.createStatement()
|
con.createStatement()
|
||||||
|
@ -120,6 +120,14 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
|
|||||||
"SELECT * FROM mapId"
|
"SELECT * FROM mapId"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val shutdown: PreparedStatement = dbConn.con.prepareStatement(
|
||||||
|
"SHUTDOWN"
|
||||||
|
)
|
||||||
|
|
||||||
|
val shutdownCompact: PreparedStatement = dbConn.con.prepareStatement(
|
||||||
|
"SHUTDOWN COMPACT"
|
||||||
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
object Statements {
|
object Statements {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user