Thanks for getting back to me. The reason why i was trying to avoid fetching the result is because i might have 100s of workflow runs using a certain workflowID so i don’t want to make 100 getResult calls to temporal cloud.
This is the workflow code
func QueryWorkflow(ctx workflow.Context) (QueryReturn, error) {
resp := QueryReturn{}
resp.QueryResult = "started"
logger := workflow.GetLogger(ctx)
logger.Info("QueryWorkflow started")
// setup query handler for query type "state"
err := workflow.SetQueryHandler(ctx, "state", func() (string, error) {
return resp.QueryResult, nil
})
if err != nil {
logger.Info("SetQueryHandler failed: " + err.Error())
return resp, err
}
resp.QueryResult = "waiting on timer"
_ = workflow.NewTimer(ctx, time.Second*120).Get(ctx, nil)
logger.Info("Timer fired")
resp.QueryResult = "donezo"
memo := map[string]interface{}{
"queryResult": resp.QueryResult,
}
err = workflow.UpsertMemo(ctx, memo)
if err != nil {
return resp, err
}
return resp, nil
}
This is how im starting the workflow, and i’ve fired off a bunch.
workflowOptions := client.StartWorkflowOptions{
ID: "query_workflow",
TaskQueue: "query",
}
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, query.QueryWorkflow)
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
This is how im fetching the workflow results
func main() {
ctx := context.Background()
// The client is a heavyweight object that should be created once per process.
c, err := client.Dial(client.Options{
HostPort: client.DefaultHostPort,
})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()
// Get list of workflows
queryStr := "WorkflowId='query_workflow'"
results, err := GetWorkflows(ctx, c, queryStr)
if err != nil {
log.Fatalln("Unable to get workflows", err)
}
// Get result of each workflow
for _, execution := range results {
exec := execution.Execution
log.Println("WorkflowID", exec.GetWorkflowId(), "RunID", exec.GetRunId())
// Query each workflow to get the result
q, err := c.QueryWorkflow(ctx, exec.GetWorkflowId(), exec.GetRunId(), "state")
if err != nil {
log.Fatalln("Unable to query workflow", err)
}
var result string
if err := q.Get(&result); err != nil {
log.Fatalln("Unable to decode query result", err)
}
log.Println("Received query result", "Result", result)
// Or we can get the result that's stored in the memo
log.Println("Memo", execution.Memo)
}
}
func GetWorkflows(ctx context.Context, c client.Client, query string) ([]*workflowpb.WorkflowExecutionInfo, error) {
var nextPageToken []byte
var workflowExecutions []*workflowpb.WorkflowExecutionInfo
for {
resp, err := c.ListWorkflow(ctx, &workflowservice.ListWorkflowExecutionsRequest{
Query: query,
NextPageToken: nextPageToken,
})
if err != nil {
return nil, err
}
workflowExecutions = append(workflowExecutions, resp.Executions...)
nextPageToken = resp.NextPageToken
if len(nextPageToken) == 0 {
return workflowExecutions, nil
}
}
}
What im finding is if i change QueryResult
to be done
instead of donezo
and restart my worker and start a new workflow then run the main function above the query for all workflow runs returns done
for queries of all workflow runs instead of just the ones run after workflow was updated