feature/Improve Berlin Group Scheduler

This commit is contained in:
Marko Milić 2025-04-01 13:58:33 +02:00
parent eb387f9112
commit 9bc77aebe8
2 changed files with 27 additions and 9 deletions

View File

@ -165,8 +165,12 @@ jwt.use.ssl=false
# Bypass TPP signature validation
# bypass_tpp_signature_validation = false
## Reject Berlin Group Consents in status "received" after defined time in minutes
# berlin_group_outdated_consents_interval = 5
## Reject Berlin Group consents with status "received" after a defined time (in seconds)
# berlin_group_outdated_consents_time_in_seconds = 300
# berlin_group_outdated_consents_interval_in_seconds =
## Expire Berlin Group consents with status "valid"
# berlin_group_expired_consents_interval_in_seconds =
## Enable writing API metrics (which APIs are called) to RDBMS

View File

@ -5,6 +5,7 @@ import code.api.util.APIUtil
import code.consent.{ConsentStatus, MappedConsent}
import code.util.Helper.MdcLoggable
import com.openbankproject.commons.util.ApiVersion
import net.liftweb.common.Full
import net.liftweb.mapper.{By, By_<}
import java.util.concurrent.TimeUnit
@ -21,8 +22,22 @@ object ConsentScheduler extends MdcLoggable {
// Starts multiple scheduled tasks with different intervals
def startAll(): Unit = {
startTask(interval = 60, () => unfinishedBerlinGroupConsents()) // Runs every 60 sec
startTask(interval = 60, () => expiredBerlinGroupConsents(), 10) // Start 10 seconds after previous job
APIUtil.getPropsAsIntValue("berlin_group_outdated_consents_interval_in_seconds") match {
case Full(interval) if interval > 0 =>
val time = APIUtil.getPropsAsIntValue("berlin_group_outdated_consents_time_in_seconds", 300)
startTask(interval = interval, () => unfinishedBerlinGroupConsents(time)) // Runs periodically
case _ =>
logger.warn("|---> Skipping unfinishedBerlinGroupConsents task: berlin_group_outdated_consents_interval_in_seconds not set or invalid")
}
APIUtil.getPropsAsIntValue("berlin_group_expired_consents_interval_in_seconds") match {
case Full(interval) if interval > 0 =>
startTask(interval = interval, () => expiredBerlinGroupConsents(), 10) // Delay for 10 seconds
case _ =>
logger.warn("|---> Skipping expiredBerlinGroupConsents task: berlin_group_expired_consents_interval_in_seconds not set or invalid")
}
}
// Generic method to schedule a task
@ -37,21 +52,20 @@ object ConsentScheduler extends MdcLoggable {
}
// Calculate the timestamp 5 minutes ago
private val someMinutesAgo: Date = {
val minutes = APIUtil.getPropsAsIntValue("berlin_group_outdated_consents_interval", 5)
private def someSecondsAgo(seconds: Int): Date = {
val cal = Calendar.getInstance()
cal.add(Calendar.MINUTE, -minutes)
cal.add(Calendar.SECOND, -seconds)
cal.getTime
}
private def unfinishedBerlinGroupConsents(): Unit = {
private def unfinishedBerlinGroupConsents(seconds: Int): Unit = {
Try {
logger.debug("|---> Checking for outdated Berlin Group consents...")
val outdatedConsents = MappedConsent.findAll(
By(MappedConsent.mStatus, ConsentStatus.received.toString),
By(MappedConsent.mApiStandard, ApiVersion.berlinGroupV13.apiStandard),
By_<(MappedConsent.updatedAt, someMinutesAgo)
By_<(MappedConsent.updatedAt, someSecondsAgo(seconds))
)
logger.debug(s"|---> Found ${outdatedConsents.size} outdated consents")