How do I know if a started cron workflow is running at the moment given the workflowID?

I’ve started an cron workflow, if I describeWorkflowExecution and get WorkflowExecutionStatus, the workflow is WORKFLOW_EXECUTION_STATUS_RUNNING, but it does not really indicated the cron is running at the moment but it is scheduled to run on the next schedule.
I’d like to know if the workflow is running or waiting to run.

Hi @ping

The workflow Running status for an execution is not dependent of your worker actually processing your workflow code.
The workflow execution is created upon client request. For cron workflows the execution itself is started right away (either on first client request, or as soon as the previous cron exec completes) and Temporal calculates the firstWorkflowTaskBackoff property of the first WorkflowExecutionStarted event based on the configured cron definition.

I think what you are looking for is to know if your worker has actually started processing your workflow code. For this case you would need to look at the workflow history and see if it contains a
WorkflowTaskStarted event, it would indicate that your worker has picked up the scheduled workflow task and has started processing your workflow code.

Which SDK are you using? You could use GetWorkflowExecutionHistory api to get the workflow history and then check if the first WorkflowExecutionStarted event exists.

I’m using Java, let me try your idea

Could be something like this:

GetWorkflowExecutionHistoryRequest req = GetWorkflowExecutionHistoryRequest.newBuilder()
                .setNamespace(client.getOptions().getNamespace())
                .setExecution(wfExec)
                .build();
        GetWorkflowExecutionHistoryResponse res =
                service.blockingStub().getWorkflowExecutionHistory(req);

        for(HistoryEvent he : res.getHistory().getEventsList()) {
            if(he.getEventType().equals(EventType.EVENT_TYPE_WORKFLOW_TASK_STARTED)) {
                  // worker started processing workflow code...can return or whatever you need here
           }
        }

You can create your WorkflowExecution via:

WorkflowExecution.newBuilder()
                .setWorkflowId("<workflow_id>")
                .setRunId("<workflow_run_id>")
                .build();

thank you, @tihomir
I try to check both EVENT_TYPE_WORKFLOW_EXECUTION_STARTED and EVENT_TYPE_WORKFLOW_TASK_STARTED. It seems necessary in my sample, which indicated the actual execution of workflow.

I would use DescribeWorkflowExecution API call. The workflow_execution_info. execution_time contains the time the first workflow task is going to be created.

Yes Maxims answer is better for this case. Thanks!

Just to add execution time is in epoch time, you would need to convert it to local time if you wanted to compare it with local dates (to see if its in the future). Here is small example that converts it to string:

DescribeWorkflowExecutionRequest req = DescribeWorkflowExecutionRequest.newBuilder()
                .setExecution(wfExec)
                .setNamespace(client.getOptions().getNamespace())
                .build();

        DescribeWorkflowExecutionResponse res = service.blockingStub().describeWorkflowExecution(req);
        Timestamp ts = res.getWorkflowExecutionInfo().getExecutionTime();

        String execLocalTimeAsString = Instant.ofEpochSecond( ts.getSeconds() , ts.getNanos() )
                .atZone( ZoneId.of( "America/New_York" ) )
                .toLocalDateTime().format(new DateTimeFormatterBuilder()
                        .appendPattern("yyyy-MM-dd hh:mm:ss")
                .toFormatter());

thank you @tihomir, @maxim
my final purpose is to call workflow query handler (cron workflow with query handler in golang) with Java SDK. As there’s issue causing worker panic (Eagerly error/return on queries for workflows waiting to start · Issue #2300 · temporalio/temporal · GitHub), I have to make sure workflow is running physically when the query handler is being called.
I’ve put above logic in Java app, and it looks good so far (no panic).