Skip to content

Commit

Permalink
Merging in #16, refactoring to build on all supported platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Audrius Karabanovas committed Aug 9, 2019
1 parent 2cce281 commit a9deef6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 54 deletions.
2 changes: 2 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
name = "github.com/sirupsen/logrus"
version = "1.4.2"

[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.1"

[[constraint]]
branch = "master"
name = "golang.org/x/sys"
24 changes: 24 additions & 0 deletions internal/service/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build linux darwin

package service

import (
"os"
"os/signal"
"syscall"

log "github.com/sirupsen/logrus"
)

// SetupServiceListener setups singal handler
func SetupServiceListener(stopCh chan<- bool, serviceName string, logger log.StdLogger) error {
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGHUP)
logger.Printf("Signal received: %v", <-sigs)
stopCh <- true
close(stopCh)
}()

return nil
}
55 changes: 55 additions & 0 deletions internal/service/handler_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// +build windows

package service

import (
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/windows/svc"
)

type beatExporterService struct {
stopCh chan<- bool
}

func (s *beatExporterService) 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:
s.stopCh <- true
break loop
default:
log.Error(fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}

// SetupServiceListener setups service handler for windows
func SetupServiceListener(stopCh chan<- bool, serviceName string, logger log.StdLogger) error {
isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
return err
}

if !isInteractive {
go func() {
err = svc.Run(serviceName, &beatExporterService{stopCh: stopCh})
if err != nil {
logger.Printf("Failed to start service: %v", err)
}
}()
}

return nil
}
82 changes: 32 additions & 50 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/trustpilot/beat-exporter/collector"

"golang.org/x/sys/windows/svc"
"github.com/trustpilot/beat-exporter/internal/service"
)

const (
Expand Down Expand Up @@ -63,16 +62,36 @@ func main() {

var beatInfo *collector.BeatInfo

stopCh := make(chan bool)

err = service.SetupServiceListener(stopCh, serviceName, log.StandardLogger())
if err != nil {
log.WithFields(log.Fields{
"err": err,
}).Errorf("could not setup service listener: %v", err)
}

t := time.NewTicker(1 * time.Second)

beatdiscovery:
for {
beatInfo, err = loadBeatType(httpClient, *beatURL)
if err != nil {
log.Errorf("Could not load beat type, with error: %v, retrying in 5s", err)
time.Sleep(5 * time.Second)
} else {
break
select {
case <-t.C:
beatInfo, err = loadBeatType(httpClient, *beatURL)
if err != nil {
log.Errorf("Could not load beat type, with error: %v, retrying in 1s", err)
continue
}

break beatdiscovery

case <-stopCh:
os.Exit(0) // signal received, stop gracefully
}
}

t.Stop()

// version metric
registry := prometheus.NewRegistry()
versionMetric := version.NewCollector(Name)
Expand All @@ -94,30 +113,20 @@ func main() {
"addr": *listenAddress,
}).Infof("Starting exporter with configured type: %s", beatInfo.Beat)

isInteractive, err := svc.IsAnInteractiveSession()
if err != nil {
log.Fatal(err)
}

stopCh := make(chan bool)
if !isInteractive {
go func() {
err = svc.Run(serviceName, &beatExporterService{stopCh: stopCh})
if err != nil {
log.Errorf("Failed to start service: %v", err)
}
go func() {
defer func() {
stopCh <- true
}()
}

go func() {
log.Info("Starting listener")
if err := http.ListenAndServe(*listenAddress, nil); err != nil {

log.WithFields(log.Fields{
"err": err,
}).Errorf("http server quit with error: %v", err)

}
fmt.Println("http server listening")
log.Info("Listener exited")
}()

for {
Expand Down Expand Up @@ -188,30 +197,3 @@ func loadBeatType(client *http.Client, url url.URL) (*collector.BeatInfo, error)

return beatInfo, nil
}

type beatExporterService struct {
stopCh chan<- bool
}

func (s *beatExporterService) 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:
s.stopCh <- true
break loop
default:
log.Error(fmt.Sprintf("unexpected control request #%d", c))
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}

0 comments on commit a9deef6

Please sign in to comment.