Skip to content

Commit

Permalink
✨ Endpoint to stop batch execution in API
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysoun committed Mar 5, 2024
1 parent 36f1c79 commit c2efe9a
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package datamaintain.monitoring.api.execution.report.api

import kotlinx.serialization.Serializable
import java.time.Instant

@Serializable
data class ExecutionStopRequest(
@Serializable(with = InstantSerializer::class)
val endDate: Instant,
val batchEndStatus: BatchEndStatus
)

enum class BatchEndStatus {
COMPLETED,
ERROR
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package api.execution.report.client

import api.execution.report.domain.module.batch.execution.BatchEndStatus
import api.execution.report.domain.module.batch.execution.BatchExecutionRepository
import io.grpc.ManagedChannel
import proto.BatchExecutionApi
import proto.BatchExecutionServiceGrpcKt
import proto.batchExecutionCreationRequest
import proto.batchExecutionStopRequest
import java.time.Instant
import java.util.*

private fun BatchEndStatus.toApi(): BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus =
when (this) {
BatchEndStatus.COMPLETED -> BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.COMPLETED
BatchEndStatus.ERROR -> BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.ERROR
}

class BatchExecutionGrpcClient(channel: ManagedChannel) : AbstractGrpcClient(channel), BatchExecutionRepository{
private val stub = BatchExecutionServiceGrpcKt.BatchExecutionServiceCoroutineStub(channel)

Expand All @@ -25,4 +33,17 @@ class BatchExecutionGrpcClient(channel: ManagedChannel) : AbstractGrpcClient(cha
}
return UUID.fromString(stub.createBatchExecution(request).id)
}

override suspend fun stopBatchExecution(
executionId: UUID,
batchExecutionEndDate: Instant,
endStatusValue: BatchEndStatus
) {
val request = batchExecutionStopRequest {
batchExecutionId = executionId.toString()
endStatus = endStatusValue.toApi()
endDate = batchExecutionEndDate.toTimestamp()
}
stub.stopBatchExecution(request)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package api.execution.report.domain.module.batch.execution

enum class BatchEndStatus {
COMPLETED,
ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface BatchExecutionRepository {
batchExecutionModuleRef: UUID,
batchExecutionEnvironmentRef: UUID,
): UUID

suspend fun stopBatchExecution(executionId: UUID, batchExecutionEndDate: Instant, endStatusValue: BatchEndStatus)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ class BatchExecutionService(
batchExecutionEnvironmentRef = moduleEnvironmentToken.environmentRef
)
}

suspend fun stopBatchExecution(executionId: UUID, batchExecutionEndDate: Instant, endStatusValue: BatchEndStatus) {
batchExecutionRepository.stopBatchExecution(executionId, batchExecutionEndDate, endStatusValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.util.*
import java.util.*

internal fun Route.executionsV1Routes(batchExecutionService: BatchExecutionService) {
route("/executions") {
Expand All @@ -23,8 +25,13 @@ internal fun Route.executionsV1Routes(batchExecutionService: BatchExecutionServi
}
put("/stop/{executionId}") {
call.respond(HttpStatusCode.OK).also {
val executionId = call.parameters["executionId"]!!.toInt()
println("Execution with id $executionId has ended, it created report ${MonitoringReport(executionId)}}")
val executionId = UUID.fromString(call.parameters.getOrFail("executionId"))
val request = call.receive<ExecutionStopRequest>()
batchExecutionService.stopBatchExecution(
executionId,
request.endDate,
request.batchEndStatus.toDomain()
)
}
}
post("/{executionId}/scripts/start") {
Expand All @@ -42,4 +49,10 @@ internal fun Route.executionsV1Routes(batchExecutionService: BatchExecutionServi
}
}
}
}
}

private fun BatchEndStatus.toDomain(): api.execution.report.domain.module.batch.execution.BatchEndStatus =
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,10 +1,9 @@
import execution.batch.BatchExecutionCreationRequest
import execution.batch.BatchExecutionService
import execution.batch.Origin
import execution.batch.Type
import execution.Status
import execution.batch.*
import proto.BatchExecutionApi
import proto.BatchExecutionServiceGrpcKt
import proto.batchExecutionCreationResponse
import proto.batchExecutionStopResponse
import java.util.*

class BatchExecutionGrpcServiceImpl(private val batchExecutionService: BatchExecutionService) :
Expand All @@ -15,8 +14,28 @@ class BatchExecutionGrpcServiceImpl(private val batchExecutionService: BatchExec
batchExecutionCreationResponse {
id = batchExecutionService.insert(request.toDomain()).id.toString()
}

override suspend fun stopBatchExecution(request: BatchExecutionApi.BatchExecutionStopRequest): BatchExecutionApi.BatchExecutionStopResponse {
batchExecutionService.updateBatchExecutionEndData(
UUID.fromString(request.batchExecutionId),
BatchExecutionEndUpdateRequest(
request.endDate.toInstant(),
request.endStatus.toDomain()
)
)
return batchExecutionStopResponse { }
}
}

private fun BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.toDomain(): Status =
when(this) {
BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.ERROR -> Status.ERROR
BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.COMPLETED -> Status.COMPLETED
BatchExecutionApi.BatchExecutionStopRequest.BatchExecutionEndStatus.UNRECOGNIZED -> throw UnrecognizedBatchExecutionEndStatus()
}

class UnrecognizedBatchExecutionEndStatus: IllegalArgumentException("Given batch execution end status was not recognized")

private fun BatchExecutionApi.BatchExecutionCreationRequest.toDomain(): BatchExecutionCreationRequest =
BatchExecutionCreationRequest(
startDate = startDate.toInstant(),
Expand Down
16 changes: 16 additions & 0 deletions modules/proto/src/main/proto/batchExecution_api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package proto;

service BatchExecutionService {
rpc createBatchExecution(BatchExecutionCreationRequest) returns (BatchExecutionCreationResponse) {}
rpc stopBatchExecution(BatchExecutionStopRequest) returns (BatchExecutionStopResponse) {}
}

message BatchExecutionCreationRequest {
Expand All @@ -29,4 +30,19 @@ message BatchExecutionCreationRequest {

message BatchExecutionCreationResponse {
string id = 1;
}

message BatchExecutionStopRequest {
string batchExecutionId = 1;
BatchExecutionEndStatus endStatus = 2;
google.protobuf.Timestamp endDate = 3;

enum BatchExecutionEndStatus {
COMPLETED = 0;
ERROR = 1;
}
}

message BatchExecutionStopResponse {

}

0 comments on commit c2efe9a

Please sign in to comment.