Skip to content

Commit a0454a6

Browse files
committed
Add tag support to new orchestrations
Signed-off-by: Hal Spang <[email protected]>
1 parent 924e542 commit a0454a6

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Unreleased
2+
* Add support for tags when creating new orchestrations ([#230](https://github.com/microsoft/durabletask-java/pull/230))
3+
14
## v1.5.2
25
* Add distributed tracing support for Azure Functions client scenarios ([#211](https://github.com/microsoft/durabletask-java/pull/211))
36

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ public String scheduleNewOrchestrationInstance(
114114
builder.setScheduledStartTimestamp(ts);
115115
}
116116

117+
if (!options.getTags().isEmpty()) {
118+
builder.putAllTags(options.getTags());
119+
}
120+
117121
Span currentSpan = Span.current();
118122
String traceParent = null;
119123
String traceState = null;

client/src/main/java/com/microsoft/durabletask/NewOrchestrationInstanceOptions.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package com.microsoft.durabletask;
44

55
import java.time.Instant;
6+
import java.util.HashMap;
7+
import java.util.Map;
68

79
/**
810
* Options for starting a new instance of an orchestration.
@@ -12,6 +14,7 @@ public final class NewOrchestrationInstanceOptions {
1214
private String instanceId;
1315
private Object input;
1416
private Instant startTime;
17+
private Map<String, String> tags;
1518

1619
/**
1720
* Default constructor for the {@link NewOrchestrationInstanceOptions} class.
@@ -71,6 +74,21 @@ public NewOrchestrationInstanceOptions setStartTime(Instant startTime) {
7174
return this;
7275
}
7376

77+
/**
78+
* Sets the tags associated with the new orchestration instance.
79+
*
80+
* @param tags the tags to associate with the new orchestration instance
81+
* @return this {@link NewOrchestrationInstanceOptions} object
82+
*/
83+
public NewOrchestrationInstanceOptions setTags(Map<String, String> tags) {
84+
if (this.tags == null) {
85+
this.tags = new HashMap<>(tags);
86+
} else {
87+
this.tags.putAll(tags);
88+
}
89+
return this;
90+
}
91+
7492
/**
7593
* Gets the user-specified version of the new orchestration.
7694
*
@@ -106,4 +124,13 @@ public Object getInput() {
106124
public Instant getStartTime() {
107125
return this.startTime;
108126
}
127+
128+
/**
129+
* Gets the tags associated with the new orchestration instance. If no tags were set, an empty map is returned.
130+
*
131+
* @return a map of tags associated with the new orchestration instance.
132+
*/
133+
public Map<String, String> getTags() {
134+
return this.tags == null ? Map.of() : Map.copyOf(this.tags);
135+
}
109136
}

client/src/main/java/com/microsoft/durabletask/OrchestrationMetadata.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.microsoft.durabletask.implementation.protobuf.OrchestratorService.OrchestrationState;
77

88
import java.time.Instant;
9+
import java.util.Map;
910

1011
import static com.microsoft.durabletask.Helpers.isNullOrEmpty;
1112

@@ -29,6 +30,7 @@ public final class OrchestrationMetadata {
2930
private final String serializedOutput;
3031
private final String serializedCustomStatus;
3132
private final FailureDetails failureDetails;
33+
private final Map<String, String> tags;
3234

3335
OrchestrationMetadata(
3436
OrchestratorService.GetInstanceResponse fetchResponse,
@@ -53,6 +55,7 @@ public final class OrchestrationMetadata {
5355
this.serializedOutput = state.getOutput().getValue();
5456
this.serializedCustomStatus = state.getCustomStatus().getValue();
5557
this.failureDetails = new FailureDetails(state.getFailureDetails());
58+
this.tags = state.getTagsMap().isEmpty() ? Map.of() : Map.copyOf(state.getTagsMap());
5659
}
5760

5861
/**
@@ -205,6 +208,15 @@ public boolean isCustomStatusFetched() {
205208
return this.serializedCustomStatus != null && !this.serializedCustomStatus.isEmpty();
206209
}
207210

211+
/**
212+
* Gets the tags associated with the orchestration instance.
213+
*
214+
* @return a map of tags associated with the orchestration instance
215+
*/
216+
public Map<String, String> getTags() {
217+
return this.tags;
218+
}
219+
208220
private <T> T readPayloadAs(Class<T> type, String payload) {
209221
if (!this.requestedInputsAndOutputs) {
210222
throw new IllegalStateException("This method can only be used when instance metadata is fetched with the option to include input and output data.");

client/src/test/java/com/microsoft/durabletask/IntegrationTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,4 +1539,29 @@ public void newUUIDTest() {
15391539
throw new RuntimeException(e);
15401540
}
15411541
}
1542+
1543+
@Test
1544+
public void newOrchestrationWithTags() {
1545+
String orchestratorName = "test-new-orchestration-with-tags";
1546+
DurableTaskGrpcWorker worker = this.createWorkerBuilder()
1547+
.addOrchestrator(orchestratorName, ctx -> {
1548+
ctx.complete("Orchestration with tags started");
1549+
})
1550+
.buildAndStart();
1551+
DurableTaskClient client = this.createClientBuilder().build();
1552+
1553+
try(worker; client) {
1554+
NewOrchestrationInstanceOptions options = new NewOrchestrationInstanceOptions()
1555+
.setTags(Map.of("key1", "value1", "key2", "value2"));
1556+
1557+
String instanceId = client.scheduleNewOrchestrationInstance(orchestratorName, options);
1558+
OrchestrationMetadata instance = client.waitForInstanceCompletion(instanceId, defaultTimeout, true);
1559+
assertNotNull(instance);
1560+
assertEquals(OrchestrationRuntimeStatus.COMPLETED, instance.getRuntimeStatus());
1561+
assertEquals("Orchestration with tags started", instance.readOutputAs(String.class));
1562+
assertEquals(options.getTags(), instance.getTags());
1563+
} catch (TimeoutException e) {
1564+
throw new RuntimeException(e);
1565+
}
1566+
}
15421567
}

0 commit comments

Comments
 (0)