Skip to content

Commit 95db90b

Browse files
authored
Add describeWorkflow method in Client (#184)
Co-authored-by: Kaili Zhu <kzhu@indeed.com>
1 parent aa6b166 commit 95db90b

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.iworkflow.gen.models.SearchAttributeValueType;
88
import io.iworkflow.gen.models.StateCompletionOutput;
99
import io.iworkflow.gen.models.WorkflowGetDataObjectsResponse;
10+
import io.iworkflow.gen.models.WorkflowGetResponse;
1011
import io.iworkflow.gen.models.WorkflowGetSearchAttributesResponse;
1112
import io.iworkflow.gen.models.WorkflowSearchRequest;
1213
import io.iworkflow.gen.models.WorkflowSearchResponse;
@@ -569,6 +570,23 @@ public Map<String, Object> getWorkflowSearchAttributes(
569570
return doGetWorkflowSearchAttributes(workflowClass, workflowId, workflowRunId, attributeKeys);
570571
}
571572

573+
/**
574+
* Describe a workflow to get its info.
575+
* If the workflow does not exist, throw the WORKFLOW_NOT_EXISTS_SUB_STATUS exception.
576+
*
577+
* @param workflowId required
578+
* @param workflowRunId optional
579+
* @return the workflow's info
580+
*/
581+
public WorkflowInfo describeWorkflow(
582+
final String workflowId,
583+
final String workflowRunId) {
584+
final WorkflowGetResponse response = unregisteredClient.getWorkflow(workflowId, workflowRunId, false);
585+
return WorkflowInfo.builder()
586+
.workflowStatus(response.getWorkflowStatus())
587+
.build();
588+
}
589+
572590
public Map<String, Object> getAllSearchAttributes(
573591
final Class<? extends ObjectWorkflow> workflowClass,
574592
final String workflowId,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,31 @@ public WorkflowGetDataObjectsResponse getAnyWorkflowDataObjects(
403403
return getAnyWorkflowDataObjects(workflowId, workflowRunId, attributeKeys, false);
404404
}
405405

406+
/**
407+
* Get a workflow's status and results(if completed &amp; requested).
408+
* If the workflow does not exist, throw the WORKFLOW_NOT_EXISTS_SUB_STATUS exception.
409+
*
410+
* @param workflowId required
411+
* @param workflowRunId optional
412+
* @param needsResults result will be returned if this is true and workflow is completed
413+
* @return the workflow's status and results
414+
*/
415+
public WorkflowGetResponse getWorkflow(
416+
final String workflowId,
417+
final String workflowRunId,
418+
final Boolean needsResults) {
419+
try {
420+
return defaultApi.apiV1WorkflowGetPost(
421+
new WorkflowGetRequest()
422+
.workflowId(workflowId)
423+
.workflowRunId(workflowRunId)
424+
.needsResults(needsResults)
425+
);
426+
} catch (final FeignException.FeignClientException exp) {
427+
throw IwfHttpException.fromFeignException(clientOptions.getObjectEncoder(), exp);
428+
}
429+
}
430+
406431
public WorkflowGetDataObjectsResponse getAnyWorkflowDataObjects(
407432
final String workflowId,
408433
final String workflowRunId,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.iworkflow.core;
2+
3+
import io.iworkflow.gen.models.WorkflowStatus;
4+
import org.immutables.value.Value;
5+
6+
@Value.Immutable
7+
public abstract class WorkflowInfo {
8+
public abstract WorkflowStatus getWorkflowStatus();
9+
10+
public static ImmutableWorkflowInfo.Builder builder() {
11+
return ImmutableWorkflowInfo.builder();
12+
}
13+
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import io.iworkflow.core.ImmutableWorkflowOptions;
88
import io.iworkflow.core.UnregisteredWorkflowOptions;
99
import io.iworkflow.core.WorkflowDefinitionException;
10+
import io.iworkflow.core.WorkflowInfo;
1011
import io.iworkflow.core.WorkflowOptions;
1112
import io.iworkflow.gen.models.Context;
1213
import io.iworkflow.gen.models.ErrorSubStatus;
1314
import io.iworkflow.gen.models.IDReusePolicy;
1415
import io.iworkflow.gen.models.WorkflowConfig;
16+
import io.iworkflow.gen.models.WorkflowStatus;
1517
import io.iworkflow.integ.basic.BasicWorkflow;
1618
import io.iworkflow.integ.basic.EmptyInputWorkflow;
1719
import io.iworkflow.integ.basic.EmptyInputWorkflowState1;
@@ -125,4 +127,27 @@ public void testWorkflowConfigOverride() throws InterruptedException {
125127
final Integer output = client.getSimpleWorkflowResultWithWait(Integer.class, wfId);
126128
Assertions.assertEquals(input + 2, output);
127129
}
130+
131+
@Test
132+
public void testGetWorkflowStatusWhenNoExistingWorkflow() {
133+
final Client client = new Client(WorkflowRegistry.registry, ClientOptions.localDefault);
134+
final String wfId = "wf-get-workflow-status-test-id" + System.currentTimeMillis() / 1000;
135+
136+
try {
137+
client.describeWorkflow(wfId, "");
138+
} catch (final ClientSideException e) {
139+
Assertions.assertEquals(ErrorSubStatus.WORKFLOW_NOT_EXISTS_SUB_STATUS, e.getErrorSubStatus());
140+
Assertions.assertEquals(400, e.getStatusCode());
141+
}
142+
}
143+
144+
@Test
145+
public void testGetWorkflowStatusWhenWorkflowIsRunning() {
146+
final Client client = new Client(WorkflowRegistry.registry, ClientOptions.localDefault);
147+
final String wfId = "wf-get-workflow-status-running-test-id" + System.currentTimeMillis() / 1000;
148+
149+
client.startWorkflow(BasicWorkflow.class, wfId, 10, null, null);
150+
final WorkflowInfo workflowInfo = client.describeWorkflow(wfId, "");
151+
Assertions.assertEquals(WorkflowStatus.RUNNING, workflowInfo.getWorkflowStatus());
152+
}
128153
}

0 commit comments

Comments
 (0)