/
app_instance.go
executable file
·73 lines (58 loc) · 1.3 KB
/
app_instance.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
/*
* Copyright (c) 2018, https://github.com/airwide-code/airwide.datacenter
* All rights reserved.
*
*/
package app
import (
"os/signal"
"syscall"
"github.com/golang/glog"
"os"
"flag"
)
var GAppInstance AppInstance
func init() {
flag.Parse()
flag.Set("alsologtostderr", "true")
flag.Set("log_dir", "false")
}
type AppInstance interface {
Initialize() error
RunLoop()
Destroy()
}
var ch = make(chan os.Signal, 1)
func DoMainAppInstance(instance AppInstance) {
if instance == nil {
// panic("instance is nil!!!!")
glog.Errorf("instance is nil, will exit.")
return
}
// global
GAppInstance = instance
glog.Info("instance initialize...")
err := instance.Initialize()
if err != nil {
glog.Infof("instance initialize error: {%v}", err)
return
}
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGHUP, syscall.SIGQUIT)
glog.Info("instance run_loop...")
go instance.RunLoop()
// fmt.Printf("%d", os.Getpid())
glog.Info("Wait quit...")
s2 := <-ch
if i, ok := s2.(syscall.Signal); ok {
glog.Infof("instance recv os.Exit(%d) signal...", i)
} else {
glog.Info("instance exit...", i)
}
instance.Destroy()
glog.Info("instance quited!")
}
func QuitAppInstance() {
/*notifier := make(chan os.Signal, 1)
signal.Stop(notifier)*/
ch <- syscall.SIGQUIT
}