Skip to content

Commit

Permalink
Don't attempt to parse workflow if it doesn't exist
Browse files Browse the repository at this point in the history
Signed-off-by: Chase Engelbrecht <[email protected]>
  • Loading branch information
engechas committed Dec 21, 2023
1 parent 903a6d9 commit 6ac125c
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import org.opensearch.index.reindex.DeleteByQueryRequestBuilder
import org.opensearch.search.builder.SearchSourceBuilder
import org.opensearch.tasks.Task
import org.opensearch.transport.TransportService
import java.util.Optional

private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO)
/**
Expand Down Expand Up @@ -119,7 +120,12 @@ class TransportDeleteWorkflowAction @Inject constructor(
) {
suspend fun resolveUserAndStart() {
try {
val workflow = getWorkflow()
val optionalWorkflow = getWorkflow()
if (optionalWorkflow.isEmpty) {
return
}

val workflow = optionalWorkflow.get()

val canDelete = user == null ||
!doFilterForUser(user) ||
Expand Down Expand Up @@ -296,17 +302,27 @@ class TransportDeleteWorkflowAction @Inject constructor(
return deletableMonitors
}

private suspend fun getWorkflow(): Workflow {
private suspend fun getWorkflow(): Optional<Workflow> {
val getRequest = GetRequest(ScheduledJob.SCHEDULED_JOBS_INDEX, workflowId)

val getResponse: GetResponse = client.suspendUntil { get(getRequest, it) }
if (getResponse.isExists == false) {
actionListener.onFailure(
AlertingException.wrap(
OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND)
)
)
if (!getResponse.isExists) {
handleWorkflowMissing()
return Optional.empty()
}

return Optional.of(parseWorkflow(getResponse))
}

private fun handleWorkflowMissing() {
actionListener.onFailure(
AlertingException.wrap(
OpenSearchStatusException("Workflow not found.", RestStatus.NOT_FOUND)
)
)
}

private fun parseWorkflow(getResponse: GetResponse): Workflow {
val xcp = XContentHelper.createParser(
xContentRegistry, LoggingDeprecationHandler.INSTANCE,
getResponse.sourceAsBytesRef, XContentType.JSON
Expand Down

0 comments on commit 6ac125c

Please sign in to comment.