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,
|
||||
corsHeader = config.corsHeader
|
||||
)
|
||||
|
||||
Runtime.getRuntime.addShutdownHook( new Thread {
|
||||
override def run(): Unit = {
|
||||
println("Shutting down gracefully...")
|
||||
backgroundTask.shutdown()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
server.start()
|
||||
}
|
||||
}
|
||||
|
@ -22,14 +22,18 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
|
||||
val requiredCountPerCombination = Math.max(1, (config.throttle * 1.01) / allCombinations.size).toInt
|
||||
|
||||
for (param <- allCombinations) {
|
||||
val countExisting = captchaManager.getCount(param).getOrElse(0)
|
||||
val countRequired = requiredCountPerCombination - countExisting
|
||||
if (countRequired > 0) {
|
||||
val countCreate = Math.min(1.0 + requiredCountPerCombination/10.0, countRequired).toInt
|
||||
println(s"Creating $countCreate of $countRequired captchas for $param")
|
||||
if (!shutdownInProgress) {
|
||||
val countExisting = captchaManager.getCount(param).getOrElse(0)
|
||||
val countRequired = requiredCountPerCombination - countExisting
|
||||
if (countRequired > 0) {
|
||||
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) {
|
||||
captchaManager.generateChallenge(param)
|
||||
for (i <- 0 until countCreate) {
|
||||
if (!shutdownInProgress) {
|
||||
captchaManager.generateChallenge(param)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -62,9 +66,22 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
|
||||
list(HelperFunctions.randomNumber(list.size))
|
||||
}
|
||||
|
||||
private val ex = new ScheduledThreadPoolExecutor(1)
|
||||
|
||||
def beginThread(delay: Int): Unit = {
|
||||
val ex = new ScheduledThreadPoolExecutor(1)
|
||||
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}
|
||||
|
||||
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 = {
|
||||
con.createStatement()
|
||||
|
@ -120,6 +120,14 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
|
||||
"SELECT * FROM mapId"
|
||||
)
|
||||
|
||||
val shutdown: PreparedStatement = dbConn.con.prepareStatement(
|
||||
"SHUTDOWN"
|
||||
)
|
||||
|
||||
val shutdownCompact: PreparedStatement = dbConn.con.prepareStatement(
|
||||
"SHUTDOWN COMPACT"
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
object Statements {
|
||||
|
Loading…
x
Reference in New Issue
Block a user