Skip to content

Commit

Permalink
autorestart agent svc on windows
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Kolberg <amd.prophet@gmail.com>
  • Loading branch information
amdprophet committed Oct 3, 2022
1 parent d13fe77 commit 26cf64f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion agent/cmd/install_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/eventlog"
Expand Down Expand Up @@ -68,10 +69,24 @@ func installService(name, displayName, desc string, args ...string) error {
return err
}
defer s.Close()

// setup recovery actions (how to handle errors from the agent service)
recoveryActions := []mgr.RecoveryAction{
{
Type: mgr.ServiceRestart,
Delay: 5 * time.Second,
},
}
if err := s.SetRecoveryActions(recoveryActions, 60); err != nil {
s.Delete()
return fmt.Errorf("SetRecoveryActions() failed: %w", err)
}

// setup eventlog
err = eventlog.InstallAsEventCreate(name, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
return fmt.Errorf("SetupEventLogSource() failed: %s", err)
return fmt.Errorf("SetupEventLogSource() failed: %w", err)
}

return s.Start(args...)
Expand Down

0 comments on commit 26cf64f

Please sign in to comment.