-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.go
161 lines (127 loc) · 2.88 KB
/
daemon.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
package kkdaemon
import (
"fmt"
"os"
"syscall"
"time"
concurrent "github.com/kklab-com/goth-concurrent"
kkpanic "github.com/kklab-com/goth-panic"
)
var DefaultService = NewDaemonService()
const StateWait = int32(0)
const StateStart = int32(1)
const StateRun = int32(2)
const StateStop = int32(3)
const shutdownGracefullySignal = syscall.Signal(0xff)
const unregisterSignal = syscall.Signal(0xfe)
var _MaxTime = time.Unix(1<<63-62135596801, 999999999)
type DaemonEntity struct {
Name string
Daemon Daemon
Order int
Next time.Time
}
type Daemon interface {
Registered() error
State() int32
Start()
Stop(sig os.Signal)
Name() string
_State() *int32
}
type daemonSetName interface {
setName(name string)
}
type DefaultDaemon struct {
name string
state int32
Params map[string]interface{}
}
func (d *DefaultDaemon) Registered() error {
return nil
}
func (d *DefaultDaemon) State() int32 {
return d.state
}
func (d *DefaultDaemon) Start() {
}
func (d *DefaultDaemon) Stop(sig os.Signal) {
}
func (d *DefaultDaemon) setName(name string) {
d.name = name
}
func (d *DefaultDaemon) Name() string {
return d.name
}
func (d *DefaultDaemon) _State() *int32 {
return &d.state
}
type _SimpleDaemon struct {
DefaultDaemon
StartFunc func()
StopFunc func(sig os.Signal)
}
func (s *_SimpleDaemon) Name() string {
return s.name
}
func (s *_SimpleDaemon) Start() {
if s.StartFunc != nil {
s.StartFunc()
}
}
func (s *_SimpleDaemon) Stop(sig os.Signal) {
if s.StopFunc != nil {
s.StopFunc(sig)
}
}
func RegisterDaemon(daemon Daemon) error {
return DefaultService.RegisterDaemon(daemon)
}
func RegisterSimpleDaemon(name string, startFunc func(), stopFunc func(sig os.Signal)) error {
return RegisterDaemon(&_SimpleDaemon{
DefaultDaemon: DefaultDaemon{
name: name,
},
StartFunc: startFunc,
StopFunc: stopFunc,
})
}
func StartDaemon(name string) error {
if entity, f := DefaultService.DaemonMap.Load(name); !f {
return fmt.Errorf(fmt.Sprintf("dameon %s not found", name))
} else {
return DefaultService.StartDaemon(entity.(*DaemonEntity))
}
}
func StopDaemon(name string, sig os.Signal) error {
if entity, f := DefaultService.DaemonMap.Load(name); !f {
return fmt.Errorf(fmt.Sprintf("dameon %s not found", name))
} else {
return DefaultService.StopDaemon(entity.(*DaemonEntity), sig)
}
}
func GetDaemon(name string) *DaemonEntity {
return DefaultService.GetDaemon(name)
}
func UnregisterDaemon(name string) error {
return DefaultService.UnregisterDaemon(name)
}
func Start() error {
return DefaultService.Start()
}
func Stop(sig os.Signal) error {
return DefaultService.Stop(sig)
}
func IsShutdown() bool {
return DefaultService.IsShutdown()
}
func ShutdownGracefully() {
DefaultService.ShutdownGracefully()
}
func ShutdownFuture() concurrent.Future {
return DefaultService.ShutdownFuture()
}
type PanicResult struct {
Daemon Daemon
Caught kkpanic.Caught
}