in background task, divide total required by number of combinations, and create in multiple iterations

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2022-02-28 09:30:31 +05:30
parent 8316084ee7
commit cb0c244779
1 changed files with 9 additions and 5 deletions

View File

@ -18,11 +18,15 @@ class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
val challengeGCPstmt = Statements.tlStmts.get.challengeGCPstmt
challengeGCPstmt.executeUpdate()
for (param <- allParameterCombinations()) {
val imageNum = captchaManager.getCount(param).getOrElse(0)
val countCreate = (config.throttle * 1.1).toInt - imageNum
if (countCreate > 0) {
println(s"Creating $countCreate captchas for $param")
val allCombinations = allParameterCombinations()
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")
for (i <- 0 until countCreate) {
captchaManager.generateChallenge(param)