Skip to content

Commit 8685758

Browse files
authored
Add tag support to new orchestrations (#231)
Signed-off-by: Hal Spang <[email protected]>
1 parent 924e542 commit 8685758

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-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 ([#231](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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
package com.microsoft.durabletask;
44

55
import java.time.Instant;
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.Map;
69

710
/**
811
* Options for starting a new instance of an orchestration.
@@ -12,6 +15,7 @@ public final class NewOrchestrationInstanceOptions {
1215
private String instanceId;
1316
private Object input;
1417
private Instant startTime;
18+
private Map<String, String> tags;
1519

1620
/**
1721
* Default constructor for the {@link NewOrchestrationInstanceOptions} class.
@@ -71,6 +75,21 @@ public NewOrchestrationInstanceOptions setStartTime(Instant startTime) {
7175
return this;
7276
}
7377

78+
/**
79+
* Sets the tags associated with the new orchestration instance.
80+
*
81+
* @param tags the tags to associate with the new orchestration instance
82+
* @return this {@link NewOrchestrationInstanceOptions} object
83+
*/
84+
public NewOrchestrationInstanceOptions setTags(Map<String, String> tags) {
85+
if (this.tags == null) {
86+
this.tags = new HashMap<>(tags);
87+
} else {
88+
this.tags.putAll(tags);
89+
}
90+
return this;
91+
}
92+
7493
/**
7594
* Gets the user-specified version of the new orchestration.
7695
*
@@ -106,4 +125,13 @@ public Object getInput() {
106125
public Instant getStartTime() {
107126
return this.startTime;
108127
}
128+
129+
/**
130+
* Gets the tags associated with the new orchestration instance. If no tags were set, an empty map is returned.
131+
*
132+
* @return a map of tags associated with the new orchestration instance.
133+
*/
134+
public Map<String, String> getTags() {
135+
return this.tags == null ? Collections.emptyMap() : new HashMap<>(this.tags);
136+
}
109137
}

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

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

88
import java.time.Instant;
9+
import java.util.HashMap;
10+
import java.util.Map;
911

1012
import static com.microsoft.durabletask.Helpers.isNullOrEmpty;
1113

@@ -29,6 +31,7 @@ public final class OrchestrationMetadata {
2931
private final String serializedOutput;
3032
private final String serializedCustomStatus;
3133
private final FailureDetails failureDetails;
34+
private final Map<String, String> tags;
3235

3336
OrchestrationMetadata(
3437
OrchestratorService.GetInstanceResponse fetchResponse,
@@ -53,6 +56,7 @@ public final class OrchestrationMetadata {
5356
this.serializedOutput = state.getOutput().getValue();
5457
this.serializedCustomStatus = state.getCustomStatus().getValue();
5558
this.failureDetails = new FailureDetails(state.getFailureDetails());
59+
this.tags = state.getTagsMap().isEmpty() ? new HashMap<>() : new HashMap<>(state.getTagsMap());
5660
}
5761

5862
/**
@@ -205,6 +209,15 @@ public boolean isCustomStatusFetched() {
205209
return this.serializedCustomStatus != null && !this.serializedCustomStatus.isEmpty();
206210
}
207211

212+
/**
213+
* Gets the tags associated with the orchestration instance.
214+
*
215+
* @return a map of tags associated with the orchestration instance
216+
*/
217+
public Map<String, String> getTags() {
218+
return this.tags;
219+
}
220+
208221
private <T> T readPayloadAs(Class<T> type, String payload) {
209222
if (!this.requestedInputsAndOutputs) {
210223
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)