Skip to content

Commit ba0a625

Browse files
committed
feat(tasks): Write a status log for DeleteOldOrtRunsTask
Generate a log for the task execution including the number of processed runs that can be evaluated by monitoring tools. Signed-off-by: Oliver Heger <oliver.heger@bosch.com>
1 parent 6298e98 commit ba0a625

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

tasks/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ dependencies {
5050

5151
implementation(libs.koinCore)
5252
implementation(libs.kotlinxCoroutines)
53+
implementation(libs.kotlinxSerializationJson)
5354
implementation(libs.kubernetesClient)
5455
implementation(libs.logback)
5556
implementation(libs.typesafeConfig)
@@ -61,6 +62,7 @@ dependencies {
6162
runtimeOnly(platform(projects.transport))
6263

6364
testImplementation(testFixtures(projects.dao))
65+
testImplementation(testFixtures(projects.utils.logging))
6466
testImplementation(testFixtures(projects.transport.transportSpi))
6567

6668
testImplementation(libs.koinTest)

tasks/src/main/kotlin/impl/DeleteOldOrtRunsTask.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ import kotlin.time.Clock
2323
import kotlin.time.Duration.Companion.days
2424
import kotlin.time.Instant
2525

26+
import kotlinx.serialization.json.JsonPrimitive
27+
2628
import org.eclipse.apoapsis.ortserver.config.ConfigManager
2729
import org.eclipse.apoapsis.ortserver.services.ortrun.OrtRunService
2830
import org.eclipse.apoapsis.ortserver.tasks.Task
31+
import org.eclipse.apoapsis.ortserver.utils.logging.JobStatusLogging
2932

3033
import org.slf4j.LoggerFactory
3134

@@ -46,6 +49,8 @@ class DeleteOldOrtRunsTask(
4649
private val ortRunMaxAgeThreshold: Instant
4750
) : Task {
4851
companion object {
52+
const val TASK_NAME = "deleteOldOrtRunsTask"
53+
4954
private val logger = LoggerFactory.getLogger(DeleteOldOrtRunsTask::class.java)
5055

5156
/**
@@ -59,8 +64,13 @@ class DeleteOldOrtRunsTask(
5964
}
6065

6166
override suspend fun execute() {
62-
logger.info("Deleting ORT runs that finished before $ortRunMaxAgeThreshold.")
67+
JobStatusLogging.runWithStatusLogging(TASK_NAME) { jsonBuilder ->
68+
logger.info("Deleting ORT runs that finished before $ortRunMaxAgeThreshold.")
6369

64-
ortRunService.deleteRunsCreatedBefore(ortRunMaxAgeThreshold)
70+
val result = ortRunService.deleteRunsCreatedBefore(ortRunMaxAgeThreshold)
71+
72+
jsonBuilder.put(JobStatusLogging.PROCESSED_COUNT_KEY, JsonPrimitive(result.totalCount))
73+
jsonBuilder.put(JobStatusLogging.FAILED_COUNT_KEY, JsonPrimitive(result.failedCount))
74+
}
6575
}
6676
}

tasks/src/test/kotlin/impl/DeleteOldOrtRunsTaskTest.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.typesafe.config.ConfigFactory
2424
import io.kotest.core.spec.style.StringSpec
2525
import io.kotest.extensions.system.withEnvironment
2626
import io.kotest.matchers.comparables.shouldBeLessThan
27+
import io.kotest.matchers.shouldBe
2728

2829
import io.mockk.coEvery
2930
import io.mockk.coVerify
@@ -35,11 +36,18 @@ import kotlin.time.Clock
3536
import kotlin.time.Duration.Companion.days
3637
import kotlin.time.Instant
3738

39+
import kotlinx.serialization.json.int
40+
import kotlinx.serialization.json.jsonPrimitive
41+
3842
import org.eclipse.apoapsis.ortserver.config.ConfigManager
3943
import org.eclipse.apoapsis.ortserver.model.util.ProcessingResult
4044
import org.eclipse.apoapsis.ortserver.services.ortrun.OrtRunService
45+
import org.eclipse.apoapsis.ortserver.utils.logging.JobStatusLogging
46+
import org.eclipse.apoapsis.ortserver.utils.logging.StatusLoggingTestExtension
4147

4248
class DeleteOldOrtRunsTaskTest : StringSpec({
49+
val logExtension = extension(StatusLoggingTestExtension())
50+
4351
"Old ORT runs should be deleted according to the configured retention policy" {
4452
val ortRunRetentionDays = 77
4553
val configMap = mapOf("dataRetention.ortRunDays" to ortRunRetentionDays.toString())
@@ -67,14 +75,32 @@ class DeleteOldOrtRunsTaskTest : StringSpec({
6775
checkDeleteInvocation(ortRunService, ortRunRetentionDays)
6876
}
6977
}
78+
79+
"A status log entry should be created for the execution of the task" {
80+
val configMap = mapOf("dataRetention.ortRunDays" to "100")
81+
val configManager = ConfigManager.create(ConfigFactory.parseMap(configMap))
82+
val ortRunService = createOrtRunService()
83+
84+
val task = DeleteOldOrtRunsTask.create(configManager, ortRunService)
85+
task.execute()
86+
87+
val status = logExtension.statusLog()
88+
89+
status[JobStatusLogging.STATUS_KEY]?.jsonPrimitive?.content shouldBe JobStatusLogging.STATUS_SUCCESS
90+
status[JobStatusLogging.PROCESSED_COUNT_KEY]?.jsonPrimitive?.int shouldBe deletionResult.totalCount
91+
status[JobStatusLogging.FAILED_COUNT_KEY]?.jsonPrimitive?.int shouldBe deletionResult.failedCount
92+
}
7093
})
7194

95+
/** The result to be returned by the [OrtRunService] mock when it is invoked to delete old ORT runs. */
96+
private val deletionResult = ProcessingResult(16, 1)
97+
7298
/**
7399
* Create a mock for [OrtRunService] and prepare it to expect a call to delete old ORT runs.
74100
*/
75101
private fun createOrtRunService(): OrtRunService =
76102
mockk<OrtRunService> {
77-
coEvery { deleteRunsCreatedBefore(any()) } returns ProcessingResult(10, 0)
103+
coEvery { deleteRunsCreatedBefore(any()) } returns deletionResult
78104
}
79105

80106
/**

0 commit comments

Comments
 (0)