forked from prometheus-community/windows_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request prometheus-community#1047 from jammiemil/master
fix Windows Service timeout during high CPU (eg. post Windows Update)
- Loading branch information
Showing
2 changed files
with
67 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//This package allows us to initiate Time Sensitive components (Like registering the windows service) as early as possible in the startup process | ||
package initiate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/prometheus-community/windows_exporter/log" | ||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
const ( | ||
serviceName = "windows_exporter" | ||
) | ||
|
||
type windowsExporterService struct { | ||
stopCh chan<- bool | ||
} | ||
|
||
func (s *windowsExporterService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) { | ||
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | ||
changes <- svc.Status{State: svc.StartPending} | ||
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} | ||
loop: | ||
for { | ||
select { | ||
case c := <-r: | ||
switch c.Cmd { | ||
case svc.Interrogate: | ||
changes <- c.CurrentStatus | ||
case svc.Stop, svc.Shutdown: | ||
log.Debug("Service Stop Received") | ||
s.stopCh <- true | ||
break loop | ||
default: | ||
log.Error(fmt.Sprintf("unexpected control request #%d", c)) | ||
} | ||
} | ||
} | ||
changes <- svc.Status{State: svc.StopPending} | ||
return | ||
} | ||
|
||
var StopCh = make(chan bool) | ||
|
||
func init() { | ||
log.Debug("Checking if We are a service") | ||
isService, err := svc.IsWindowsService() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Debug("Attempting to start exporter service") | ||
if isService { | ||
go func() { | ||
err = svc.Run(serviceName, &windowsExporterService{stopCh: StopCh}) | ||
if err != nil { | ||
log.Errorf("Failed to start service: %v", err) | ||
} | ||
}() | ||
} | ||
} |