Skip to content

Commit

Permalink
Add RestartServiceWithVerify to helpers_windows.go
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684855644
  • Loading branch information
Glazier Bot authored and copybara-github committed Oct 11, 2024
1 parent a5e7c6e commit 3b1370f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions go/helpers/helpers_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ func RestartService(name string) error {
return s.Start()
}

// RestartServiceWithVerify attempts to restart local system services and verifies the service is running with a 60 second timeout.
func RestartServiceWithVerify(name string) error {
if err := RestartService(name); err != nil {
return err
}
status := svc.Status{
State: svc.StartPending, // Assume the service is starting
}
for retry := 0; status.State == svc.StartPending; {
deck.Infof("Waiting for service %q to start, sleeping for 5 seconds", name)
time.Sleep(5 * time.Second)
var err error
status, _, err = GetServiceState(name)
if err != nil {
return err
}
retry++
if retry > 12 {
return fmt.Errorf("timed out waiting for service %q to start", name)
}
}
if status.State != svc.Running {
return fmt.Errorf("service %q is not running after restart, current state: %v", name, status.State)
}
return nil
}

// SetSysEnv sets a system environment variable
func SetSysEnv(key, value string) error {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `System\CurrentControlSet\Control\Session Manager\Environment`, registry.SET_VALUE)
Expand Down

0 comments on commit 3b1370f

Please sign in to comment.