Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NewWorkflowOption to config version, time, input, etc when start a new workflow #945

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ public <T extends Workflow> String scheduleNewWorkflow(Class<T> clazz, Object in
return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(), input, instanceId);
}

/**
* Schedules a new workflow with a specified set of options for execution.
*
* @param <T> any Workflow type
* @param clazz Class extending Workflow to start an instance of.
* @param options the options for the new workflow, including input, instance ID, etc.
* @return the <code>instanceId</code> parameter value.
*/
public <T extends Workflow> String scheduleNewWorkflow(Class<T> clazz, NewWorkflowOption options) {
return this.innerClient.scheduleNewOrchestrationInstance(clazz.getCanonicalName(),
options.getNewOrchestrationInstanceOptions());
}

/**
* Terminates the workflow associated with the provided instance id.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2023 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.workflows.client;

import com.microsoft.durabletask.NewOrchestrationInstanceOptions;

import java.time.Instant;

/**
* Options for starting a new instance of a workflow.
*/
public class NewWorkflowOption {
private final NewOrchestrationInstanceOptions newOrchestrationInstanceOptions = new NewOrchestrationInstanceOptions();

/**
* Sets the version of the workflow to start.
*
* @param version the user-defined version of workflow
* @return this {@link NewWorkflowOption} object
*/
public NewWorkflowOption setVersion(String version) {
this.newOrchestrationInstanceOptions.setVersion(version);
return this;

Check warning on line 34 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L33-L34

Added lines #L33 - L34 were not covered by tests
}

/**
* Sets the instance ID of the workflow to start.
*
* <p>If no instance ID is configured, the workflow will be created with a randomly generated instance ID.
*
* @param instanceId the ID of the new workflow
* @return this {@link NewWorkflowOption} object
*/
public NewWorkflowOption setInstanceId(String instanceId) {
this.newOrchestrationInstanceOptions.setInstanceId(instanceId);
return this;

Check warning on line 47 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L46-L47

Added lines #L46 - L47 were not covered by tests
}

/**
* Sets the input of the workflow to start.
*
* @param input the input of the new workflow
* @return this {@link NewWorkflowOption} object
*/
public NewWorkflowOption setInput(Object input) {
this.newOrchestrationInstanceOptions.setInput(input);
return this;
}

/**
* Sets the start time of the new workflow.
*
* <p>By default, new workflow instances start executing immediately. This method can be used
* to start them at a specific time in the future.
*
* @param startTime the start time of the new workflow
* @return this {@link NewWorkflowOption} object
*/
public NewWorkflowOption setStartTime(Instant startTime) {
this.newOrchestrationInstanceOptions.setStartTime(startTime);
return this;
}

/**
* Gets the user-specified version of the new workflow.
*
* @return the user-specified version of the new workflow.
*/
public String getVersion() {
return this.newOrchestrationInstanceOptions.getVersion();

Check warning on line 81 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L81

Added line #L81 was not covered by tests
}

/**
* Gets the instance ID of the new workflow.
*
* @return the instance ID of the new workflow.
*/
public String getInstanceId() {
return this.newOrchestrationInstanceOptions.getInstanceId();

Check warning on line 90 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L90

Added line #L90 was not covered by tests
}

/**
* Gets the input of the new workflow.
*
* @return the input of the new workflow.
*/
public Object getInput() {
return this.newOrchestrationInstanceOptions.getInput();

Check warning on line 99 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L99

Added line #L99 was not covered by tests
}

/**
* Gets the configured start time of the new workflow instance.
*
* @return the configured start time of the new workflow instance.
*/
public Instant getStartTime() {
return this.newOrchestrationInstanceOptions.getStartTime();

Check warning on line 108 in sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java

View check run for this annotation

Codecov / codecov/patch

sdk-workflows/src/main/java/io/dapr/workflows/client/NewWorkflowOption.java#L108

Added line #L108 was not covered by tests
}

public NewOrchestrationInstanceOptions getNewOrchestrationInstanceOptions() {
return newOrchestrationInstanceOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.lang.reflect.Constructor;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;

Expand Down Expand Up @@ -106,6 +107,19 @@ public void scheduleNewWorkflowWithArgsNameInputInstance() {
.scheduleNewOrchestrationInstance(expectedName, expectedInput, expectedInstanceId);
}

@Test
public void scheduleNewWorkflowWithNewWorkflowOption() {
String expectedName = TestWorkflow.class.getCanonicalName();
Object expectedInput = new Object();
NewWorkflowOption newWorkflowOption = new NewWorkflowOption();
newWorkflowOption.setInput(expectedInput).setStartTime(Instant.now());

client.scheduleNewWorkflow(TestWorkflow.class, newWorkflowOption);

verify(mockInnerClient, times(1))
.scheduleNewOrchestrationInstance(expectedName, newWorkflowOption.getNewOrchestrationInstanceOptions());
}

@Test
public void terminateWorkflow() {
String expectedArgument = "TestWorkflowInstanceId";
Expand Down
Loading