Skip to content

Commit

Permalink
Adopt svc_runner to pr-244 (#248)
Browse files Browse the repository at this point in the history
(cherry picked from commit b123bcd)
  • Loading branch information
Marius Sturm authored and bernd committed Jul 5, 2018
1 parent fdd6f0f commit 4800d38
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions daemon/svc_runner_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,22 @@ func (r *SvcRunner) Name() string {
func (r *SvcRunner) Running() bool {
m, err := mgr.Connect()
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
return false
}
defer m.Disconnect()

s, err := m.OpenService(r.serviceName)
// service exist so we only update the properties
if err != nil {
backends.SetStatusLogErrorf(r.name, "Can't get status of service %s cause it doesn't exist: %v", r.serviceName, err)
r.backend.SetStatusLogErrorf("Can't get status of service %s cause it doesn't exist: %v", r.serviceName, err)
return false
}
defer s.Close()

status, err := s.Query()
if err != nil {
backends.SetStatusLogErrorf(r.name, "Can't query status of service %s: %v", r.serviceName, err)
r.backend.SetStatusLogErrorf("Can't query status of service %s: %v", r.serviceName, err)
}

return status.State == svc.Running
Expand All @@ -107,8 +107,8 @@ func (r *SvcRunner) SetDaemon(d *DaemonConfig) {
r.daemon = d
}

func (r *SvcRunner) GetBackend() backends.Backend {
return r.backend
func (r *SvcRunner) GetBackend() *backends.Backend {
return &r.backend
}

func (r *SvcRunner) SetBackend(b backends.Backend) {
Expand All @@ -120,12 +120,12 @@ func (r *SvcRunner) SetBackend(b backends.Backend) {
func (r *SvcRunner) ValidateBeforeStart() error {
execPath, err := exec.LookPath(r.exec)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to find collector executable %s", r.exec)
return r.backend.SetStatusLogErrorf("Failed to find collector executable %s", r.exec)
}

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

Expand All @@ -147,21 +147,21 @@ func (r *SvcRunner) ValidateBeforeStart() error {
}
err = s.UpdateConfig(currentConfig)
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to update service: %v", err)
r.backend.SetStatusLogErrorf("Failed to update service: %v", err)
}
// service needs to be created
} else {
s, err = m.CreateService(r.serviceName,
execPath,
serviceConfig)
if err != nil {
backends.SetStatusLogErrorf(r.name, "Failed to install service: %v", err)
r.backend.SetStatusLogErrorf("Failed to install service: %v", err)
}
defer s.Close()
err = eventlog.InstallAsEventCreate(r.serviceName, eventlog.Error|eventlog.Warning|eventlog.Info)
if err != nil {
s.Delete()
backends.SetStatusLogErrorf(r.name, "SetupEventLogSource() failed: %v", err)
r.backend.SetStatusLogErrorf("SetupEventLogSource() failed: %v", err)
}
}

Expand All @@ -184,7 +184,7 @@ func (r *SvcRunner) startSupervisor() {
continue
}

backends.SetStatusLogErrorf(r.name, "Backend finished unexpectedly, sending restart signal")
r.backend.SetStatusLogErrorf("Backend finished unexpectedly, sending restart signal")
r.Restart()
}
}()
Expand All @@ -201,19 +201,19 @@ func (r *SvcRunner) start() error {

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

ws, err := m.OpenService(r.serviceName)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not access service: %v", err)
return r.backend.SetStatusLogErrorf("Could not access service: %v", err)
}
defer ws.Close()

err = ws.Start("is", "manual-started")
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not start service: %v", err)
return r.backend.SetStatusLogErrorf("Could not start service: %v", err)
}

r.setSupervised(true)
Expand All @@ -235,30 +235,30 @@ func (r *SvcRunner) stop() error {

m, err := mgr.Connect()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Failed to connect to service manager: %v", err)
return r.backend.SetStatusLogErrorf("Failed to connect to service manager: %v", err)
}
defer m.Disconnect()

ws, err := m.OpenService(r.serviceName)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not access service: %v", err)
return r.backend.SetStatusLogErrorf("Could not access service: %v", err)
}
defer ws.Close()

status, err := ws.Control(svc.Stop)
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not send stop control: %v", err)
return r.backend.SetStatusLogErrorf("Could not send stop control: %v", err)
}

timeout := time.Now().Add(10 * time.Second)
for status.State != svc.Stopped {
if timeout.Before(time.Now()) {
return backends.SetStatusLogErrorf(r.name, "Timeout waiting for service to go to stopped state: %v", err)
return r.backend.SetStatusLogErrorf("Timeout waiting for service to go to stopped state: %v", err)
}
time.Sleep(300 * time.Millisecond)
status, err = ws.Query()
if err != nil {
return backends.SetStatusLogErrorf(r.name, "Could not retrieve service status: %v", err)
return r.backend.SetStatusLogErrorf("Could not retrieve service status: %v", err)
}
}

Expand Down

0 comments on commit 4800d38

Please sign in to comment.