Skip to content

Commit

Permalink
feat: allow to prune old data periodically (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
crm-dhu authored Jan 21, 2025
1 parent 915dbf1 commit dead594
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions orchard-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ com.salesforce.mce.orchard {
resource {
reAttemptDelay = 10 seconds
}

// Configure the default TTL for all workflows
workflow {
ttl = 30 days
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class OrchardSettings private (config: Config) {

val resourceReattemptDelayPolicy = delayPolicy(config, "resource.reAttemptDelay")

val workflowTtl = config.getDuration("workflow.ttl")

}

object OrchardSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package com.salesforce.mce.orchard.db

import java.time.{LocalDate, LocalDateTime}
import java.time.{Duration, LocalDate, LocalDateTime}

import slick.jdbc.GetResult
import slick.jdbc.PostgresProfile.api._
Expand Down Expand Up @@ -105,6 +105,11 @@ object WorkflowQuery {
.result
}

def pruneOldData(ttl: Duration): DBIO[Int] = {
val cutoffTime = LocalDateTime.now().minus(ttl)
WorkflowTable().filter(r => r.createdAt < cutoffTime).delete
}

def filterByStatus(status: Status.Value): DBIO[Seq[WorkflowTable.R]] =
WorkflowTable().filter(r => r.status === status).result

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ object OrchardSystem {
val CancelingScanDelay = 10.seconds
val HeartBeatDelay = 10.seconds
val CheckAdoptionDelay = 1.minute
val PruneDelay = 1.hour

sealed trait Msg
case class ActivateMsg(workflowId: String) extends Msg
private case object ScanCanceling extends Msg
private case class WorkflowTerminated(workflowId: String) extends Msg
private case object HeartBeat extends Msg
private case object AdoptOrphanWorkflows extends Msg
private case object PruneWorkflows extends Msg

def apply(database: OrchardDatabase, orchardSettings: OrchardSettings): Behavior[Msg] = Behaviors.setup { ctx =>
Behaviors.withTimers { timers =>
timers.startSingleTimer(ScanCanceling, CancelingScanDelay)
timers.startSingleTimer(HeartBeat, HeartBeatDelay)
timers.startSingleTimer(AdoptOrphanWorkflows, CheckAdoptionDelay)
timers.startSingleTimer(PruneWorkflows, PruneDelay)
val managerId =
s"os-${InetAddress.getLocalHost().getHostName()}-${UUID.randomUUID()}".take(64)
apply(
Expand Down Expand Up @@ -126,6 +129,11 @@ object OrchardSystem {
timers.startSingleTimer(AdoptOrphanWorkflows, CheckAdoptionDelay)
Behaviors.same

case PruneWorkflows =>
ctx.log.info(s"${ctx.self} Received PrueWorkflows")
database.sync(WorkflowQuery.pruneOldData(orchardSettings.workflowTtl))
timers.startSingleTimer(PruneWorkflows, PruneDelay)
Behaviors.same
}
.receiveSignal { case (_, signal) => // log PreRestart
ctx.log.info(s"${ctx.self} receiveSignal ${signal.toString}")
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ThisBuild / version := "0.24.0"
ThisBuild / version := "0.25.0"

0 comments on commit dead594

Please sign in to comment.