@@ -2,16 +2,15 @@ package cmd
22
33import (
44 "bufio"
5+ "context"
56 "encoding/json"
67 "fmt"
78 "log"
89 "math/rand"
910 "net/http"
1011 "os"
11- "os/signal"
1212 "runtime"
1313 "strings"
14- "syscall"
1514 "time"
1615
1716 "github.com/alt-dima/iacconsole-cli/utils"
@@ -44,25 +43,23 @@ var agentCmd = &cobra.Command{
4443 log .Printf ("Agent ID: %s" , agentID )
4544 log .Printf ("Connecting to: %s" , wsURL )
4645
47- runAgent (wsURL , authHeader , agentID )
46+ runAgent (cmd . Context (), wsURL , authHeader , agentID )
4847 },
4948}
5049
51- func runAgent (wsURL , authHeader , agentID string ) {
52- interrupt := make (chan os.Signal , 1 )
53- signal .Notify (interrupt , os .Interrupt , syscall .SIGTERM )
54-
50+ func runAgent (ctx context.Context , wsURL , authHeader , agentID string ) {
5551 for {
5652 header := http.Header {}
5753 header .Add ("Authorization" , authHeader )
5854
59- c , _ , err := websocket .DefaultDialer .Dial (wsURL , header )
55+ dialer := websocket .DefaultDialer
56+ c , _ , err := dialer .DialContext (ctx , wsURL , header )
6057 if err != nil {
6158 log .Printf ("Dial error: %v. Retrying in 5s..." , err )
6259 select {
6360 case <- time .After (5 * time .Second ):
6461 continue
65- case <- interrupt :
62+ case <- ctx . Done () :
6663 return
6764 }
6865 }
@@ -140,7 +137,7 @@ func runAgent(wsURL, authHeader, agentID string) {
140137 continue
141138 }
142139 // log.Printf("Received command: %+v", cmd)
143- go executeCommand (c , cmd , autoExecute )
140+ go executeCommand (ctx , c , cmd , autoExecute )
144141 case "ping" :
145142 pong := utils.AgentPong {
146143 AgentMessage : utils.AgentMessage {Type : "pong" },
@@ -158,8 +155,12 @@ func runAgent(wsURL, authHeader, agentID string) {
158155 case <- done :
159156 c .Close ()
160157 log .Printf ("Connection lost. Retrying in 5s..." )
161- time .Sleep (5 * time .Second )
162- case <- interrupt :
158+ select {
159+ case <- time .After (5 * time .Second ):
160+ case <- ctx .Done ():
161+ return
162+ }
163+ case <- ctx .Done ():
163164 log .Println ("Interrupt received, closing connection..." )
164165 // Cleanly close the connection by sending a close message and then
165166 // waiting (with timeout) for the server to close the connection.
@@ -177,7 +178,7 @@ func runAgent(wsURL, authHeader, agentID string) {
177178 }
178179}
179180
180- func executeCommand (c * websocket.Conn , cmd utils.AgentCommand , autoExecute bool ) {
181+ func executeCommand (ctx context. Context , c * websocket.Conn , cmd utils.AgentCommand , autoExecute bool ) {
181182 // Format command for display
182183 cmdStr := formatCommandString (cmd )
183184
@@ -231,7 +232,7 @@ func executeCommand(c *websocket.Conn, cmd utils.AgentCommand, autoExecute bool)
231232 state .IacconsoleApiUrl = os .Getenv ("IACCONSOLE_API_URL" )
232233 state .StateS3Path = "./state"
233234
234- utils .ExecuteAgentCommand (c , cmd , state )
235+ utils .ExecuteAgentCommand (ctx , c , cmd , state )
235236}
236237
237238// formatCommandString creates a human-readable string representation of the command
0 commit comments