-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
executable file
·74 lines (69 loc) · 1.8 KB
/
init.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
package engine
import (
"context"
"fmt"
"github.com/coffeehc/base/log"
"github.com/coffeehc/boot/configuration"
"github.com/spf13/cobra"
"go.uber.org/zap"
"os"
"os/signal"
"syscall"
)
type ServiceStart func(ctx context.Context, cmd *cobra.Command, args []string) (ServiceCloseCallback, error)
type ServiceCloseCallback func()
func WaitServiceStop(ctx context.Context, closeCallback func()) {
ctx, cancelFunc := context.WithCancel(ctx)
var sigChan = make(chan os.Signal, 1)
go func() {
<-ctx.Done()
sigChan <- syscall.SIGINT
}()
//if runtime.GOOS != "darwin" && runtime.GOOS != "ios" {
signal.Notify(sigChan,
//syscall.SIGHUP,
//syscall.SIGINT,
syscall.SIGKILL,
syscall.SIGTERM,
//syscall.SIGQUIT
)
//}
sig := <-sigChan
log.Debug("收到指令", zap.Any("signal", sig))
if ctx.Err() == nil && cancelFunc != nil {
cancelFunc()
}
if closeCallback != nil {
closeCallback()
}
log.Info("关闭程序", zap.Any("signal", sig))
}
func StartEngine(ctx context.Context, serviceInfo configuration.ServiceInfo, start ServiceStart) {
serviceInfo.Version = configuration.Version
if serviceInfo.Version == "" {
fmt.Printf("没有指定版本号")
os.Exit(-1)
}
var rootCmd = &cobra.Command{
Use: configuration.GetServiceName(),
Short: fmt.Sprintf("%s 服务", configuration.GetServiceName()),
Long: serviceInfo.Descriptor,
Run: func(cmd *cobra.Command, args []string) {
configuration.PrintVersionInfo()
cmd.Help()
},
}
rootCmd.AddCommand(
buildVersionCmd(),
buildReStartCmd(ctx, serviceInfo, start),
buildStartCmd(ctx, serviceInfo, start),
buildDaemonStartCmd(ctx, serviceInfo, start),
buildStopCmd(ctx, serviceInfo),
buildSetupCmd(serviceInfo),
)
_err := rootCmd.ExecuteContext(ctx)
if _err != nil {
log.Error("启动错误", zap.Error(_err))
os.Exit(-1)
}
}