Skip to content

Commit fa40463

Browse files
authored
Call patch invoke on control + c (#15)
<!-- mesa-description-start --> ## TL;DR Handles `Control+C` during an `invoke` command to gracefully terminate the remote session by updating its status to `failed`. ## Why we made these changes Previously, interrupting an `invoke` command with `Control+C` would only terminate the local CLI process, leaving the remote session running. This could lead to orphaned resources and unexpected behavior. This change ensures that when a user cancels an operation, the remote session is properly cleaned up. ## What changed? - **`cmd/invoke.go`**: - Implemented a signal handler to catch `SIGINT` (`Control+C`). - On cancellation, the CLI now sends a patch request to mark the remote invocation as 'failed' with a user-cancelled message. - Reordered the invocation creation process to ensure the `invocationID` is available immediately for the cancellation handler. <sup>_Description generated by Mesa. [Update settings](https://app.mesa.dev/onkernel/settings/pull-requests)_</sup> <!-- mesa-description-end -->
1 parent 000a938 commit fa40463

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cmd/invoke.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,23 @@ func runInvoke(cmd *cobra.Command, args []string) error {
9292
return nil
9393
}
9494

95-
// this is a little indirect but we try to fail out of the invocation by deleting the
96-
// underlying browser sessions
95+
// On cancel, mark the invocation as failed via the update endpoint
9796
once := sync.Once{}
9897
onCancel(cmd.Context(), func() {
9998
once.Do(func() {
10099
cleanupStarted.Store(true)
101100
defer close(cleanupDone)
102101
pterm.Warning.Println("Invocation cancelled...cleaning up...")
103-
if err := client.Invocations.DeleteBrowsers(context.Background(), resp.ID, option.WithRequestTimeout(30*time.Second)); err != nil {
104-
pterm.Error.Printf("Failed to cancel invocation: %v\n", err)
102+
if _, err := client.Invocations.Update(
103+
context.Background(),
104+
resp.ID,
105+
kernel.InvocationUpdateParams{
106+
Status: kernel.InvocationUpdateParamsStatusFailed,
107+
Output: kernel.Opt(`{"error":"Invocation cancelled by user"}`),
108+
},
109+
option.WithRequestTimeout(30*time.Second),
110+
); err != nil {
111+
pterm.Error.Printf("Failed to mark invocation as failed: %v\n", err)
105112
}
106113
})
107114
})

0 commit comments

Comments
 (0)