Search in sources :

Example 1 with ContextPropagator

use of io.temporal.common.context.ContextPropagator in project sdk-java by temporalio.

the class SyncWorkflowContext method executeChildWorkflow.

private Promise<Optional<Payloads>> executeChildWorkflow(String workflowId, String name, ChildWorkflowOptions options, Header header, Optional<Payloads> input, CompletablePromise<WorkflowExecution> executionResult) {
    CompletablePromise<Optional<Payloads>> result = Workflow.newPromise();
    if (CancellationScope.current().isCancelRequested()) {
        CanceledFailure CanceledFailure = new CanceledFailure("execute called from a canceled scope");
        executionResult.completeExceptionally(CanceledFailure);
        result.completeExceptionally(CanceledFailure);
        return result;
    }
    final StartChildWorkflowExecutionCommandAttributes.Builder attributes = StartChildWorkflowExecutionCommandAttributes.newBuilder().setWorkflowType(WorkflowType.newBuilder().setName(name).build());
    attributes.setWorkflowId(workflowId);
    attributes.setNamespace(OptionsUtils.safeGet(options.getNamespace()));
    input.ifPresent(attributes::setInput);
    attributes.setWorkflowRunTimeout(ProtobufTimeUtils.toProtoDuration(options.getWorkflowRunTimeout()));
    attributes.setWorkflowExecutionTimeout(ProtobufTimeUtils.toProtoDuration(options.getWorkflowExecutionTimeout()));
    attributes.setWorkflowTaskTimeout(ProtobufTimeUtils.toProtoDuration(options.getWorkflowTaskTimeout()));
    String taskQueue = options.getTaskQueue();
    if (taskQueue != null) {
        attributes.setTaskQueue(TaskQueue.newBuilder().setName(taskQueue));
    }
    if (options.getWorkflowIdReusePolicy() != null) {
        attributes.setWorkflowIdReusePolicy(options.getWorkflowIdReusePolicy());
    }
    RetryOptions retryOptions = options.getRetryOptions();
    if (retryOptions != null) {
        attributes.setRetryPolicy(toRetryPolicy(retryOptions));
    }
    attributes.setCronSchedule(OptionsUtils.safeGet(options.getCronSchedule()));
    Map<String, Object> memo = options.getMemo();
    if (memo != null) {
        attributes.setMemo(Memo.newBuilder().putAllFields(intoPayloadMap(getDataConverter(), memo)));
    }
    Map<String, Object> searchAttributes = options.getSearchAttributes();
    if (searchAttributes != null) {
        attributes.setSearchAttributes(InternalUtils.convertMapToSearchAttributes(searchAttributes));
    }
    List<ContextPropagator> propagators = options.getContextPropagators();
    if (propagators == null) {
        propagators = this.contextPropagators;
    }
    io.temporal.api.common.v1.Header grpcHeader = toHeaderGrpc(header, extractContextsAndConvertToBytes(propagators));
    attributes.setHeader(grpcHeader);
    ParentClosePolicy parentClosePolicy = options.getParentClosePolicy();
    if (parentClosePolicy != null) {
        attributes.setParentClosePolicy(parentClosePolicy);
    }
    StartChildWorkflowExecutionParameters parameters = new StartChildWorkflowExecutionParameters(attributes, options.getCancellationType());
    Functions.Proc1<Exception> cancellationCallback = context.startChildWorkflow(parameters, (we) -> runner.executeInWorkflowThread("child workflow started callback", () -> executionResult.complete(we)), (output, failure) -> {
        if (failure != null) {
            runner.executeInWorkflowThread("child workflow failure callback", () -> result.completeExceptionally(mapChildWorkflowException(failure)));
        } else {
            runner.executeInWorkflowThread("child workflow completion callback", () -> result.complete(output));
        }
    });
    AtomicBoolean callbackCalled = new AtomicBoolean();
    CancellationScope.current().getCancellationRequest().thenApply((reason) -> {
        if (!callbackCalled.getAndSet(true)) {
            cancellationCallback.apply(new CanceledFailure(reason));
        }
        return null;
    });
    return result;
}
Also used : CanceledFailure(io.temporal.failure.CanceledFailure) Functions(io.temporal.workflow.Functions) ParentClosePolicy(io.temporal.api.enums.v1.ParentClosePolicy) ChildWorkflowTaskFailedException(io.temporal.internal.replay.ChildWorkflowTaskFailedException) WorkflowException(io.temporal.client.WorkflowException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StartChildWorkflowExecutionCommandAttributes(io.temporal.api.command.v1.StartChildWorkflowExecutionCommandAttributes) ContextPropagator(io.temporal.common.context.ContextPropagator) RetryOptions(io.temporal.common.RetryOptions) StartChildWorkflowExecutionParameters(io.temporal.internal.replay.StartChildWorkflowExecutionParameters)

Example 2 with ContextPropagator

use of io.temporal.common.context.ContextPropagator 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)

Example 3 with ContextPropagator

use of io.temporal.common.context.ContextPropagator in project sdk-java by temporalio.

the class RootWorkflowClientHelper method extractContextsAndConvertToBytes.

private io.temporal.common.interceptors.Header extractContextsAndConvertToBytes(List<ContextPropagator> workflowOptionsContextPropagators) {
    List<ContextPropagator> workflowClientContextPropagators = clientOptions.getContextPropagators();
    if ((workflowClientContextPropagators.isEmpty() && workflowOptionsContextPropagators == null) || (workflowOptionsContextPropagators != null && workflowOptionsContextPropagators.isEmpty())) {
        return null;
    }
    List<ContextPropagator> listToUse = MoreObjects.firstNonNull(workflowOptionsContextPropagators, workflowClientContextPropagators);
    Map<String, Payload> result = new HashMap<>();
    for (ContextPropagator propagator : listToUse) {
        result.putAll(propagator.serializeContext(propagator.getCurrentContext()));
    }
    return new io.temporal.common.interceptors.Header(result);
}
Also used : ContextPropagator(io.temporal.common.context.ContextPropagator) ByteString(com.google.protobuf.ByteString)

Example 4 with ContextPropagator

use of io.temporal.common.context.ContextPropagator in project sdk-java by temporalio.

the class SyncWorkflowContext method constructExecuteActivityParameters.

private ExecuteActivityParameters constructExecuteActivityParameters(String name, ActivityOptions options, Header header, Optional<Payloads> input) {
    String taskQueue = options.getTaskQueue();
    if (taskQueue == null) {
        taskQueue = context.getTaskQueue();
    }
    ScheduleActivityTaskCommandAttributes.Builder attributes = ScheduleActivityTaskCommandAttributes.newBuilder().setActivityType(ActivityType.newBuilder().setName(name)).setTaskQueue(TaskQueue.newBuilder().setName(taskQueue)).setScheduleToStartTimeout(ProtobufTimeUtils.toProtoDuration(options.getScheduleToStartTimeout())).setStartToCloseTimeout(ProtobufTimeUtils.toProtoDuration(options.getStartToCloseTimeout())).setScheduleToCloseTimeout(ProtobufTimeUtils.toProtoDuration(options.getScheduleToCloseTimeout())).setHeartbeatTimeout(ProtobufTimeUtils.toProtoDuration(options.getHeartbeatTimeout()));
    input.ifPresent(attributes::setInput);
    RetryOptions retryOptions = options.getRetryOptions();
    if (retryOptions != null) {
        attributes.setRetryPolicy(toRetryPolicy(retryOptions));
    }
    // Set the context value.  Use the context propagators from the ActivityOptions
    // if present, otherwise use the ones configured on the WorkflowContext
    List<ContextPropagator> propagators = options.getContextPropagators();
    if (propagators == null) {
        propagators = this.contextPropagators;
    }
    io.temporal.api.common.v1.Header grpcHeader = toHeaderGrpc(header, extractContextsAndConvertToBytes(propagators));
    attributes.setHeader(grpcHeader);
    return new ExecuteActivityParameters(attributes, options.getCancellationType());
}
Also used : ExecuteActivityParameters(io.temporal.internal.replay.ExecuteActivityParameters) ScheduleActivityTaskCommandAttributes(io.temporal.api.command.v1.ScheduleActivityTaskCommandAttributes) ContextPropagator(io.temporal.common.context.ContextPropagator) RetryOptions(io.temporal.common.RetryOptions)

Example 5 with ContextPropagator

use of io.temporal.common.context.ContextPropagator in project sdk-java by temporalio.

the class WorkflowContext method getPropagatedContexts.

/**
 * @return a map of propagated context objects, keyed by propagator name
 */
Map<String, Object> getPropagatedContexts() {
    if (contextPropagators == null || contextPropagators.isEmpty()) {
        return new HashMap<>();
    }
    Header headers = startedAttributes.getHeader();
    Map<String, Payload> headerData = new HashMap<>(headers.getFieldsMap());
    Map<String, Object> contextData = new HashMap<>();
    for (ContextPropagator propagator : contextPropagators) {
        contextData.put(propagator.getName(), propagator.deserializeContext(headerData));
    }
    return contextData;
}
Also used : Header(io.temporal.api.common.v1.Header) HashMap(java.util.HashMap) ContextPropagator(io.temporal.common.context.ContextPropagator) Payload(io.temporal.api.common.v1.Payload)

Aggregations

ContextPropagator (io.temporal.common.context.ContextPropagator)5 RetryOptions (io.temporal.common.RetryOptions)2 ByteString (com.google.protobuf.ByteString)1 ContinueAsNewWorkflowExecutionCommandAttributes (io.temporal.api.command.v1.ContinueAsNewWorkflowExecutionCommandAttributes)1 ScheduleActivityTaskCommandAttributes (io.temporal.api.command.v1.ScheduleActivityTaskCommandAttributes)1 StartChildWorkflowExecutionCommandAttributes (io.temporal.api.command.v1.StartChildWorkflowExecutionCommandAttributes)1 Header (io.temporal.api.common.v1.Header)1 Payload (io.temporal.api.common.v1.Payload)1 Payloads (io.temporal.api.common.v1.Payloads)1 ParentClosePolicy (io.temporal.api.enums.v1.ParentClosePolicy)1 WorkflowException (io.temporal.client.WorkflowException)1 CanceledFailure (io.temporal.failure.CanceledFailure)1 ChildWorkflowTaskFailedException (io.temporal.internal.replay.ChildWorkflowTaskFailedException)1 ExecuteActivityParameters (io.temporal.internal.replay.ExecuteActivityParameters)1 StartChildWorkflowExecutionParameters (io.temporal.internal.replay.StartChildWorkflowExecutionParameters)1 ContinueAsNewOptions (io.temporal.workflow.ContinueAsNewOptions)1 Functions (io.temporal.workflow.Functions)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Nullable (javax.annotation.Nullable)1