-
Notifications
You must be signed in to change notification settings - Fork 85
/
svc_common.go
51 lines (40 loc) · 916 Bytes
/
svc_common.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
// +build !windows
package svc
import (
"context"
"os"
"syscall"
)
// Run runs your Service.
//
// Run will block until one of the signals specified in sig is received or a provided context is done.
// If sig is empty syscall.SIGINT and syscall.SIGTERM are used by default.
func Run(service Service, sig ...os.Signal) error {
env := environment{}
if err := service.Init(env); err != nil {
return err
}
if err := service.Start(); err != nil {
return err
}
if len(sig) == 0 {
sig = []os.Signal{syscall.SIGINT, syscall.SIGTERM}
}
signalChan := make(chan os.Signal, 1)
signalNotify(signalChan, sig...)
var ctx context.Context
if s, ok := service.(Context); ok {
ctx = s.Context()
} else {
ctx = context.Background()
}
select {
case <-signalChan:
case <-ctx.Done():
}
return service.Stop()
}
type environment struct{}
func (environment) IsWindowsService() bool {
return false
}