-
Notifications
You must be signed in to change notification settings - Fork 84
feat: add --reverse flag to temporal workflow show
#988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -707,16 +707,24 @@ func coloredEventType(e enums.EventType) string { | |
| type structuredHistoryIter struct { | ||
| ctx context.Context | ||
| client client.Client | ||
| namespace string | ||
| workflowID string | ||
| runID string | ||
| includeDetails bool | ||
| // If set true, long poll the history for updates | ||
| follow bool | ||
| // If set true, fetch history newest-event-first via GetWorkflowExecutionHistoryReverse | ||
| reverse bool | ||
| // If and when the iterator encounters a workflow-terminating event, it will store it here | ||
| wfResult *history.HistoryEvent | ||
|
|
||
| // Internal | ||
| // Internal (forward) | ||
| iter client.HistoryEventIterator | ||
|
|
||
| // Internal (reverse) | ||
| reverseBuf []*history.HistoryEvent | ||
| reverseNextToken []byte | ||
| reverseStarted bool | ||
| } | ||
|
|
||
| func (s *structuredHistoryIter) print(cctx *CommandContext) error { | ||
|
|
@@ -784,15 +792,20 @@ func (s *structuredHistoryIter) Next() (any, error) { | |
| Type: coloredEventType(event.EventType), | ||
| } | ||
|
|
||
| // Follow continue as new | ||
| if attr := event.GetWorkflowExecutionContinuedAsNewEventAttributes(); attr != nil { | ||
| s.runID = attr.NewExecutionRunId | ||
| s.iter = nil | ||
| // Follow continue as new (forward only; reverse traversal stays within the requested run) | ||
| if !s.reverse { | ||
| if attr := event.GetWorkflowExecutionContinuedAsNewEventAttributes(); attr != nil { | ||
| s.runID = attr.NewExecutionRunId | ||
| s.iter = nil | ||
| } | ||
| } | ||
| return data, nil | ||
| } | ||
|
|
||
| func (s *structuredHistoryIter) NextRawEvent() (*history.HistoryEvent, error) { | ||
| if s.reverse { | ||
| return s.nextRawEventReverse() | ||
| } | ||
| // Load iter | ||
| if s.iter == nil { | ||
| s.iter = s.client.GetWorkflowHistory( | ||
|
|
@@ -811,6 +824,39 @@ func (s *structuredHistoryIter) NextRawEvent() (*history.HistoryEvent, error) { | |
| return event, nil | ||
| } | ||
|
|
||
| func (s *structuredHistoryIter) nextRawEventReverse() (*history.HistoryEvent, error) { | ||
| for len(s.reverseBuf) == 0 { | ||
| if s.reverseStarted && len(s.reverseNextToken) == 0 { | ||
| return nil, nil | ||
| } | ||
| s.reverseStarted = true | ||
| resp, err := s.client.WorkflowService().GetWorkflowExecutionHistoryReverse( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the default page size. For large history would be nice to allow customers to provide a custom size.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the max page size configured on the server side is 256 (batches) I think. |
||
| s.ctx, | ||
| &workflowservice.GetWorkflowExecutionHistoryReverseRequest{ | ||
| Namespace: s.namespace, | ||
| Execution: &common.WorkflowExecution{ | ||
| WorkflowId: s.workflowID, | ||
| RunId: s.runID, | ||
| }, | ||
| NextPageToken: s.reverseNextToken, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| s.reverseNextToken = resp.GetNextPageToken() | ||
| if h := resp.GetHistory(); h != nil { | ||
| s.reverseBuf = h.GetEvents() | ||
| } | ||
| } | ||
| event := s.reverseBuf[0] | ||
| s.reverseBuf = s.reverseBuf[1:] | ||
| if isWorkflowTerminatingEvent(event.EventType) { | ||
| s.wfResult = event | ||
| } | ||
| return event, nil | ||
| } | ||
|
|
||
| type eventFieldValue struct { | ||
| field string | ||
| value string | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -420,6 +420,93 @@ func (s *SharedServerSuite) TestWorkflow_Show_JSON() { | |
| s.NotContains(out, "Results:") | ||
| } | ||
|
|
||
| func (s *SharedServerSuite) TestWorkflow_Show_Reverse() { | ||
| s.Worker().OnDevWorkflow(func(ctx workflow.Context, a any) (any, error) { | ||
| workflow.GetSignalChannel(ctx, "my-signal").Receive(ctx, nil) | ||
| return "hi!", nil | ||
| }) | ||
|
|
||
| run, err := s.Client.ExecuteWorkflow( | ||
| s.Context, | ||
| client.StartWorkflowOptions{TaskQueue: s.Worker().Options.TaskQueue}, | ||
| DevWorkflow, | ||
| "ignored", | ||
| ) | ||
| s.NoError(err) | ||
| s.NoError(s.Client.SignalWorkflow(s.Context, run.GetID(), "", "my-signal", nil)) | ||
| s.NoError(run.Get(s.Context, nil)) | ||
|
|
||
| res := s.Execute( | ||
| "workflow", "show", | ||
| "--address", s.Address(), | ||
| "-w", run.GetID(), | ||
| "--reverse", | ||
| ) | ||
| s.NoError(res.Err) | ||
| out := res.Stdout.String() | ||
| completedIdx := strings.Index(out, "WorkflowExecutionCompleted") | ||
| startedIdx := strings.Index(out, "WorkflowExecutionStarted") | ||
| s.Greater(completedIdx, -1, "output should include the completed event") | ||
| s.Greater(startedIdx, -1, "output should include the started event") | ||
| s.Less(completedIdx, startedIdx, "completed event should appear before started event in reverse order") | ||
| } | ||
|
|
||
| func (s *SharedServerSuite) TestWorkflow_Show_Reverse_JSON() { | ||
| s.Worker().OnDevWorkflow(func(ctx workflow.Context, a any) (any, error) { | ||
| workflow.GetSignalChannel(ctx, "my-signal").Receive(ctx, nil) | ||
| return "hi!", nil | ||
| }) | ||
|
|
||
| run, err := s.Client.ExecuteWorkflow( | ||
| s.Context, | ||
| client.StartWorkflowOptions{TaskQueue: s.Worker().Options.TaskQueue}, | ||
| DevWorkflow, | ||
| "workflow-param", | ||
| ) | ||
| s.NoError(err) | ||
| s.NoError(s.Client.SignalWorkflow(s.Context, run.GetID(), "", "my-signal", nil)) | ||
| s.NoError(run.Get(s.Context, nil)) | ||
|
|
||
| res := s.Execute( | ||
| "workflow", "show", | ||
| "--address", s.Address(), | ||
| "-w", run.GetID(), | ||
| "--reverse", | ||
| "-o", "json", | ||
| ) | ||
| s.NoError(res.Err) | ||
| out := res.Stdout.String() | ||
|
|
||
| var parsed struct { | ||
| Events []struct { | ||
| EventId string `json:"eventId"` | ||
| } `json:"events"` | ||
| } | ||
| s.NoError(json.Unmarshal([]byte(out), &parsed)) | ||
| s.GreaterOrEqual(len(parsed.Events), 2) | ||
| prev := int64(-1) | ||
| for i, e := range parsed.Events { | ||
| id, err := strconv.ParseInt(e.EventId, 10, 64) | ||
| s.NoError(err) | ||
| if i > 0 { | ||
| s.Less(id, prev, "event %d (id=%d) should have smaller eventId than previous (%d) in reverse order", i, id, prev) | ||
| } | ||
| prev = id | ||
| } | ||
| } | ||
|
|
||
| func (s *SharedServerSuite) TestWorkflow_Show_Reverse_RejectsFollow() { | ||
| res := s.Execute( | ||
| "workflow", "show", | ||
| "--address", s.Address(), | ||
| "-w", "does-not-matter", | ||
| "--reverse", | ||
| "--follow", | ||
| ) | ||
| s.Error(res.Err) | ||
| s.ErrorContains(res.Err, "--reverse cannot be combined with --follow") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not something for this PR, but it would be nice to have a way to generate MarkFlagsMutuallyExclusive in the yaml file below. Seems like this is not currently supported for auto-gen but something the under lying framework supports. |
||
| } | ||
|
|
||
| func (s *SharedServerSuite) TestWorkflow_List() { | ||
| s.Worker().OnDevWorkflow(func(ctx workflow.Context, a any) (any, error) { | ||
| return a, nil | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.