From a2aafd15935391ac3517092cbac403f1d7eb21f2 Mon Sep 17 00:00:00 2001 From: Son Roy Almerol Date: Tue, 24 Dec 2024 11:32:04 -0500 Subject: [PATCH] fix version checker loop --- cmd/windows_agent/service.go | 80 ++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/cmd/windows_agent/service.go b/cmd/windows_agent/service.go index fdc719e..f76c744 100644 --- a/cmd/windows_agent/service.go +++ b/cmd/windows_agent/service.go @@ -93,54 +93,56 @@ func (p *agentService) versionCheck() { Version: Version, } - versionCheck := func() { + commonFunc := func() { _, _ = agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/version", nil, &versionResp) - } - - versionCheck() - for { - select { - case <-p.ctx.Done(): - return - case <-time.After(time.Minute * 2): - if versionResp.Version != Version { - var dlResp io.ReadCloser - dlResp, err := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/binary", nil, nil) - if err != nil { - if hasLogger { - logger.Errorf("Update download %s error: %s", versionResp.Version, err) - } - continue + if versionResp.Version != Version { + var dlResp io.ReadCloser + dlResp, err := agent.ProxmoxHTTPRequest(http.MethodGet, "/api2/json/plus/binary", nil, nil) + if err != nil { + if hasLogger { + logger.Errorf("Update download %s error: %s", versionResp.Version, err) } + return + } - closeResp := func() { - _, _ = io.Copy(io.Discard, dlResp) - dlResp.Close() - } + closeResp := func() { + _, _ = io.Copy(io.Discard, dlResp) + dlResp.Close() + } - err = selfupdate.Apply(dlResp, selfupdate.Options{}) - if err != nil { - if hasLogger { - logger.Errorf("Update download %s error: %s", versionResp.Version, err) - } - closeResp() - continue + err = selfupdate.Apply(dlResp, selfupdate.Options{}) + if err != nil { + if hasLogger { + logger.Errorf("Update download %s error: %s", versionResp.Version, err) } + closeResp() + return + } - ex, err := os.Executable() - if err != nil { - if hasLogger { - logger.Errorf("Update download %s error: %s", versionResp.Version, err) - } - closeResp() - continue + ex, err := os.Executable() + if err != nil { + if hasLogger { + logger.Errorf("Update download %s error: %s", versionResp.Version, err) } - - var restartCmd *exec.Cmd - restartCmd = exec.Command(ex, "restart") - restartCmd.Start() + closeResp() + return } + + var restartCmd *exec.Cmd + restartCmd = exec.Command(ex, "restart") + restartCmd.Start() + } + } + + commonFunc() + + for { + select { + case <-p.ctx.Done(): + return + case <-time.After(time.Minute * 2): + commonFunc() } } }