Skip to content

Commit 6b678dc

Browse files
committed
feat: handle ctrl+c in reporting
1 parent c215b5b commit 6b678dc

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

cmd/installer/cli/install.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ type InstallCmdFlags struct {
8282
func InstallCmd(ctx context.Context, name string) *cobra.Command {
8383
var flags InstallCmdFlags
8484

85+
ctx, cancel := context.WithCancel(ctx)
86+
8587
cmd := &cobra.Command{
8688
Use: "install",
8789
Short: fmt.Sprintf("Install %s", name),
@@ -94,13 +96,20 @@ func InstallCmd(ctx context.Context, name string) *cobra.Command {
9496
},
9597
PostRun: func(cmd *cobra.Command, args []string) {
9698
runtimeconfig.Cleanup()
99+
cancel() // Cancel context when command completes
97100
},
98101
RunE: func(cmd *cobra.Command, args []string) error {
99102
clusterID := metrics.ClusterID()
100103
metricsReporter := NewInstallReporter(
101104
replicatedAppURL(), flags.license.Spec.LicenseID, clusterID, cmd.CalledAs(),
102105
)
103106
metricsReporter.ReportInstallationStarted(ctx)
107+
108+
// Setup signal handler with the metrics reporter cleanup function
109+
signalHandler(ctx, cancel, func(ctx context.Context, err error) {
110+
metricsReporter.ReportInstallationFailed(ctx, err)
111+
})
112+
104113
if err := runInstall(cmd.Context(), name, flags, metricsReporter); err != nil {
105114
metricsReporter.ReportInstallationFailed(ctx, err)
106115
return err

cmd/installer/cli/join.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ type JoinCmdFlags struct {
4646
func JoinCmd(ctx context.Context, name string) *cobra.Command {
4747
var flags JoinCmdFlags
4848

49+
ctx, cancel := context.WithCancel(ctx)
50+
4951
cmd := &cobra.Command{
5052
Use: "join <url> <token>",
5153
Short: fmt.Sprintf("Join %s", name),
@@ -61,6 +63,7 @@ func JoinCmd(ctx context.Context, name string) *cobra.Command {
6163
},
6264
PostRun: func(cmd *cobra.Command, args []string) {
6365
runtimeconfig.Cleanup()
66+
cancel() // Cancel context when command completes
6467
},
6568
RunE: func(cmd *cobra.Command, args []string) error {
6669
logrus.Debugf("fetching join token remotely")
@@ -70,6 +73,12 @@ func JoinCmd(ctx context.Context, name string) *cobra.Command {
7073
}
7174
metricsReporter := NewJoinReporter(jcmd.InstallationSpec.MetricsBaseURL, jcmd.ClusterID, cmd.CalledAs())
7275
metricsReporter.ReportJoinStarted(ctx)
76+
77+
// Setup signal handler with the metrics reporter cleanup function
78+
signalHandler(ctx, cancel, func(ctx context.Context, err error) {
79+
metricsReporter.ReportJoinFailed(ctx, err)
80+
})
81+
7382
if err := runJoin(cmd.Context(), name, flags, jcmd, metricsReporter); err != nil {
7483
metricsReporter.ReportJoinFailed(ctx, err)
7584
return err

cmd/installer/cli/signal.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
10+
"github.com/sirupsen/logrus"
11+
)
12+
13+
// signalHandler sets up handling for signals to ensure cleanup functions are called.
14+
func signalHandler(ctx context.Context, cancel context.CancelFunc, cleanupFuncs ...func(context.Context, error)) {
15+
sigChan := make(chan os.Signal, 1)
16+
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
17+
18+
go func() {
19+
select {
20+
case sig := <-sigChan:
21+
logrus.Debugf("Received signal: %v", sig)
22+
err := fmt.Errorf("command interrupted by signal: %v", sig)
23+
24+
for _, cleanup := range cleanupFuncs {
25+
cleanup(ctx, err)
26+
}
27+
28+
// Cancel the context after cleanup functions run
29+
cancel()
30+
31+
// Exit with non-zero status
32+
os.Exit(1)
33+
case <-ctx.Done():
34+
// Context was canceled elsewhere, do nothing
35+
return
36+
}
37+
}()
38+
}

0 commit comments

Comments
 (0)