This repository was archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.go
More file actions
64 lines (55 loc) · 1.39 KB
/
container.go
File metadata and controls
64 lines (55 loc) · 1.39 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
package loric
import (
"fmt"
"os"
"os/signal"
"runtime"
"strconv"
"syscall"
"github.com/cr-mao/loric/component"
"github.com/cr-mao/loric/conf"
"github.com/cr-mao/loric/log"
"github.com/cr-mao/loric/sugar"
)
type Container struct {
sig chan os.Signal
components []component.Component
}
// NewContainer 创建一个容器
func NewContainer() *Container {
return &Container{sig: make(chan os.Signal)}
}
// Add 添加组件
func (c *Container) Add(components ...component.Component) {
c.components = append(c.components, components...)
}
// Serve 启动容器
func (c *Container) Serve() {
log.Debug(fmt.Sprintf("Welcome to the lorig framework %s, Learn more at %s", Version, Website))
for _, comp := range c.components {
comp.Init()
}
for _, comp := range c.components {
comp.Start()
}
c.doSavePID()
switch runtime.GOOS {
case `windows`:
signal.Notify(c.sig, syscall.SIGINT, syscall.SIGTERM)
default:
signal.Notify(c.sig, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGABRT, syscall.SIGTERM)
}
sig := <-c.sig
log.Warnf("process got signal %v, container will close", sig)
signal.Stop(c.sig)
for _, comp := range c.components {
comp.Destroy()
}
}
func (c *Container) doSavePID() {
filename := conf.GetString("app.pidPath", "server.pid")
err := sugar.WriteFile(filename, []byte(strconv.Itoa(syscall.Getpid())))
if err != nil {
log.Fatalf("pid save failed: %v", err)
}
}