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());
}
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);
}
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));
}
}
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);
}
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);
}
Aggregations