Added policy
This commit is contained in:
1 parent
f4d6b1f87d
commit
c2b10eb85c
7 files changed
+107
-8
No files matched your search
@@ -0,0 +1,11 @@
|
||||
package no.iktdev.eventi.tasks
|
||||
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
object DefaultTaskPolicy : TaskPolicy() {
|
||||
override fun heartbeatInterval(): Duration = 5.minutes
|
||||
override fun abandonTimeout(): Duration = 10.minutes
|
||||
override fun cleanupInterval(): Duration = 7.days
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package no.iktdev.eventi.tasks
|
||||
|
||||
object GlobalTaskPolicy {
|
||||
private var _policy: TaskPolicy? = null
|
||||
|
||||
val policy: TaskPolicy
|
||||
get() = _policy ?: DefaultTaskPolicy
|
||||
|
||||
fun set(policy: TaskPolicy): PolicyValidationResult {
|
||||
val result = policy.validate()
|
||||
|
||||
if (!result.isValid) {
|
||||
// Ikke sett policyen hvis den er ugyldig
|
||||
return result
|
||||
}
|
||||
|
||||
_policy = policy
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package no.iktdev.eventi.tasks
|
||||
|
||||
data class PolicyValidationResult(
|
||||
val isValid: Boolean,
|
||||
val errors: List<String>,
|
||||
val warnings: List<String>
|
||||
)
|
||||
@@ -15,8 +15,6 @@ import no.iktdev.eventi.registry.TaskListenerRegistry
|
||||
import org.jetbrains.annotations.VisibleForTesting
|
||||
import java.util.UUID
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
/**
|
||||
* Abstract base class for handling tasks with asynchronous processing and reporting.
|
||||
@@ -52,7 +50,8 @@ abstract class TaskListener(val taskType: TaskType = TaskType.CPU_INTENSIVE): Ta
|
||||
private val heartbeatScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
@VisibleForTesting
|
||||
internal var heartbeatRunner: Job? = null
|
||||
fun withHeartbeatRunner(interval: Duration = 5.minutes, block: () -> Unit): Job {
|
||||
fun withHeartbeatRunner(block: () -> Unit): Job {
|
||||
val interval = GlobalTaskPolicy.policy.heartbeatInterval()
|
||||
return heartbeatScope.launch {
|
||||
while (isActive) {
|
||||
block()
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package no.iktdev.eventi.tasks
|
||||
|
||||
import mu.KotlinLogging
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
abstract class TaskPolicy {
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
abstract fun heartbeatInterval(): Duration
|
||||
abstract fun abandonTimeout(): Duration
|
||||
abstract fun cleanupInterval(): Duration
|
||||
|
||||
// Minstegrenser
|
||||
open fun minHeartbeat() = 5.seconds
|
||||
open fun minTimeout() = 1.minutes
|
||||
open fun minCleanup() = 5.minutes
|
||||
|
||||
fun validate(): PolicyValidationResult {
|
||||
val hb = heartbeatInterval()
|
||||
val timeout = abandonTimeout()
|
||||
val cleanup = cleanupInterval()
|
||||
|
||||
val errors = mutableListOf<String>()
|
||||
val warnings = mutableListOf<String>()
|
||||
|
||||
// Hard errors
|
||||
if (hb < minHeartbeat())
|
||||
errors += "Heartbeat interval $hb is below minimum ${minHeartbeat()}"
|
||||
|
||||
if (timeout < minTimeout())
|
||||
errors += "Abandon timeout $timeout is below minimum ${minTimeout()}"
|
||||
|
||||
if (hb >= timeout)
|
||||
errors += "Heartbeat interval $hb must be smaller than abandon timeout $timeout"
|
||||
|
||||
if (cleanup < minCleanup())
|
||||
errors += "Cleanup interval $cleanup is below minimum ${minCleanup()}"
|
||||
|
||||
// Soft warnings
|
||||
if (hb < 30.seconds)
|
||||
warnings += "Heartbeat interval $hb is very low and may cause unnecessary load."
|
||||
|
||||
if (timeout < 3.minutes)
|
||||
warnings += "Abandon timeout $timeout is very low and may release active tasks prematurely."
|
||||
|
||||
if (cleanup < 10.minutes)
|
||||
warnings += "Cleanup interval $cleanup is very low and may delete files too early."
|
||||
|
||||
// Log warnings (optional)
|
||||
warnings.forEach { log.warn(it) }
|
||||
|
||||
return PolicyValidationResult(
|
||||
isValid = errors.isEmpty(),
|
||||
errors = errors,
|
||||
warnings = warnings
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class TaskListenerTest {
|
||||
override suspend fun onTask(task: Task): Event? {
|
||||
onTaskCalled = true
|
||||
|
||||
withHeartbeatRunner(10.milliseconds) {
|
||||
withHeartbeatRunner() {
|
||||
heartbeatRan = true
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ class TaskListenerTest {
|
||||
override fun supports(task: Task) = true
|
||||
|
||||
override suspend fun onTask(task: Task): Event {
|
||||
withHeartbeatRunner(10.milliseconds) {
|
||||
withHeartbeatRunner() {
|
||||
heartbeatRan = true
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ class TaskListenerTest {
|
||||
override fun supports(task: Task) = true
|
||||
|
||||
override suspend fun onTask(task: Task): Event? {
|
||||
withHeartbeatRunner(10.milliseconds) {
|
||||
withHeartbeatRunner() {
|
||||
heartbeatRan = true
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ class TaskListenerTest {
|
||||
override suspend fun onTask(task: Task): Event {
|
||||
onTaskCalled = true
|
||||
|
||||
withHeartbeatRunner(10.milliseconds) {
|
||||
withHeartbeatRunner() {
|
||||
heartbeatRan = true
|
||||
}
|
||||
|
||||
|
||||
@@ -88,7 +88,7 @@ class TaskPollerImplementationTest : TestBase() {
|
||||
override fun supports(task: Task) = task is EchoTask
|
||||
|
||||
override suspend fun onTask(task: Task): Event {
|
||||
withHeartbeatRunner(1.seconds) {
|
||||
withHeartbeatRunner() {
|
||||
println("Heartbeat")
|
||||
}
|
||||
if (task is EchoTask) {
|
||||
|
||||
Reference in new issue
Block a user