Search in sources :

Example 1 with WorkflowType

use of io.temporal.api.common.v1.WorkflowType in project sdk-java by temporalio.

the class ReplayWorkflowTaskHandler method createCompletedWFTRequest.

private Result createCompletedWFTRequest(String workflowType, PollWorkflowTaskQueueResponseOrBuilder workflowTask, WorkflowTaskResult result) {
    WorkflowExecution execution = workflowTask.getWorkflowExecution();
    if (log.isTraceEnabled()) {
        log.trace("WorkflowTask startedEventId=" + workflowTask.getStartedEventId() + ", WorkflowId=" + execution.getWorkflowId() + ", RunId=" + execution.getRunId() + " completed with \n" + WorkflowExecutionUtils.prettyPrintCommands(result.getCommands()));
    } else if (log.isDebugEnabled()) {
        log.debug("WorkflowTask startedEventId=" + workflowTask.getStartedEventId() + ", WorkflowId=" + execution.getWorkflowId() + ", RunId=" + execution.getRunId() + " completed with " + result.getCommands().size() + " new commands");
    }
    RespondWorkflowTaskCompletedRequest.Builder completedRequest = RespondWorkflowTaskCompletedRequest.newBuilder().setTaskToken(workflowTask.getTaskToken()).addAllCommands(result.getCommands()).putAllQueryResults(result.getQueryResults()).setForceCreateNewWorkflowTask(result.isForceWorkflowTask()).setReturnNewWorkflowTask(result.isForceWorkflowTask());
    if (stickyTaskQueueName != null && (stickyTaskQueueScheduleToStartTimeout == null || !stickyTaskQueueScheduleToStartTimeout.isZero())) {
        StickyExecutionAttributes.Builder attributes = StickyExecutionAttributes.newBuilder().setWorkerTaskQueue(createStickyTaskQueue(stickyTaskQueueName));
        if (stickyTaskQueueScheduleToStartTimeout != null) {
            attributes.setScheduleToStartTimeout(ProtobufTimeUtils.toProtoDuration(stickyTaskQueueScheduleToStartTimeout));
        }
        completedRequest.setStickyAttributes(attributes);
    }
    return new Result(workflowType, completedRequest.build(), null, null, null, result.isFinalCommand());
}
Also used : StickyExecutionAttributes(io.temporal.api.taskqueue.v1.StickyExecutionAttributes) WorkflowExecution(io.temporal.api.common.v1.WorkflowExecution)

Example 2 with WorkflowType

use of io.temporal.api.common.v1.WorkflowType in project sdk-java by temporalio.

the class ReplayWorkflowTaskHandler method createStatefulHandler.

// TODO(maxim): Consider refactoring that avoids mutating workflow task.
private WorkflowRunTaskHandler createStatefulHandler(PollWorkflowTaskQueueResponse.Builder workflowTask, Scope metricsScope) throws Exception {
    WorkflowType workflowType = workflowTask.getWorkflowType();
    List<HistoryEvent> events = workflowTask.getHistory().getEventsList();
    // Sticky workflow task with partial history.
    if (events.isEmpty() || events.get(0).getEventId() > 1) {
        GetWorkflowExecutionHistoryRequest getHistoryRequest = GetWorkflowExecutionHistoryRequest.newBuilder().setNamespace(namespace).setExecution(workflowTask.getWorkflowExecution()).build();
        GetWorkflowExecutionHistoryResponse getHistoryResponse = service.blockingStub().withOption(METRICS_TAGS_CALL_OPTIONS_KEY, metricsScope).getWorkflowExecutionHistory(getHistoryRequest);
        workflowTask.setHistory(getHistoryResponse.getHistory()).setNextPageToken(getHistoryResponse.getNextPageToken());
    }
    ReplayWorkflow workflow = workflowFactory.getWorkflow(workflowType);
    return new ReplayWorkflowRunTaskHandler(service, namespace, workflow, workflowTask, options, metricsScope, localActivityTaskPoller);
}
Also used : WorkflowType(io.temporal.api.common.v1.WorkflowType) HistoryEvent(io.temporal.api.history.v1.HistoryEvent)

Example 3 with WorkflowType

use of io.temporal.api.common.v1.WorkflowType 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 4 with WorkflowType

use of io.temporal.api.common.v1.WorkflowType in project sdk-java by temporalio.

the class SyncWorkflow method start.

@Override
public void start(HistoryEvent event, ReplayWorkflowContext context) {
    if (event.getEventType() != EventType.EVENT_TYPE_WORKFLOW_EXECUTION_STARTED || !event.hasWorkflowExecutionStartedEventAttributes()) {
        throw new IllegalArgumentException("first event is not WorkflowExecutionStarted, but " + event.getEventType());
    }
    WorkflowExecutionStartedEventAttributes startEvent = event.getWorkflowExecutionStartedEventAttributes();
    WorkflowType workflowType = startEvent.getWorkflowType();
    if (workflow == null) {
        throw new IllegalArgumentException("Unknown workflow type: " + workflowType);
    }
    Optional<Payloads> result = startEvent.hasLastCompletionResult() ? Optional.of(startEvent.getLastCompletionResult()) : Optional.empty();
    Optional<Failure> lastFailure = startEvent.hasContinuedFailure() ? Optional.of(startEvent.getContinuedFailure()) : Optional.empty();
    SyncWorkflowContext syncContext = new SyncWorkflowContext(context, workflowImplementationOptions, dataConverter, contextPropagators, result, lastFailure);
    workflowProc = new WorkflowExecuteRunnable(syncContext, workflow, startEvent, workflowImplementationOptions);
    // The following order is ensured by this code and DeterministicRunner implementation:
    // 1. workflow.initialize
    // 2. signal handler (if signalWithStart was called)
    // 3. main workflow method
    runner = DeterministicRunner.newRunner(threadPool, syncContext, () -> {
        workflow.initialize();
        WorkflowInternal.newWorkflowMethodThread(() -> workflowProc.run(), workflowMethodThreadNameStrategy.createThreadName(context.getWorkflowExecution())).start();
    }, cache);
}
Also used : WorkflowType(io.temporal.api.common.v1.WorkflowType) WorkflowExecutionStartedEventAttributes(io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes) Failure(io.temporal.api.failure.v1.Failure) Payloads(io.temporal.api.common.v1.Payloads)

Example 5 with WorkflowType

use of io.temporal.api.common.v1.WorkflowType in project sdk-java by temporalio.

the class SyncWorkflowContext method continueAsNew.

@Override
public void continueAsNew(ContinueAsNewInput input) {
    ContinueAsNewWorkflowExecutionCommandAttributes.Builder attributes = ContinueAsNewWorkflowExecutionCommandAttributes.newBuilder();
    String workflowType = input.getWorkflowType();
    if (workflowType != null) {
        attributes.setWorkflowType(WorkflowType.newBuilder().setName(workflowType));
    }
    @Nullable ContinueAsNewOptions options = input.getOptions();
    if (options != null) {
        if (options.getWorkflowRunTimeout() != null) {
            attributes.setWorkflowRunTimeout(ProtobufTimeUtils.toProtoDuration(options.getWorkflowRunTimeout()));
        }
        if (options.getWorkflowTaskTimeout() != null) {
            attributes.setWorkflowTaskTimeout(ProtobufTimeUtils.toProtoDuration(options.getWorkflowTaskTimeout()));
        }
        if (!options.getTaskQueue().isEmpty()) {
            attributes.setTaskQueue(TaskQueue.newBuilder().setName(options.getTaskQueue()));
        }
        Map<String, Object> searchAttributes = options.getSearchAttributes();
        if (searchAttributes != null) {
            attributes.setSearchAttributes(SearchAttributes.newBuilder().putAllIndexedFields(intoPayloadMap(DataConverter.getDefaultInstance(), searchAttributes)));
        }
        Map<String, Object> memo = options.getMemo();
        if (memo != null) {
            attributes.setMemo(Memo.newBuilder().putAllFields(intoPayloadMap(getDataConverter(), memo)));
        }
    }
    List<ContextPropagator> propagators = options != null && options.getContextPropagators() != null ? options.getContextPropagators() : this.contextPropagators;
    io.temporal.api.common.v1.Header grpcHeader = toHeaderGrpc(input.getHeader(), extractContextsAndConvertToBytes(propagators));
    attributes.setHeader(grpcHeader);
    Optional<Payloads> payloads = getDataConverter().toPayloads(input.getArgs());
    payloads.ifPresent(attributes::setInput);
    context.continueAsNewOnCompletion(attributes.build());
    WorkflowThread.exit(null);
}
Also used : ContinueAsNewOptions(io.temporal.workflow.ContinueAsNewOptions) ContinueAsNewWorkflowExecutionCommandAttributes(io.temporal.api.command.v1.ContinueAsNewWorkflowExecutionCommandAttributes) ContextPropagator(io.temporal.common.context.ContextPropagator) Nullable(javax.annotation.Nullable) Payloads(io.temporal.api.common.v1.Payloads)

Aggregations

Payloads (io.temporal.api.common.v1.Payloads)3 WorkflowExecution (io.temporal.api.common.v1.WorkflowExecution)3 WorkflowType (io.temporal.api.common.v1.WorkflowType)3 Failure (io.temporal.api.failure.v1.Failure)2 HistoryEvent (io.temporal.api.history.v1.HistoryEvent)2 WorkflowExecutionStartedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionStartedEventAttributes)2 ContinueAsNewWorkflowExecutionCommandAttributes (io.temporal.api.command.v1.ContinueAsNewWorkflowExecutionCommandAttributes)1 WorkflowExecutionAlreadyStartedFailure (io.temporal.api.errordetails.v1.WorkflowExecutionAlreadyStartedFailure)1 WorkflowExecutionCanceledEventAttributes (io.temporal.api.history.v1.WorkflowExecutionCanceledEventAttributes)1 WorkflowExecutionCompletedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionCompletedEventAttributes)1 WorkflowExecutionFailedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionFailedEventAttributes)1 WorkflowExecutionTerminatedEventAttributes (io.temporal.api.history.v1.WorkflowExecutionTerminatedEventAttributes)1 WorkflowExecutionTimedOutEventAttributes (io.temporal.api.history.v1.WorkflowExecutionTimedOutEventAttributes)1 WorkflowQuery (io.temporal.api.query.v1.WorkflowQuery)1 StickyExecutionAttributes (io.temporal.api.taskqueue.v1.StickyExecutionAttributes)1 PollWorkflowTaskQueueResponse (io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse)1 RespondQueryTaskCompletedRequest (io.temporal.api.workflowservice.v1.RespondQueryTaskCompletedRequest)1 WorkflowExecutionAlreadyStarted (io.temporal.client.WorkflowExecutionAlreadyStarted)1 WorkflowFailedException (io.temporal.client.WorkflowFailedException)1 WorkflowServiceException (io.temporal.client.WorkflowServiceException)1