-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
167 lines (141 loc) · 3.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package zcli
import (
"context"
"log"
"strings"
"sync"
"time"
"github.com/lei006/go-daemon/daemontool/daemon"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zshell"
)
type (
app struct {
run func()
status bool
}
serviceStop struct {
}
serviceStart struct {
}
serviceRestart struct {
}
serviceInstall struct {
}
serviceUnInstall struct {
}
serviceStatus struct {
}
)
var (
service daemon.ServiceIfe
serviceErr error
once sync.Once
)
var s = make(chan struct{})
func (a *app) Start(daemon.ServiceIfe) error {
a.status = true
go func() {
a.run()
s <- struct{}{}
}()
return nil
}
func (a *app) Stop(daemon.ServiceIfe) error {
if !a.status {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
select {
case <-s:
case <-ctx.Done():
// return errors.New("forced timeout")
}
return nil
}
func (*serviceStatus) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceStatus) Run(_ []string) {
log.Printf("%s: %s\n", service.String(), service.Status())
}
func (*serviceInstall) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceInstall) Run(_ []string) {
CheckErr(service.Install(), true)
CheckErr(service.Start(), true)
}
func (*serviceUnInstall) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceUnInstall) Run(_ []string) {
CheckErr(service.Uninstall(), true)
}
func (*serviceStart) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceStart) Run(_ []string) {
CheckErr(service.Start(), true)
}
func (*serviceStop) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceStop) Run(_ []string) {
CheckErr(service.Stop(), true)
}
func (*serviceRestart) Flags(_ *Subcommand) {
CheckErr(serviceErr, true)
}
func (*serviceRestart) Run(_ []string) {
CheckErr(service.Restart(), true)
}
// LaunchServiceRun Launch Service and run
func LaunchServiceRun(name string, description string, fn func(), config ...*daemon.Config) error {
_, _ = LaunchService(name, description, fn, config...)
Parse()
if serviceErr != nil && (serviceErr != daemon.ErrNoServiceSystemDetected && !daemon.IsPermissionError(serviceErr)) {
return serviceErr
}
if service == nil {
fn()
return nil
}
return service.Run()
}
// LaunchService Launch Service
func LaunchService(name string, description string, fn func(), config ...*daemon.Config) (daemon.ServiceIfe, error) {
once.Do(func() {
var daemonConfig *daemon.Config
if len(config) > 0 {
daemonConfig = config[0]
daemonConfig.Name = name
daemonConfig.Description = description
} else {
daemonConfig = &daemon.Config{
Name: name,
Description: description,
Option: daemon.DefData{
// "UserService": true,
},
}
}
// The file path is redirected to the current execution file path
_, gogccflags, _, _ := zshell.Run("go env GOGCCFLAGS")
if !strings.Contains(
gogccflags, zfile.RealPath(zfile.ProgramPath()+"../../../..")) {
zfile.ProjectPath = zfile.ProgramPath()
}
service, serviceErr = daemon.New(&app{
run: fn,
}, daemonConfig)
Add("install", GetLangText("install"), &serviceInstall{})
Add("uninstall", GetLangText("uninstall"), &serviceUnInstall{})
Add("status", GetLangText("status"), &serviceStatus{})
Add("start", GetLangText("start"), &serviceStart{})
Add("stop", GetLangText("stop"), &serviceStop{})
Add("restart", GetLangText("restart"), &serviceRestart{})
})
return service, serviceErr
}