-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathservice.go
More file actions
67 lines (55 loc) · 1.05 KB
/
service.go
File metadata and controls
67 lines (55 loc) · 1.05 KB
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
package gonet
import (
"fmt"
"os"
"os/signal"
"runtime"
"runtime/debug"
"syscall"
)
type IService interface {
Init() bool
Reload()
MainLoop()
Final() bool
}
type Service struct {
terminate bool
Derived IService
}
func (this *Service) Terminate() {
this.terminate = true
}
func (this *Service) isTerminate() bool {
return this.terminate
}
func (this *Service) Main() bool {
defer func() {
if err := recover(); err != nil {
fmt.Println("[异常] ", err, "\n", string(debug.Stack()))
}
}()
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGTERM, syscall.SIGPIPE, syscall.SIGHUP)
go func() {
for sig := range ch {
switch sig {
case syscall.SIGHUP:
this.Derived.Reload()
case syscall.SIGPIPE:
default:
this.Terminate()
}
fmt.Println("[服务] 收到信号 ", sig)
}
}()
runtime.GOMAXPROCS(runtime.NumCPU())
if !this.Derived.Init() {
return false
}
for !this.isTerminate() {
this.Derived.MainLoop()
}
this.Derived.Final()
return true
}