Search in sources :

Example 1 with CanceledFailure

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);
}
Also used : ChildWorkflowFailure(io.temporal.failure.ChildWorkflowFailure) CanceledFailure(io.temporal.failure.CanceledFailure)

Example 2 with CanceledFailure

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));
    }
}
Also used : WorkflowExecutionFailedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionFailedEventAttributes) WorkflowExecutionCanceledEventAttributes(io.temporal.api.history.v1.WorkflowExecutionCanceledEventAttributes) CanceledFailure(io.temporal.failure.CanceledFailure) EncodedValues(io.temporal.common.converter.EncodedValues) WorkflowExecutionTimedOutEventAttributes(io.temporal.api.history.v1.WorkflowExecutionTimedOutEventAttributes) WorkflowFailedException(io.temporal.client.WorkflowFailedException) TimeoutFailure(io.temporal.failure.TimeoutFailure) WorkflowExecutionCompletedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionCompletedEventAttributes) WorkflowExecutionTerminatedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionTerminatedEventAttributes) TerminatedFailure(io.temporal.failure.TerminatedFailure) Payloads(io.temporal.api.common.v1.Payloads)

Example 3 with CanceledFailure

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);
}
Also used : CanceledFailure(io.temporal.failure.CanceledFailure) Functions(io.temporal.workflow.Functions) SignalExternalWorkflowExecutionCommandAttributes(io.temporal.api.command.v1.SignalExternalWorkflowExecutionCommandAttributes) ChildWorkflowTaskFailedException(io.temporal.internal.replay.ChildWorkflowTaskFailedException) WorkflowException(io.temporal.client.WorkflowException) Payloads(io.temporal.api.common.v1.Payloads)

Example 4 with CanceledFailure

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;
}
Also used : ExecuteActivityParameters(io.temporal.internal.replay.ExecuteActivityParameters) CanceledFailure(io.temporal.failure.CanceledFailure) Functions(io.temporal.workflow.Functions) ChildWorkflowTaskFailedException(io.temporal.internal.replay.ChildWorkflowTaskFailedException) WorkflowException(io.temporal.client.WorkflowException)

Example 5 with CanceledFailure

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;
}
Also used : CanceledFailure(io.temporal.failure.CanceledFailure) Functions(io.temporal.workflow.Functions)

Aggregations

CanceledFailure (io.temporal.failure.CanceledFailure)29 WorkflowFailedException (io.temporal.client.WorkflowFailedException)19 Test (org.junit.Test)17 WorkflowStub (io.temporal.client.WorkflowStub)16 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)7 WorkflowException (io.temporal.client.WorkflowException)5 ChildWorkflowTaskFailedException (io.temporal.internal.replay.ChildWorkflowTaskFailedException)4 Functions (io.temporal.workflow.Functions)4 EncodedValues (io.temporal.common.converter.EncodedValues)3 ChildWorkflowFailure (io.temporal.failure.ChildWorkflowFailure)3 TestWorkflow1 (io.temporal.workflow.shared.TestWorkflows.TestWorkflow1)3 Payloads (io.temporal.api.common.v1.Payloads)2 History (io.temporal.api.history.v1.History)2 HistoryEvent (io.temporal.api.history.v1.HistoryEvent)2 WorkflowOptions (io.temporal.client.WorkflowOptions)2 CancellationScope (io.temporal.workflow.CancellationScope)2 TestWorkflows (io.temporal.workflow.shared.TestWorkflows)2 StatusRuntimeException (io.grpc.StatusRuntimeException)1 SignalExternalWorkflowExecutionCommandAttributes (io.temporal.api.command.v1.SignalExternalWorkflowExecutionCommandAttributes)1 StartChildWorkflowExecutionCommandAttributes (io.temporal.api.command.v1.StartChildWorkflowExecutionCommandAttributes)1