Skip to content

Commit 54cdb47

Browse files
authored
Add new APIs without the workflowRunId (#185)
Co-authored-by: Kaili Zhu <kzhu@indeed.com>
1 parent 95db90b commit 54cdb47

File tree

7 files changed

+232
-30
lines changed

7 files changed

+232
-30
lines changed

src/main/java/io/iworkflow/core/Client.java

Lines changed: 179 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,15 @@ public List<StateCompletionOutput> getComplexWorkflowResultWithWait(final String
319319
return getComplexWorkflowResultWithWait(workflowId, "");
320320
}
321321

322+
/**
323+
* Emit a signal message for the workflow object to receive from external sources
324+
*
325+
* @param workflowClass required
326+
* @param workflowId required
327+
* @param workflowRunId optional, can be empty
328+
* @param signalChannelName required
329+
* @param signalValue optional, can be null
330+
*/
322331
public void signalWorkflow(
323332
final Class<? extends ObjectWorkflow> workflowClass,
324333
final String workflowId,
@@ -343,46 +352,92 @@ public void signalWorkflow(
343352
}
344353

345354
/**
346-
* @param workflowId workflowId
347-
* @param workflowRunId workflowRunId
348-
* @param resetWorkflowTypeAndOptions the combination parameter for reset
355+
* Emit a signal message for the workflow object to receive from external sources
356+
*
357+
* @param workflowClass required
358+
* @param workflowId required
359+
* @param signalChannelName required
360+
* @param signalValue optional, can be null
361+
*/
362+
public void signalWorkflow(
363+
final Class<? extends ObjectWorkflow> workflowClass,
364+
final String workflowId,
365+
final String signalChannelName,
366+
final Object signalValue) {
367+
signalWorkflow(workflowClass, workflowId, "", signalChannelName, signalValue);
368+
}
369+
370+
/**
371+
* @param workflowId required
372+
* @param workflowRunId optional, can be empty
373+
* @param resetWorkflowTypeAndOptions required, the combination parameter for reset
349374
* @return the new runId after reset
350375
*/
351376
public String resetWorkflow(
352377
final String workflowId,
353378
final String workflowRunId,
354379
final ResetWorkflowTypeAndOptions resetWorkflowTypeAndOptions
355380
) {
356-
357381
return unregisteredClient.resetWorkflow(workflowId, workflowRunId, resetWorkflowTypeAndOptions);
358382
}
359383

360384
/**
361-
* Stop a workflow, this is essentially terminate the workflow gracefully
385+
* @param workflowId required
386+
* @param resetWorkflowTypeAndOptions required, the combination parameter for reset
387+
* @return the new runId after reset
388+
*/
389+
public String resetWorkflow(
390+
final String workflowId,
391+
final ResetWorkflowTypeAndOptions resetWorkflowTypeAndOptions
392+
) {
393+
return resetWorkflow(workflowId, "", resetWorkflowTypeAndOptions);
394+
}
395+
396+
/**
397+
* Stop a workflow with options
362398
*
363399
* @param workflowId required
364400
* @param workflowRunId optional, can be empty
401+
* @param options optional, can be null. If not set, the workflow status will be CANCELED
365402
*/
366403
public void stopWorkflow(
367404
final String workflowId,
368-
final String workflowRunId) {
369-
unregisteredClient.stopWorkflow(workflowId, workflowRunId);
405+
final String workflowRunId,
406+
final StopWorkflowOptions options) {
407+
unregisteredClient.stopWorkflow(workflowId, workflowRunId, options);
370408
}
371409

372410
/**
373411
* Stop a workflow with options
374412
*
375413
* @param workflowId required
376-
* @param workflowRunId optional
377-
* @param options optional
414+
* @param options optional, can be null. If not set, the workflow status will be CANCELED
378415
*/
379416
public void stopWorkflow(
380417
final String workflowId,
381-
final String workflowRunId,
382418
final StopWorkflowOptions options) {
383-
unregisteredClient.stopWorkflow(workflowId, workflowRunId, options);
419+
stopWorkflow(workflowId, "", options);
384420
}
385421

422+
/**
423+
* Stop a workflow, this is essentially terminate the workflow gracefully
424+
* The workflow status will be CANCELED
425+
*
426+
* @param workflowId required
427+
*/
428+
public void stopWorkflow(final String workflowId) {
429+
stopWorkflow(workflowId, "", null);
430+
}
431+
432+
/**
433+
* Get specified data attributes (by keys) of a workflow
434+
*
435+
* @param workflowClass required
436+
* @param workflowId required
437+
* @param workflowRunId optional, can be empty
438+
* @param keys required, cannot be empty or null
439+
* @return the data attributes
440+
*/
386441
public Map<String, Object> getWorkflowDataObjects(
387442
final Class<? extends ObjectWorkflow> workflowClass,
388443
final String workflowId,
@@ -395,13 +450,49 @@ public Map<String, Object> getWorkflowDataObjects(
395450
return doGetWorkflowDataObjects(workflowClass, workflowId, workflowRunId, keys);
396451
}
397452

453+
/**
454+
* Get specified data attributes (by keys) of a workflow
455+
*
456+
* @param workflowClass required
457+
* @param workflowId required
458+
* @param keys required, cannot be empty or null
459+
* @return the data attributes
460+
*/
461+
public Map<String, Object> getWorkflowDataObjects(
462+
final Class<? extends ObjectWorkflow> workflowClass,
463+
final String workflowId,
464+
List<String> keys) {
465+
return getWorkflowDataObjects(workflowClass, workflowId, "", keys);
466+
}
467+
468+
/**
469+
* Get all the data attributes of a workflow
470+
*
471+
* @param workflowClass required
472+
* @param workflowId required
473+
* @param workflowRunId optional, can be empty
474+
* @return the data attributes
475+
*/
398476
public Map<String, Object> getAllDataObjects(
399477
final Class<? extends ObjectWorkflow> workflowClass,
400478
final String workflowId,
401479
final String workflowRunId) {
402480
return doGetWorkflowDataObjects(workflowClass, workflowId, workflowRunId, null);
403481
}
404482

483+
/**
484+
* Get all the data attributes of a workflow
485+
*
486+
* @param workflowClass required
487+
* @param workflowId required
488+
* @return the data attributes
489+
*/
490+
public Map<String, Object> getAllDataObjects(
491+
final Class<? extends ObjectWorkflow> workflowClass,
492+
final String workflowId) {
493+
return getAllDataObjects(workflowClass, workflowId, "");
494+
}
495+
405496
private Map<String, Object> doGetWorkflowDataObjects(
406497
final Class<? extends ObjectWorkflow> workflowClass,
407498
final String workflowId,
@@ -471,8 +562,8 @@ public WorkflowSearchResponse searchWorkflow(final WorkflowSearchRequest request
471562
* create a new stub for invoking RPC
472563
*
473564
* @param workflowClassForRpc the class of defining the RPCs to invoke
474-
* @param workflowId workflowId is required
475-
* @param workflowRunId optional
565+
* @param workflowId required
566+
* @param workflowRunId optional, can be empty
476567
* @param <T> the class of defining the RPCs to invoke
477568
* @return the result of the RPC
478569
*/
@@ -515,6 +606,18 @@ public <T> T newRpcStub(Class<T> workflowClassForRpc, String workflowId, String
515606
return result;
516607
}
517608

609+
/**
610+
* create a new stub for invoking RPC
611+
*
612+
* @param workflowClassForRpc the class of defining the RPCs to invoke
613+
* @param workflowId required
614+
* @param <T> the class of defining the RPCs to invoke
615+
* @return the result of the RPC
616+
*/
617+
public <T> T newRpcStub(Class<T> workflowClassForRpc, String workflowId) {
618+
return newRpcStub(workflowClassForRpc, workflowId, "");
619+
}
620+
518621
/**
519622
* invoking the RPC through RPC stub
520623
*
@@ -559,6 +662,15 @@ public void invokeRPC(RpcDefinitions.RpcProc0 rpcStubMethod) {
559662
rpcStubMethod.execute(null, null, null);
560663
}
561664

665+
/**
666+
* Get specified search attributes (by attributeKeys) of a workflow
667+
*
668+
* @param workflowClass required
669+
* @param workflowId required
670+
* @param workflowRunId optional, can be empty
671+
* @param attributeKeys required, cannot be empty or null
672+
* @return the search attributes
673+
*/
562674
public Map<String, Object> getWorkflowSearchAttributes(
563675
final Class<? extends ObjectWorkflow> workflowClass,
564676
final String workflowId,
@@ -570,12 +682,55 @@ public Map<String, Object> getWorkflowSearchAttributes(
570682
return doGetWorkflowSearchAttributes(workflowClass, workflowId, workflowRunId, attributeKeys);
571683
}
572684

685+
/**
686+
* Get specified search attributes (by attributeKeys) of a workflow
687+
*
688+
* @param workflowClass required
689+
* @param workflowId required
690+
* @param attributeKeys required, cannot be empty or null
691+
* @return the search attributes
692+
*/
693+
public Map<String, Object> getWorkflowSearchAttributes(
694+
final Class<? extends ObjectWorkflow> workflowClass,
695+
final String workflowId,
696+
List<String> attributeKeys) {
697+
return getWorkflowSearchAttributes(workflowClass, workflowId, "", attributeKeys);
698+
}
699+
700+
/**
701+
* Get all the search attributes of a workflow
702+
*
703+
* @param workflowClass required
704+
* @param workflowId required
705+
* @param workflowRunId optional, can be empty
706+
* @return the search attributes
707+
*/
708+
public Map<String, Object> getAllSearchAttributes(
709+
final Class<? extends ObjectWorkflow> workflowClass,
710+
final String workflowId,
711+
final String workflowRunId) {
712+
return doGetWorkflowSearchAttributes(workflowClass, workflowId, workflowRunId, null);
713+
}
714+
715+
/**
716+
* Get all the search attributes of a workflow
717+
*
718+
* @param workflowClass required
719+
* @param workflowId required
720+
* @return the search attributes
721+
*/
722+
public Map<String, Object> getAllSearchAttributes(
723+
final Class<? extends ObjectWorkflow> workflowClass,
724+
final String workflowId) {
725+
return getAllSearchAttributes(workflowClass, workflowId, "");
726+
}
727+
573728
/**
574729
* Describe a workflow to get its info.
575730
* If the workflow does not exist, throw the WORKFLOW_NOT_EXISTS_SUB_STATUS exception.
576731
*
577732
* @param workflowId required
578-
* @param workflowRunId optional
733+
* @param workflowRunId optional, can be empty
579734
* @return the workflow's info
580735
*/
581736
public WorkflowInfo describeWorkflow(
@@ -587,11 +742,16 @@ public WorkflowInfo describeWorkflow(
587742
.build();
588743
}
589744

590-
public Map<String, Object> getAllSearchAttributes(
591-
final Class<? extends ObjectWorkflow> workflowClass,
592-
final String workflowId,
593-
final String workflowRunId) {
594-
return doGetWorkflowSearchAttributes(workflowClass, workflowId, workflowRunId, null);
745+
/**
746+
* Describe a workflow to get its info.
747+
* If the workflow does not exist, throw the WORKFLOW_NOT_EXISTS_SUB_STATUS exception.
748+
*
749+
* @param workflowId required
750+
* @return the workflow's info
751+
*/
752+
public WorkflowInfo describeWorkflow(
753+
final String workflowId) {
754+
return describeWorkflow(workflowId, "");
595755
}
596756

597757
private Map<String, Object> doGetWorkflowSearchAttributes(

src/test/java/io/iworkflow/integ/BasicTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void testGetWorkflowStatusWhenWorkflowIsRunning() {
147147
final String wfId = "wf-get-workflow-status-running-test-id" + System.currentTimeMillis() / 1000;
148148

149149
client.startWorkflow(BasicWorkflow.class, wfId, 10, null, null);
150-
final WorkflowInfo workflowInfo = client.describeWorkflow(wfId, "");
150+
final WorkflowInfo workflowInfo = client.describeWorkflow(wfId);
151151
Assertions.assertEquals(WorkflowStatus.RUNNING, workflowInfo.getWorkflowStatus());
152152
}
153153
}

src/test/java/io/iworkflow/integ/NoStartStateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void testNoStateWorkflow() throws InterruptedException {
5757

5858
Assertions.assertEquals(RPC_OUTPUT, rpcOutput);
5959

60-
client.stopWorkflow(wfId, "");
60+
client.stopWorkflow(wfId, null);
6161
}
6262

6363
@Test

src/test/java/io/iworkflow/integ/PersistenceTest.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,43 @@ public void testPersistenceWorkflow() throws InterruptedException {
3333
final String runId = client.startWorkflow(
3434
BasicPersistenceWorkflow.class, wfId, 10, "start");
3535
final String output = client.getSimpleWorkflowResultWithWait(String.class, wfId);
36+
Assertions.assertEquals("test-value-2", output);
37+
3638
Map<String, Object> map =
3739
client.getWorkflowDataObjects(BasicPersistenceWorkflow.class, wfId, runId, Arrays.asList(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
3840
Assertions.assertEquals(
3941
"query-start-query-decide", map.get(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
42+
43+
// test no runId
44+
Map<String, Object> map2 =
45+
client.getWorkflowDataObjects(BasicPersistenceWorkflow.class, wfId, Arrays.asList(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
46+
Assertions.assertEquals(
47+
"query-start-query-decide", map2.get(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
48+
4049
Map<String, Object> allDataObjects = client.getAllDataObjects(BasicPersistenceWorkflow.class, wfId, runId);
4150
Assertions.assertEquals(3, allDataObjects.size());
42-
4351
Assertions.assertEquals("query-start-query-decide", allDataObjects.get(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
4452

45-
Assertions.assertEquals("test-value-2", output);
53+
// test no runId
54+
Map<String, Object> allDataObjects2 = client.getAllDataObjects(BasicPersistenceWorkflow.class, wfId);
55+
Assertions.assertEquals(3, allDataObjects2.size());
56+
Assertions.assertEquals("query-start-query-decide", allDataObjects2.get(BasicPersistenceWorkflow.TEST_DATA_OBJECT_KEY));
4657

4758
final Map<String, Object> searchAttributes1 = client.getWorkflowSearchAttributes(BasicPersistenceWorkflow.class,
4859
wfId, "", Arrays.asList(TEST_SEARCH_ATTRIBUTE_KEYWORD, TEST_SEARCH_ATTRIBUTE_INT));
49-
50-
final Map<String, Object> searchAttributes2 = client.getAllSearchAttributes(BasicPersistenceWorkflow.class,
51-
wfId, "");
52-
5360
Assertions.assertEquals(ImmutableMap.builder()
5461
.put(TEST_SEARCH_ATTRIBUTE_INT, 2L)
5562
.put(TEST_SEARCH_ATTRIBUTE_KEYWORD, "keyword-2")
5663
.build(), searchAttributes1);
5764

65+
// test no runId
66+
final Map<String, Object> searchAttributes2 = client.getWorkflowSearchAttributes(BasicPersistenceWorkflow.class,
67+
wfId, Arrays.asList(TEST_SEARCH_ATTRIBUTE_KEYWORD, TEST_SEARCH_ATTRIBUTE_INT));
68+
Assertions.assertEquals(ImmutableMap.builder()
69+
.put(TEST_SEARCH_ATTRIBUTE_INT, 2L)
70+
.put(TEST_SEARCH_ATTRIBUTE_KEYWORD, "keyword-2")
71+
.build(), searchAttributes2);
72+
5873
// TODO fix
5974
// Expected :{CustomIntField=2, CustomKeywordField=keyword-2, CustomDatetimeField=2023-04-17T21:17:49-00:00}
6075
// Actual :{CustomDatetimeField=2023-04-17T21:17:49Z, CustomIntField=2, CustomKeywordField=keyword-2}

src/test/java/io/iworkflow/integ/RpcTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void testRPCWorkflowFunc0() throws InterruptedException {
9494
final String runId = client.startWorkflow(
9595
RpcWorkflow.class, wfId, 10, 999);
9696

97-
final RpcWorkflow rpcStub = client.newRpcStub(RpcWorkflow.class, wfId, "");
97+
final RpcWorkflow rpcStub = client.newRpcStub(RpcWorkflow.class, wfId);
9898
final Long rpcOutput = client.invokeRPC(rpcStub::testRpcFunc0);
9999

100100
Assertions.assertEquals(RPC_OUTPUT, rpcOutput);
@@ -227,7 +227,7 @@ public void testRpcError() throws InterruptedException {
227227
Assertions.assertEquals("java.lang.RuntimeException", errResp.getOriginalWorkerErrorType());
228228
Assertions.assertEquals("worker API error, status:501, errorType:java.lang.RuntimeException", errResp.getDetail());
229229
}
230-
client.stopWorkflow(wfId, "");
230+
client.stopWorkflow(wfId, null);
231231
}
232232

233233
}

src/test/java/io/iworkflow/integ/SignalTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public void testBasicSignalWorkflow() throws InterruptedException {
3838
client.signalWorkflow(
3939
BasicSignalWorkflow.class, wfId, runId, SIGNAL_CHANNEL_NAME_1, Integer.valueOf(2));
4040

41+
// test no runId
42+
client.signalWorkflow(
43+
BasicSignalWorkflow.class, wfId, SIGNAL_CHANNEL_NAME_1, Integer.valueOf(2));
44+
4145
// test sending null signal
4246
client.signalWorkflow(
4347
BasicSignalWorkflow.class, wfId, runId, SIGNAL_CHANNEL_NAME_3, null);

0 commit comments

Comments
 (0)