simplify logic for background task

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2022-02-24 20:06:20 +05:30
parent 6758ec3acf
commit 497b88f0d3
1 changed files with 10 additions and 6 deletions

View File

@ -18,13 +18,17 @@ class BackgroundTask(config: Config, captcha: Captcha) {
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate()
val imageNum = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
var throttleIn = (config.throttle * 1.1).toInt
if (imageNum.next())
throttleIn = (throttleIn - imageNum.getInt("total"))
while (0 < throttleIn) {
val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery()
val imageNum = if (imageNumResult.next()) {
imageNumResult.getInt("total")
} else {
0
}
val throttle = (config.throttle * 1.1).toInt - imageNum
for (i <- 0 until throttle) {
captcha.generateChallenge(getRandomParam())
throttleIn -= 1
}
} catch { case exception: Exception => println(exception) }
}