Skip to content

Commit a991af8

Browse files
committed
update await terminal
1 parent 1ec979b commit a991af8

File tree

1 file changed

+56
-9
lines changed

1 file changed

+56
-9
lines changed

internal/actions/await_terminal_autofix_request_status.go

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package actions
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"time"
@@ -10,15 +11,17 @@ import (
1011
)
1112

1213
type AwaitTerminalAutofixRequestStatusAction struct {
13-
OnFailureActions []Action
14-
OnSuccessActions []Action
15-
SubmissionID string
14+
InProgressActions []Action
15+
OnFailureActions []Action
16+
OnSuccessActions []Action
17+
SubmissionID string
1618
}
1719

1820
type AwaitTerminalAutofixRequestStatusActionArgs struct {
19-
OnFailureActions []client.ActionDefinition `json:"on_failure_actions"`
20-
OnSuccessActions []client.ActionDefinition `json:"on_success_actions"`
21-
SubmissionID string `json:"submission_id"`
21+
InProgressActions []client.ActionDefinition `json:"in_progress_actions"`
22+
OnFailureActions []client.ActionDefinition `json:"on_failure_actions"`
23+
OnSuccessActions []client.ActionDefinition `json:"on_success_actions"`
24+
SubmissionID string `json:"submission_id"`
2225
}
2326

2427
func NewAwaitTerminalAutofixRequestStatusAction(argsJson json.RawMessage) (*AwaitTerminalAutofixRequestStatusAction, error) {
@@ -47,17 +50,41 @@ func NewAwaitTerminalAutofixRequestStatusAction(argsJson json.RawMessage) (*Awai
4750
onFailureActions = append(onFailureActions, action)
4851
}
4952

53+
inProgressActions := []Action{}
54+
for _, actionDefinition := range awaitTerminalAutofixRequestStatusActionArgs.InProgressActions {
55+
action, err := ActionFromDefinition(actionDefinition)
56+
if err != nil {
57+
return nil, err
58+
}
59+
60+
inProgressActions = append(inProgressActions, action)
61+
}
62+
5063
return &AwaitTerminalAutofixRequestStatusAction{
51-
OnFailureActions: onFailureActions,
52-
OnSuccessActions: onSuccessActions,
53-
SubmissionID: awaitTerminalAutofixRequestStatusActionArgs.SubmissionID,
64+
InProgressActions: inProgressActions,
65+
OnFailureActions: onFailureActions,
66+
OnSuccessActions: onSuccessActions,
67+
SubmissionID: awaitTerminalAutofixRequestStatusActionArgs.SubmissionID,
5468
}, nil
5569
}
5670

5771
func (a *AwaitTerminalAutofixRequestStatusAction) Execute() error {
5872
attempts := 0
5973
autofixRequestStatus := "in_progress"
6074

75+
inProgressActionsDoneCh := make(chan bool)
76+
77+
ctx, cancel := context.WithCancel(context.Background())
78+
defer cancel()
79+
80+
go func() {
81+
if err := a.executeInProgressActions(ctx); err != nil {
82+
sentry.CaptureException(err)
83+
}
84+
85+
inProgressActionsDoneCh <- true
86+
}()
87+
6188
// We wait for upto 60 seconds (+ the time it takes to fetch status each time)
6289
for autofixRequestStatus == "in_progress" && attempts < 60 {
6390
var err error
@@ -75,6 +102,10 @@ func (a *AwaitTerminalAutofixRequestStatusAction) Execute() error {
75102
time.Sleep(time.Second)
76103
}
77104

105+
// Ensure interruptible actions (like printing progress bars) finish early
106+
cancel()
107+
<-inProgressActionsDoneCh
108+
78109
switch autofixRequestStatus {
79110
case "success":
80111
for _, action := range a.OnSuccessActions {
@@ -101,3 +132,19 @@ func (a *AwaitTerminalAutofixRequestStatusAction) Execute() error {
101132

102133
return nil
103134
}
135+
136+
func (a *AwaitTerminalAutofixRequestStatusAction) executeInProgressActions(ctx context.Context) error {
137+
for _, action := range a.InProgressActions {
138+
if interruptibleAction, ok := action.(InterruptibleAction); ok {
139+
if err := interruptibleAction.ExecuteWithContext(ctx); err != nil {
140+
return err
141+
}
142+
} else {
143+
if err := action.Execute(); err != nil {
144+
return err
145+
}
146+
}
147+
}
148+
149+
return nil
150+
}

0 commit comments

Comments
 (0)