Skip to content

Commit 9898a41

Browse files
Support Delay Start in Java Client (#640)
1 parent 38667f1 commit 9898a41

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

src/main/java/com/uber/cadence/client/WorkflowOptions.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public static WorkflowOptions merge(
6666
.setMemo(o.getMemo())
6767
.setSearchAttributes(o.getSearchAttributes())
6868
.setContextPropagators(o.getContextPropagators())
69+
.setDelayStart(o.delayStart)
6970
.validateBuildWithDefaults();
7071
}
7172

@@ -91,6 +92,8 @@ public static final class Builder {
9192

9293
private List<ContextPropagator> contextPropagators;
9394

95+
private Duration delayStart;
96+
9497
public Builder() {}
9598

9699
public Builder(WorkflowOptions o) {
@@ -107,6 +110,7 @@ public Builder(WorkflowOptions o) {
107110
this.memo = o.memo;
108111
this.searchAttributes = o.searchAttributes;
109112
this.contextPropagators = o.contextPropagators;
113+
this.delayStart = o.delayStart;
110114
}
111115

112116
/**
@@ -214,6 +218,11 @@ public Builder setContextPropagators(List<ContextPropagator> contextPropagators)
214218
return this;
215219
}
216220

221+
public Builder setDelayStart(Duration delayStart) {
222+
this.delayStart = delayStart;
223+
return this;
224+
}
225+
217226
public WorkflowOptions build() {
218227
return new WorkflowOptions(
219228
workflowId,
@@ -225,7 +234,8 @@ public WorkflowOptions build() {
225234
cronSchedule,
226235
memo,
227236
searchAttributes,
228-
contextPropagators);
237+
contextPropagators,
238+
delayStart);
229239
}
230240

231241
/**
@@ -261,6 +271,13 @@ public WorkflowOptions validateBuildWithDefaults() {
261271
cron.validate();
262272
}
263273

274+
if (delayStart != null) {
275+
if (delayStart.getSeconds() < 0) {
276+
throw new IllegalArgumentException(
277+
"Delay start (in seconds) value cannot be lower than zero");
278+
}
279+
}
280+
264281
return new WorkflowOptions(
265282
workflowId,
266283
policy,
@@ -272,7 +289,8 @@ public WorkflowOptions validateBuildWithDefaults() {
272289
cronSchedule,
273290
memo,
274291
searchAttributes,
275-
contextPropagators);
292+
contextPropagators,
293+
delayStart);
276294
}
277295
}
278296

@@ -296,6 +314,8 @@ public WorkflowOptions validateBuildWithDefaults() {
296314

297315
private List<ContextPropagator> contextPropagators;
298316

317+
private Duration delayStart;
318+
299319
private WorkflowOptions(
300320
String workflowId,
301321
WorkflowIdReusePolicy workflowIdReusePolicy,
@@ -306,7 +326,8 @@ private WorkflowOptions(
306326
String cronSchedule,
307327
Map<String, Object> memo,
308328
Map<String, Object> searchAttributes,
309-
List<ContextPropagator> contextPropagators) {
329+
List<ContextPropagator> contextPropagators,
330+
Duration delayStart) {
310331
this.workflowId = workflowId;
311332
this.workflowIdReusePolicy = workflowIdReusePolicy;
312333
this.executionStartToCloseTimeout = executionStartToCloseTimeout;
@@ -317,6 +338,7 @@ private WorkflowOptions(
317338
this.memo = memo;
318339
this.searchAttributes = searchAttributes;
319340
this.contextPropagators = contextPropagators;
341+
this.delayStart = delayStart;
320342
}
321343

322344
public String getWorkflowId() {
@@ -359,6 +381,10 @@ public List<ContextPropagator> getContextPropagators() {
359381
return contextPropagators;
360382
}
361383

384+
public Duration getDelayStart() {
385+
return delayStart;
386+
}
387+
362388
@Override
363389
public boolean equals(Object o) {
364390
if (this == o) return true;
@@ -373,7 +399,8 @@ public boolean equals(Object o) {
373399
&& Objects.equals(cronSchedule, that.cronSchedule)
374400
&& Objects.equals(memo, that.memo)
375401
&& Objects.equals(searchAttributes, that.searchAttributes)
376-
&& Objects.equals(contextPropagators, that.contextPropagators);
402+
&& Objects.equals(contextPropagators, that.contextPropagators)
403+
&& Objects.equals(delayStart, that.delayStart);
377404
}
378405

379406
@Override
@@ -388,7 +415,8 @@ public int hashCode() {
388415
cronSchedule,
389416
memo,
390417
searchAttributes,
391-
contextPropagators);
418+
contextPropagators,
419+
delayStart);
392420
}
393421

394422
@Override
@@ -419,6 +447,9 @@ public String toString() {
419447
+ ", contextPropagators='"
420448
+ contextPropagators
421449
+ '\''
450+
+ ", delayStart='"
451+
+ delayStart
452+
+ '\''
422453
+ '}';
423454
}
424455
}

src/main/java/com/uber/cadence/internal/common/StartWorkflowExecutionParameters.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public final class StartWorkflowExecutionParameters {
5454

5555
private Map<String, byte[]> context;
5656

57+
private Duration delayStart;
58+
5759
/**
5860
* Returns the value of the WorkflowId property for this object.
5961
*
@@ -307,6 +309,14 @@ public void setContext(Map<String, byte[]> context) {
307309
this.context = context;
308310
}
309311

312+
public void setDelayStart(Duration delayStart) {
313+
this.delayStart = delayStart;
314+
}
315+
316+
public Duration getDelayStart() {
317+
return delayStart;
318+
}
319+
310320
public StartWorkflowExecutionParameters withRetryParameters(RetryParameters retryParameters) {
311321
this.retryParameters = retryParameters;
312322
return this;
@@ -383,6 +393,8 @@ public String toString() {
383393
+ searchAttributes
384394
+ ", context='"
385395
+ context
396+
+ ", delayStart='"
397+
+ delayStart
386398
+ '\''
387399
+ '}';
388400
}
@@ -403,7 +415,8 @@ public boolean equals(Object o) {
403415
&& Objects.equals(cronSchedule, that.cronSchedule)
404416
&& Objects.equals(memo, that.memo)
405417
&& Objects.equals(searchAttributes, that.searchAttributes)
406-
&& Objects.equals(context, that.context);
418+
&& Objects.equals(context, that.context)
419+
&& Objects.equals(delayStart, that.delayStart);
407420
}
408421

409422
@Override
@@ -420,7 +433,8 @@ public int hashCode() {
420433
cronSchedule,
421434
memo,
422435
searchAttributes,
423-
context);
436+
context,
437+
delayStart);
424438
result = 31 * result + Arrays.hashCode(input);
425439
return result;
426440
}

src/main/java/com/uber/cadence/internal/external/GenericWorkflowClientExternalImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ private StartWorkflowExecutionRequest getStartRequest(
210210
request.setMemo(toMemoThrift(startParameters.getMemo()));
211211
request.setSearchAttributes(toSearchAttributesThrift(startParameters.getSearchAttributes()));
212212
request.setHeader(toHeaderThrift(startParameters.getContext()));
213+
if (startParameters.getDelayStart() != null) {
214+
request.setDelayStartSeconds((int) startParameters.getDelayStart().getSeconds());
215+
}
213216

214217
return request;
215218
}
@@ -379,6 +382,9 @@ private WorkflowExecution signalWithStartWorkflowInternal(
379382
if (!Strings.isNullOrEmpty(startParameters.getCronSchedule())) {
380383
request.setCronSchedule(startParameters.getCronSchedule());
381384
}
385+
if (startParameters.getDelayStart() != null) {
386+
request.setDelayStartSeconds((int) startParameters.getDelayStart().getSeconds());
387+
}
382388
StartWorkflowExecutionResponse result;
383389
try {
384390
result =

src/main/java/com/uber/cadence/internal/sync/WorkflowStubImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ private StartWorkflowExecutionParameters getStartWorkflowExecutionParameters(
169169
p.setMemo(convertMemoFromObjectToBytes(o.getMemo()));
170170
p.setSearchAttributes(convertSearchAttributesFromObjectToBytes(o.getSearchAttributes()));
171171
p.setContext(extractContextsAndConvertToBytes(o.getContextPropagators()));
172+
p.setDelayStart(o.getDelayStart());
172173
return p;
173174
}
174175

src/test/java/com/uber/cadence/workflow/WorkflowTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,6 +4467,29 @@ public void testGetVersion() {
44674467
"executeActivity customActivity1");
44684468
}
44694469

4470+
@Test
4471+
public void testDelayStart() {
4472+
Assume.assumeTrue("skipping for non docker tests", useExternalService);
4473+
4474+
int delaySeconds = 5;
4475+
startWorkerFor(TestGetVersionWorkflowImpl.class);
4476+
WorkflowOptions options =
4477+
newWorkflowOptionsBuilder(taskList).setDelayStart(Duration.ofSeconds(delaySeconds)).build();
4478+
4479+
LocalDateTime start = LocalDateTime.now();
4480+
System.out.printf("\n\nSTART: %s \n\n", start.toString());
4481+
4482+
TestWorkflow1 workflowStub = workflowClient.newWorkflowStub(TestWorkflow1.class, options);
4483+
String result = workflowStub.execute(taskList);
4484+
4485+
System.out.printf("\n\nRESULT: %s \n\n", result.toString());
4486+
LocalDateTime end = LocalDateTime.now();
4487+
System.out.printf("\n\nEND: %s \n\n", end.toString());
4488+
4489+
assertTrue(
4490+
"end time should be at least +10 seconds", start.plusSeconds(delaySeconds).isBefore(end));
4491+
}
4492+
44704493
public static class TestGetVersionWorkflow2Impl implements TestWorkflow1 {
44714494

44724495
@Override

0 commit comments

Comments
 (0)