create equal number of captchas for each parameter combination in background task

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2022-02-28 00:17:13 +05:30
parent e44a1df22e
commit 8316084ee7
3 changed files with 33 additions and 22 deletions

View File

@ -18,22 +18,33 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate() challengeGCPstmt.executeUpdate()
val imageNumResult = Statements.tlStmts.get.getCountChallengeTable.executeQuery() for (param <- allParameterCombinations()) {
val imageNum = if (imageNumResult.next()) { val imageNum = captchaManager.getCount(param).getOrElse(0)
imageNumResult.getInt("total") val countCreate = (config.throttle * 1.1).toInt - imageNum
} else { if (countCreate > 0) {
0 println(s"Creating $countCreate captchas for $param")
}
val throttle = (config.throttle * 1.1).toInt - imageNum for (i <- 0 until countCreate) {
captchaManager.generateChallenge(param)
for (i <- 0 until throttle) { }
captchaManager.generateChallenge(getRandomParam()) }
} }
} catch { case exception: Exception => println(exception) } } catch { case exception: Exception => println(exception) }
} }
} }
private def allParameterCombinations(): List[Parameters] = {
(config.captchaConfig).flatMap {captcha =>
(captcha.allowedLevels).flatMap {level =>
(captcha.allowedMedia).flatMap {media =>
(captcha.allowedInputType).map {inputType =>
Parameters(level, media, inputType, Some(Size(0, 0)))
}
}
}
}
}
private def getRandomParam(): Parameters = { private def getRandomParam(): Parameters = {
val captcha = pickRandom(config.captchaConfig) val captcha = pickRandom(config.captchaConfig)
val level = pickRandom(captcha.allowedLevels) val level = pickRandom(captcha.allowedLevels)

View File

@ -105,8 +105,8 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
} }
} }
private def getCount(param: Parameters): Option[Int] = { def getCount(param: Parameters): Option[Int] = {
val countPstmt = Statements.tlStmts.get.countPstmt val countPstmt = Statements.tlStmts.get.countForParameterPstmt
countPstmt.setString(1, param.level) countPstmt.setString(1, param.level)
countPstmt.setString(2, param.media) countPstmt.setString(2, param.media)
countPstmt.setString(3, param.input_type) countPstmt.setString(3, param.input_type)
@ -123,16 +123,16 @@ class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
if (count == 0) { if (count == 0) {
None None
} else { } else {
val tokenPstmt = Statements.tlStmts.get.tokenPstmt val tokenPstmt = Statements.tlStmts.get.tokenPstmt
tokenPstmt.setString(1, param.level) tokenPstmt.setString(1, param.level)
tokenPstmt.setString(2, param.media) tokenPstmt.setString(2, param.media)
tokenPstmt.setString(3, param.input_type) tokenPstmt.setString(3, param.input_type)
tokenPstmt.setInt(4, count) tokenPstmt.setInt(4, count)
val rs = tokenPstmt.executeQuery() val rs = tokenPstmt.executeQuery()
if (rs.next()) { if (rs.next()) {
Some(rs.getInt("token")) Some(rs.getInt("token"))
} else { } else {
None None
} }
} }
} }

View File

@ -70,7 +70,7 @@ class Statements(dbConn: DBConn, maxAttempts: Int) {
"WHERE token = ?;" "WHERE token = ?;"
) )
val countPstmt: PreparedStatement = dbConn.con.prepareStatement( val countForParameterPstmt: PreparedStatement = dbConn.con.prepareStatement(
s""" s"""
SELECT count(*) as count SELECT count(*) as count
FROM challenge FROM challenge