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