-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprepare.go
105 lines (86 loc) · 2.02 KB
/
prepare.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
package gone
type Preparer struct {
heaven Heaven
}
func (p *Preparer) BeforeStart(fn any) *Preparer {
p.heaven.BeforeStart(WrapNormalFnToProcess(fn))
return p
}
func (p *Preparer) AfterStart(fn any) *Preparer {
p.heaven.AfterStart(WrapNormalFnToProcess(fn))
return p
}
func (p *Preparer) BeforeStop(fn any) *Preparer {
p.heaven.BeforeStop(WrapNormalFnToProcess(fn))
return p
}
func (p *Preparer) AfterStop(fn any) *Preparer {
p.heaven.AfterStop(WrapNormalFnToProcess(fn))
return p
}
func (p *Preparer) SetAfterStopSignalWaitSecond(sec int) {
p.heaven.SetAfterStopSignalWaitSecond(sec)
}
func (p *Preparer) Run(fns ...any) {
p.SetAfterStopSignalWaitSecond(0)
for _, fn := range fns {
p.AfterStart(fn)
}
p.heaven.
Install().
Start().
Stop()
}
func (p *Preparer) Serve(fns ...any) {
for _, fn := range fns {
p.AfterStart(fn)
}
p.heaven.
Install().
Start().
WaitEnd().
Stop()
}
func (p *Preparer) Load(goner Goner) *Preparer {
if gonerWithId, ok := goner.(NamedGoner); ok {
p.heaven.GetCemetery().Bury(goner, gonerWithId.GetGonerId())
} else {
p.heaven.GetCemetery().Bury(goner)
}
return p
}
func (p *Preparer) Bury(goner Goner) *Preparer {
return p.Load(goner)
}
func (p *Preparer) LoadPriest(priests ...Priest) *Preparer {
for _, priest := range priests {
err := priest(p.heaven.GetCemetery())
if err != nil {
panic(err)
}
}
return p
}
func (p *Preparer) Loads(priests ...Priest) *Preparer {
return p.LoadPriest(priests...)
}
func Prepare(priests ...Priest) *Preparer {
h := New(priests...)
return &Preparer{
heaven: h,
}
}
var Default = Prepare()
/*
Run A Gone Program;
gone.Run vs gone.Serve:
- gone.Run, The main goroutine never hangs, and the program is terminated when the main goroutine exits.
- gone.Serve, The main goroutine calls Heaven.WaitEnd and hangs, and the program waiting for the stop signal for exiting.
*/
func Run(priests ...Priest) {
Prepare(priests...).Run()
}
// Serve Start for A Gone Server Program.
func Serve(priests ...Priest) {
Prepare(priests...).Serve()
}