forked from kubemq-io/kubemq-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
97 lines (88 loc) · 2.06 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//go:build !container
// +build !container
package main
import (
"os"
"github.com/kardianos/service"
"github.com/kubemq-io/kubemq-sources/pkg/logger"
)
type appService struct {
serviceExit chan bool
errCh chan error
svclogger service.Logger
}
func newAppService() *appService {
a := &appService{
serviceExit: make(chan bool, 1),
errCh: make(chan error, 5),
}
return a
}
func (a *appService) init(action, username, password string) error {
options := make(service.KeyValue)
options["Restart"] = "on-success"
options["SuccessExitStatus"] = "1 2 8 SIGKILL"
options["Password"] = password
svcConfig := &service.Config{
Name: "KubeMQ Sources",
DisplayName: "KubeMQ Sources",
Description: "KubeMQ Sources connects external systems and cloud services with KubeMQ message queue broker",
UserName: username,
Arguments: nil,
Executable: "",
Dependencies: []string{},
WorkingDirectory: "",
ChRoot: "",
Option: options,
}
srv, err := service.New(a, svcConfig)
if err != nil {
return err
}
a.svclogger, err = srv.Logger(a.errCh)
if err != nil {
log.Fatal(err)
}
go func() {
for {
err := <-a.errCh
if err != nil {
log.Error(err)
}
}
}()
if action != "" {
err := service.Control(srv, action)
if err != nil {
return err
}
log.Infof("service command %s completed", action)
return nil
}
err = srv.Run()
return err
}
func (a *appService) Start(s service.Service) error {
if service.Interactive() {
preRun()
log.Infof("starting kubemq sources connectors version: %s", version)
} else {
logger.ServiceLog.SetLogger(a.svclogger)
log.Infof("starting kubemq sources connectors as a service version: %s", version)
}
go func() {
err := runInteractive(a.serviceExit)
if err != nil {
log.Errorf("kubemq sources ended with error: %s", err)
os.Exit(1)
} else {
os.Exit(0)
}
}()
return nil
}
func (a *appService) Stop(s service.Service) error {
a.serviceExit <- true
log.Infof("kubemq sources connectors stopped")
return nil
}