Skip to content

Commit

Permalink
Merge pull request #8 from sonroyaalmerol/bugfix/autoupdate
Browse files Browse the repository at this point in the history
fix version checker loop
  • Loading branch information
sonroyaalmerol authored Dec 24, 2024
2 parents 2c6c560 + a2aafd1 commit 6778c15
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions cmd/windows_agent/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
Expand Down

0 comments on commit 6778c15

Please sign in to comment.