-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging in #16, refactoring to build on all supported platforms
- Loading branch information
Audrius Karabanovas
committed
Aug 9, 2019
1 parent
2cce281
commit a9deef6
Showing
5 changed files
with
113 additions
and
54 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 | ||
} |
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,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 | ||
} |
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