diff --git a/modules/api-execution-report/api/src/main/kotlin/datamaintain/monitoring/api/execution/report/api/ScriptExecutionStop.kt b/modules/api-execution-report/api/src/main/kotlin/datamaintain/monitoring/api/execution/report/api/ScriptExecutionStop.kt index 1c0e50e..b2a8cfd 100644 --- a/modules/api-execution-report/api/src/main/kotlin/datamaintain/monitoring/api/execution/report/api/ScriptExecutionStop.kt +++ b/modules/api-execution-report/api/src/main/kotlin/datamaintain/monitoring/api/execution/report/api/ScriptExecutionStop.kt @@ -1,13 +1,14 @@ package datamaintain.monitoring.api.execution.report.api import kotlinx.serialization.Serializable +import java.time.Instant @Serializable data class ScriptExecutionStop( - val checksum: String, - val executionDurationInMillis: Long?, val executionStatus: ExecutionStatus, - val executionOutput: String? + val executionOutput: String?, + @Serializable(with = InstantSerializer::class) + val executionEndDate: Instant ) @Serializable diff --git a/modules/api-execution-report/client/src/main/kotlin/api/execution/report/client/ScriptExecutionGrpcClient.kt b/modules/api-execution-report/client/src/main/kotlin/api/execution/report/client/ScriptExecutionGrpcClient.kt index 7869de7..dc9be2b 100644 --- a/modules/api-execution-report/client/src/main/kotlin/api/execution/report/client/ScriptExecutionGrpcClient.kt +++ b/modules/api-execution-report/client/src/main/kotlin/api/execution/report/client/ScriptExecutionGrpcClient.kt @@ -1,9 +1,13 @@ package api.execution.report.client +import api.execution.report.domain.module.script.execution.ExecutionStatus import api.execution.report.domain.module.script.execution.ScriptExecutionRepository +import api.execution.report.domain.module.script.execution.ScriptExecutionStopRequest import io.grpc.ManagedChannel +import proto.ScriptExecutionApi import proto.ScriptExecutionServiceGrpcKt import proto.scriptExecutionCreationRequest +import proto.scriptExecutionStopRequest import java.time.Instant import java.util.* @@ -24,4 +28,24 @@ class ScriptExecutionGrpcClient(channel: ManagedChannel) : AbstractGrpcClient(ch batchExecutionId = scriptBatchExecutionId.toString() scriptChecksum = executedScriptChecksum }).scriptExecutionId) -} \ No newline at end of file + + override suspend fun endScriptExecution( + scriptExecutionId: UUID, + scriptExecutionStopRequest: ScriptExecutionStopRequest + ) { + stub.stopScriptExecution( + scriptExecutionStopRequest { + endDate = scriptExecutionStopRequest.executionEndDate.toTimestamp() + status = scriptExecutionStopRequest.executionStatus.toApi() + output = scriptExecutionStopRequest.executionOutput?: "" + executionId = scriptExecutionId.toString() + } + ) + } +} + +private fun ExecutionStatus.toApi(): ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus = + when(this) { + ExecutionStatus.OK -> ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.OK + ExecutionStatus.KO -> ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.KO + } diff --git a/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionRepository.kt b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionRepository.kt index 74281a9..590ae93 100644 --- a/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionRepository.kt +++ b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionRepository.kt @@ -11,4 +11,6 @@ interface ScriptExecutionRepository { scriptBatchExecutionId: UUID, executedScriptChecksum: String ): UUID + + suspend fun endScriptExecution(scriptExecutionId: UUID, scriptExecutionStopRequest: ScriptExecutionStopRequest) } \ No newline at end of file diff --git a/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionService.kt b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionService.kt index 0ba2cf7..0e1e07f 100644 --- a/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionService.kt +++ b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionService.kt @@ -20,4 +20,11 @@ class ScriptExecutionService( executedScriptChecksum = scriptChecksum ) } + + suspend fun endScriptExecution(scriptExecutionId: UUID, scriptExecutionStopRequest: ScriptExecutionStopRequest) { + return scriptExecutionRepository.endScriptExecution( + scriptExecutionId, + scriptExecutionStopRequest + ) + } } \ No newline at end of file diff --git a/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionStopRequest.kt b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionStopRequest.kt new file mode 100644 index 0000000..58a1407 --- /dev/null +++ b/modules/api-execution-report/domain/src/main/kotlin/api/execution/report/domain/module/script/execution/ScriptExecutionStopRequest.kt @@ -0,0 +1,14 @@ +package api.execution.report.domain.module.script.execution + +import java.time.Instant + +data class ScriptExecutionStopRequest( + val executionStatus: ExecutionStatus, + val executionOutput: String?, + val executionEndDate: Instant +) + +enum class ExecutionStatus { + OK, + KO +} \ No newline at end of file diff --git a/modules/api-execution-report/rest/src/main/kotlin/api/execution/report/rest/v1/route/ExecutionsRoute.kt b/modules/api-execution-report/rest/src/main/kotlin/api/execution/report/rest/v1/route/ExecutionsRoute.kt index 5792455..931abb0 100644 --- a/modules/api-execution-report/rest/src/main/kotlin/api/execution/report/rest/v1/route/ExecutionsRoute.kt +++ b/modules/api-execution-report/rest/src/main/kotlin/api/execution/report/rest/v1/route/ExecutionsRoute.kt @@ -1,6 +1,7 @@ package api.execution.report.rest.v1.route import api.execution.report.domain.module.batch.execution.BatchExecutionService +import api.execution.report.domain.module.script.execution.ExecutionStatus import api.execution.report.domain.module.script.execution.ScriptExecutionService import datamaintain.monitoring.api.execution.report.api.* import io.ktor.http.* @@ -46,16 +47,31 @@ internal fun Route.executionsV1Routes(batchExecutionService: BatchExecutionServi scriptExecutionId )) } - post("/{executionId}/scripts/stop") { + put("/scripts/{scriptExecutionId}/stop") { val scriptExecutionStop = call.receive() - val executionId = call.parameters.getOrFail("executionId").toInt() - call.respond(HttpStatusCode.OK).also { - println("Start script execution $scriptExecutionStop for batch $executionId") - } + val scriptExecutionId = UUID.fromString(call.parameters.getOrFail("scriptExecutionId")) + scriptExecutionService.endScriptExecution( + scriptExecutionId = scriptExecutionId, + scriptExecutionStopRequest = scriptExecutionStop.toDomain() + ) + call.respond(HttpStatusCode.OK) } } } +private fun ScriptExecutionStop.toDomain(): api.execution.report.domain.module.script.execution.ScriptExecutionStopRequest = + api.execution.report.domain.module.script.execution.ScriptExecutionStopRequest( + executionStatus = executionStatus.toDomain(), + executionOutput = executionOutput, + executionEndDate = executionEndDate + ) + +private fun datamaintain.monitoring.api.execution.report.api.ExecutionStatus.toDomain(): ExecutionStatus = + when (this) { + datamaintain.monitoring.api.execution.report.api.ExecutionStatus.OK -> ExecutionStatus.OK + datamaintain.monitoring.api.execution.report.api.ExecutionStatus.KO -> ExecutionStatus.KO + } + private fun ScriptExecutionStart.toDomain(): api.execution.report.domain.module.script.execution.ScriptExecutionStart = api.execution.report.domain.module.script.execution.ScriptExecutionStart( name = name, @@ -69,4 +85,4 @@ private fun BatchEndStatus.toDomain(): api.execution.report.domain.module.batch. when(this) { BatchEndStatus.COMPLETED -> api.execution.report.domain.module.batch.execution.BatchEndStatus.COMPLETED BatchEndStatus.ERROR -> api.execution.report.domain.module.batch.execution.BatchEndStatus.ERROR - } + } \ No newline at end of file diff --git a/modules/app-server/server/src/main/kotlin/ScriptExecutionGrpcServiceImpl.kt b/modules/app-server/server/src/main/kotlin/ScriptExecutionGrpcServiceImpl.kt index 3151ef6..c7b41d0 100644 --- a/modules/app-server/server/src/main/kotlin/ScriptExecutionGrpcServiceImpl.kt +++ b/modules/app-server/server/src/main/kotlin/ScriptExecutionGrpcServiceImpl.kt @@ -1,8 +1,8 @@ +import execution.Status import execution.script.ScriptExecutionCreationRequest +import execution.script.ScriptExecutionEndUpdateRequest import execution.script.ScriptExecutionService -import proto.ScriptExecutionApi -import proto.ScriptExecutionServiceGrpcKt -import proto.scriptExecutionCreationResponse +import proto.* import java.util.* class ScriptExecutionGrpcServiceImpl(private val scriptExecutionService: ScriptExecutionService) : @@ -10,8 +10,32 @@ class ScriptExecutionGrpcServiceImpl(private val scriptExecutionService: ScriptE override suspend fun createScriptExecution(request: ScriptExecutionApi.ScriptExecutionCreationRequest): ScriptExecutionApi.ScriptExecutionCreationResponse { return scriptExecutionCreationResponse { scriptExecutionId = scriptExecutionService.insert(request.toScriptExecutionCreationRequest()).id.toString() } } + + override suspend fun stopScriptExecution(request: ScriptExecutionApi.ScriptExecutionStopRequest): ScriptExecutionApi.ScriptExecutionStopResponse { + scriptExecutionService.updateScriptExecutionEndData( + scriptExecutionId = UUID.fromString(request.executionId), + executionEndData = request.toScriptExecutionEndUpdateRequest() + ) + return scriptExecutionStopResponse { } + } } +private fun ScriptExecutionApi.ScriptExecutionStopRequest.toScriptExecutionEndUpdateRequest(): ScriptExecutionEndUpdateRequest = ScriptExecutionEndUpdateRequest( + endDate = endDate.toInstant(), + output = output, + status = status.toDomain() + ) + +private fun ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.toDomain(): Status = + when(this) { + ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.OK -> Status.COMPLETED + ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.KO -> Status.ERROR + ScriptExecutionApi.ScriptExecutionStopRequest.ExecutionStatus.UNRECOGNIZED -> throw UnrecognizedStatusException + } + +object UnrecognizedStatusException : IllegalArgumentException("Given status was not recognized") + + private fun ScriptExecutionApi.ScriptExecutionCreationRequest.toScriptExecutionCreationRequest(): ScriptExecutionCreationRequest { return ScriptExecutionCreationRequest( startDate = startDate.toInstant(), diff --git a/modules/proto/src/main/proto/scriptExecution_api.proto b/modules/proto/src/main/proto/scriptExecution_api.proto index bf163cb..4e86a09 100644 --- a/modules/proto/src/main/proto/scriptExecution_api.proto +++ b/modules/proto/src/main/proto/scriptExecution_api.proto @@ -6,6 +6,7 @@ package proto; service ScriptExecutionService { rpc createScriptExecution(ScriptExecutionCreationRequest) returns (ScriptExecutionCreationResponse); + rpc stopScriptExecution(ScriptExecutionStopRequest) returns (ScriptExecutionStopResponse); } message ScriptExecutionCreationRequest { @@ -18,4 +19,20 @@ message ScriptExecutionCreationRequest { message ScriptExecutionCreationResponse { string scriptExecutionId = 1; +} + +message ScriptExecutionStopRequest { + google.protobuf.Timestamp endDate = 1; + string output = 2; + ExecutionStatus status = 3; + string executionId = 4; + + enum ExecutionStatus { + OK = 0; + KO = 1; + } +} + +message ScriptExecutionStopResponse { + } \ No newline at end of file