-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2291 from hongwei1/develop
feature/added the hikariCP properties file and new DataBaseCleanerScheduler
- Loading branch information
Showing
17 changed files
with
114 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
obp-api/src/main/scala/code/scheduler/DataBaseCleanerScheduler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package code.scheduler | ||
|
||
import code.actorsystem.ObpLookupSystem | ||
import code.api.Constant | ||
import code.api.util.APIUtil.generateUUID | ||
import code.api.util.APIUtil | ||
import code.nonce.Nonces | ||
import code.util.Helper.MdcLoggable | ||
import net.liftweb.common.Full | ||
import net.liftweb.mapper.{By, By_<=} | ||
|
||
import java.util.concurrent.TimeUnit | ||
import java.util.Date | ||
import scala.concurrent.duration._ | ||
import code.token.Tokens | ||
|
||
|
||
object DataBaseCleanerScheduler extends MdcLoggable { | ||
|
||
private lazy val actorSystem = ObpLookupSystem.obpLookupSystem | ||
implicit lazy val executor = actorSystem.dispatcher | ||
private lazy val scheduler = actorSystem.scheduler | ||
private val oneDayInMillis: Long = 86400000 | ||
//in scala DataBaseCleanerScheduler.getClass.getSimpleName ==> DataBaseCleanerScheduler$ | ||
private val jobName = DataBaseCleanerScheduler.getClass.getSimpleName.replace("$", "") | ||
private val apiInstanceId = Constant.ApiInstanceId | ||
|
||
def start(intervalInSeconds: Long): Unit = { | ||
logger.info(s"Hello from $jobName.start") | ||
|
||
logger.info(s"--------- Clean up Jobs ---------") | ||
logger.info(s"Delete all Jobs created by api_instance_id=$apiInstanceId") | ||
JobScheduler.findAll(By(JobScheduler.Name, apiInstanceId)).map { i => | ||
logger.info(s"Job name: ${i.name}, Date: ${i.createdAt}") | ||
i | ||
}.map(_.delete_!) | ||
logger.info(s"Delete all Jobs older than 5 days") | ||
val fiveDaysAgo: Date = new Date(new Date().getTime - (oneDayInMillis * 5)) | ||
JobScheduler.findAll(By_<=(JobScheduler.createdAt, fiveDaysAgo)).map { i => | ||
logger.info(s"Job name: ${i.name}, Date: ${i.createdAt}, api_instance_id: ${apiInstanceId}") | ||
i | ||
}.map(_.delete_!) | ||
|
||
scheduler.schedule( | ||
initialDelay = Duration(intervalInSeconds, TimeUnit.SECONDS), | ||
interval = Duration(intervalInSeconds, TimeUnit.SECONDS), | ||
runnable = new Runnable { | ||
def run(): Unit = { | ||
JobScheduler.find(By(JobScheduler.Name, jobName)) match { | ||
case Full(job) => // There is an ongoing/hanging job | ||
logger.info(s"Cannot start $jobName.start.run due to ongoing job. Job ID: ${job.JobId}") | ||
case _ => // Start a new job | ||
val uniqueId = generateUUID() | ||
val job = JobScheduler.create | ||
.JobId(uniqueId) | ||
.Name(jobName) | ||
.ApiInstanceId(apiInstanceId) | ||
.saveMe() | ||
logger.info(s"Starting $jobName.Job ID: $uniqueId") | ||
deleteExpiredTokensAndNonces() | ||
JobScheduler.delete_!(job) // Allow future jobs | ||
logger.info(s"End of $jobName.Job ID: $uniqueId") | ||
} | ||
} | ||
} | ||
) | ||
logger.info(s"Bye from $jobName.start") | ||
} | ||
|
||
def deleteExpiredTokensAndNonces() = { | ||
//looks for expired tokens and nonces and deletes them | ||
val currentDate = new Date() | ||
//delete expired tokens and nonces | ||
Tokens.tokens.vend.deleteExpiredTokens(currentDate) | ||
Nonces.nonces.vend.deleteExpiredNonces(currentDate) | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters