use of io.temporal.failure.CanceledFailure in project sdk-java by temporalio.
the class ChildWorkflowStateMachine method cancelStartChildCommand.
private void cancelStartChildCommand() {
cancelCommand();
RuntimeException failure = new ChildWorkflowFailure(0, 0, workflowType, WorkflowExecution.newBuilder().setWorkflowId(workflowId).build(), namespace, RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, new CanceledFailure("Child immediately canceled", null, null));
completionCallback.apply(Optional.empty(), failure);
}
use of io.temporal.failure.CanceledFailure in project sdk-java by temporalio.
the class WorkflowExecutionUtils method getResultFromCloseEvent.
public static Optional<Payloads> getResultFromCloseEvent(WorkflowExecution workflowExecution, Optional<String> workflowType, HistoryEvent closeEvent, DataConverter converter) {
if (closeEvent == null) {
throw new IllegalStateException("Workflow is still running");
}
switch(closeEvent.getEventType()) {
case EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED:
WorkflowExecutionCompletedEventAttributes completedEventAttributes = closeEvent.getWorkflowExecutionCompletedEventAttributes();
if (completedEventAttributes.hasResult()) {
return Optional.of(completedEventAttributes.getResult());
}
return Optional.empty();
case EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED:
String message = null;
WorkflowExecutionCanceledEventAttributes attributes = closeEvent.getWorkflowExecutionCanceledEventAttributes();
Optional<Payloads> details = attributes.hasDetails() ? Optional.of(attributes.getDetails()) : Optional.empty();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, new CanceledFailure("Workflow canceled", new EncodedValues(details, converter), null));
case EVENT_TYPE_WORKFLOW_EXECUTION_FAILED:
WorkflowExecutionFailedEventAttributes failed = closeEvent.getWorkflowExecutionFailedEventAttributes();
throw new WorkflowExecutionFailedException(failed.getFailure(), failed.getWorkflowTaskCompletedEventId(), failed.getRetryState());
case EVENT_TYPE_WORKFLOW_EXECUTION_TERMINATED:
WorkflowExecutionTerminatedEventAttributes terminated = closeEvent.getWorkflowExecutionTerminatedEventAttributes();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, RetryState.RETRY_STATE_NON_RETRYABLE_FAILURE, new TerminatedFailure(terminated.getReason(), null));
case EVENT_TYPE_WORKFLOW_EXECUTION_TIMED_OUT:
WorkflowExecutionTimedOutEventAttributes timedOut = closeEvent.getWorkflowExecutionTimedOutEventAttributes();
throw new WorkflowFailedException(workflowExecution, workflowType.orElse(null), 0, timedOut.getRetryState(), new TimeoutFailure(null, null, TimeoutType.TIMEOUT_TYPE_START_TO_CLOSE));
default:
throw new RuntimeException("Workflow end state is not completed: " + WorkflowExecutionUtils.prettyPrintObject(closeEvent));
}
}
use of io.temporal.failure.CanceledFailure in project sdk-java by temporalio.
the class SyncWorkflowContext method signalExternalWorkflow.
@Override
public SignalExternalOutput signalExternalWorkflow(SignalExternalInput input) {
SignalExternalWorkflowExecutionCommandAttributes.Builder attributes = SignalExternalWorkflowExecutionCommandAttributes.newBuilder();
attributes.setSignalName(input.getSignalName());
attributes.setExecution(input.getExecution());
Optional<Payloads> payloads = getDataConverter().toPayloads(input.getArgs());
payloads.ifPresent(attributes::setInput);
CompletablePromise<Void> result = Workflow.newPromise();
Functions.Proc1<Exception> cancellationCallback = context.signalExternalWorkflowExecution(attributes, (output, failure) -> {
if (failure != null) {
runner.executeInWorkflowThread("child workflow failure callback", () -> result.completeExceptionally(FailureConverter.failureToException(failure, getDataConverter())));
} else {
runner.executeInWorkflowThread("child workflow completion callback", () -> result.complete(output));
}
});
CancellationScope.current().getCancellationRequest().thenApply((reason) -> {
cancellationCallback.apply(new CanceledFailure(reason));
return null;
});
return new SignalExternalOutput(result);
}
use of io.temporal.failure.CanceledFailure in project sdk-java by temporalio.
the class SyncWorkflowContext method executeActivityOnce.
private Promise<Optional<Payloads>> executeActivityOnce(String name, ActivityOptions options, Header header, Optional<Payloads> input) {
ActivityCallback callback = new ActivityCallback();
ExecuteActivityParameters params = constructExecuteActivityParameters(name, options, header, input);
Functions.Proc1<Exception> cancellationCallback = context.scheduleActivityTask(params, callback::invoke);
CancellationScope.current().getCancellationRequest().thenApply((reason) -> {
cancellationCallback.apply(new CanceledFailure(reason));
return null;
});
return callback.result;
}
use of io.temporal.failure.CanceledFailure in project sdk-java by temporalio.
the class SyncWorkflowContext method newTimer.
@Override
public Promise<Void> newTimer(Duration delay) {
CompletablePromise<Void> p = Workflow.newPromise();
Functions.Proc1<RuntimeException> cancellationHandler = context.newTimer(delay, (e) -> runner.executeInWorkflowThread("timer-callback", () -> {
if (e == null) {
p.complete(null);
} else {
p.completeExceptionally(e);
}
}));
CancellationScope.current().getCancellationRequest().thenApply((r) -> {
cancellationHandler.apply(new CanceledFailure(r));
return r;
});
return p;
}
Aggregations