Skip to content

Commit

Permalink
✨ Endpoint to stop a script execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysoun committed Mar 19, 2024
1 parent b39900f commit a464b35
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*

Expand All @@ -24,4 +28,24 @@ class ScriptExecutionGrpcClient(channel: ManagedChannel) : AbstractGrpcClient(ch
batchExecutionId = scriptBatchExecutionId.toString()
scriptChecksum = executedScriptChecksum
}).scriptExecutionId)
}

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
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface ScriptExecutionRepository {
scriptBatchExecutionId: UUID,
executedScriptChecksum: String
): UUID

suspend fun endScriptExecution(scriptExecutionId: UUID, scriptExecutionStopRequest: ScriptExecutionStopRequest)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ class ScriptExecutionService(
executedScriptChecksum = scriptChecksum
)
}

suspend fun endScriptExecution(scriptExecutionId: UUID, scriptExecutionStopRequest: ScriptExecutionStopRequest) {
return scriptExecutionRepository.endScriptExecution(
scriptExecutionId,
scriptExecutionStopRequest
)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -46,16 +47,31 @@ internal fun Route.executionsV1Routes(batchExecutionService: BatchExecutionServi
scriptExecutionId
))
}
post("/{executionId}/scripts/stop") {
put("/scripts/{scriptExecutionId}/stop") {
val scriptExecutionStop = call.receive<ScriptExecutionStop>()
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,
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
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) :
ScriptExecutionServiceGrpcKt.ScriptExecutionServiceCoroutineImplBase() {
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(),
Expand Down
17 changes: 17 additions & 0 deletions modules/proto/src/main/proto/scriptExecution_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proto;

service ScriptExecutionService {
rpc createScriptExecution(ScriptExecutionCreationRequest) returns (ScriptExecutionCreationResponse);
rpc stopScriptExecution(ScriptExecutionStopRequest) returns (ScriptExecutionStopResponse);
}

message ScriptExecutionCreationRequest {
Expand All @@ -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 {

}

0 comments on commit a464b35

Please sign in to comment.