Query status of workflow from another workflow

Hi,
I am trying to query the status of a workflow from another workflow for logging purposes. It is a production environment where one Temporal server and one temporal worker are run in separate container in a kubernetes environment. Using the examples

[samples-java/core/src/main/java/io/temporal/samples/hello/HelloSearchAttributes.java at main · temporalio/samples-java · GitHub](https://temporal sample code)
How can I get a workflow state/status from an external application in java?

I wrote the following snippet

WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
 WorkflowServiceGrpc.WorkflowServiceBlockingStub stub = service.blockingStub();
 DescribeWorkflowExecutionRequest request = DescribeWorkflowExecutionRequest.newBuilder()
                .setExecution(WorkflowExecution.newBuilder().setWorkflowId(workflowId))
                .build();
 DescribeWorkflowExecutionResponse response = stub.describeWorkflowExecution(request);
 WorkflowExecutionStatus workflowstatus = response.getWorkflowExecutionInfo().getStatus();

But when i run the code it gives a connection exception that says 127.0.0.1:7223 connection refused error.
Then i went through the temporal code and realized that its the default temporal option and that probably we need to give temporal server IP/Port to query. Then i wrote the following code

WorkflowServiceStubs service = new WorkflowServiceStubsImpl(null,
              WorkflowServiceStubsOptions.newBuilder().setTarget(<TemporalServerIP> + ":" + <temporalPort>).build());
 WorkflowServiceGrpc.WorkflowServiceBlockingStub stub = service.blockingStub();
 DescribeWorkflowExecutionRequest request = DescribeWorkflowExecutionRequest.newBuilder()
                .setExecution(WorkflowExecution.newBuilder().setWorkflowId(workflowId))
                .setNamespace("default")
                .build();
 DescribeWorkflowExecutionResponse response = stub.describeWorkflowExecution(request);
 workflowstatus = response.getWorkflowExecutionInfo().getStatus();

The above code works. But the question is

  1. Even if all the other workflows are running in the same worker, do we still need to provide the temporal server IP/Port for doing the status query? Since this code is executed from a workflow, my assumption, the temporal server details would be available to it already and there is no need to send it separately(like in second snippet). We do not set the temporal IP/PORT when we trigger child workflow from the parent workflow. Why this API requires it?
  2. Is there a problem with the first approach or it works only in the test environment? Did i make any mistake in the first block of code?

I assume you are doing this from activity code so don’t have to create new service stubs each time (it’s also not recommended) but could pass it to your activity as input when you register the activity with your worker.

What’s your use case for needing to get the execution status of the other execution?

@tihomir thanks for your reply!
The use case is:
our system runs Temporal workflows for executing business tasks. but the UI represents the workflows as application objects(the temporal UI is hidden from user). But sometimes, the temporal workflows fail and the application objects representing temporal workflows are not updated properly(like re-start/crash etc). in such cases we need a way to update the application workflow objects as errored.
So we wrote another workflow(say ‘cleanupworkflow’ )- it queries to see if the application object says the workflow is in progress, whether the temporal workflow is actually live in the background. if not we mark the application objects as errored. This is why we need to query the status of the workflow(actually executing business logic) from another workflow(cleanupworkflow)

I have one more question related to this. can this query of the another workflows’ status be done in the

  1. workflow itself - the workflow code creates new service stubs and triggers
  2. Activity within the workflow?
    I mean can this piece of code
DescribeWorkflowExecutionRequest request = DescribeWorkflowExecutionRequest.newBuilder()
                .setExecution(WorkflowExecution.newBuilder().setWorkflowId(workflowId))
                .setNamespace("default")
                .build();
 DescribeWorkflowExecutionResponse response = stub.describeWorkflowExecution(request);
 workflowstatus = response.getWorkflowExecutionInfo().getStatus();

be put in the activity or can be put inside the workflow?

No, any external calls (which includes calls through Temporal Client) must be executed by activities only.