feat: AfterStopSignalWaitSecond as an Attribute add to heaven

This commit is contained in:
dapeng 2024-05-30 21:01:45 +08:00
parent 7046d2f56e
commit 564fe8d6a0
4 changed files with 36 additions and 47 deletions

View File

@ -76,7 +76,7 @@ func TestNew(t *testing.T) {
}
func TestServe(t *testing.T) {
gone.AfterStopSignalWaitSecond = 0
gone.AfterStopSignalWaitSecond = 1
gone.Serve(func(cemetery gone.Cemetery) error {
go func() {
tom := cemetery.GetTomById(gone.IdGoneHeaven)

View File

@ -9,51 +9,16 @@ import (
"time"
)
// Run 开始运行一个Gone程序`gone.Run` 和 `gone.Serve` 的区别是:
// 1. gone.Serve启动的程序主协程会调用 Heaven.WaitEnd 挂起等待停机信号,可以用于服务程序的开发
// 2. gone.Run启动的程序主协程则不会挂起运行完就结束适合开发一致性运行的代码
//
// // 定义加载服务的Priest函数
// func LoadServer(c Cemetery) error {
// c.Bury(goneXorm.New())
// c.Bury(goneGin.New())
// return nil
// }
//
// // 加载组件的Priest函数
// func LoadComponent(c Cemetery) error {
// c.Bury(componentA.New())
// c.Bury(componentB.New())
// }
//
//
// gone.Run(LoadServer, LoadComponent)//开始运行
func Run(priests ...Priest) {
AfterStopSignalWaitSecond = 0
New(priests...).
Install().
Start().
Stop()
}
// Serve 开始服务,参考[Run](#Run)
func Serve(priests ...Priest) {
New(priests...).
Install().
Start().
WaitEnd().
Stop()
}
// New 新建Heaven; Heaven 代表了一个应用程序;
func New(priests ...Priest) Heaven {
cemetery := newCemetery()
h := heaven{
SimpleLogger: &defaultLogger{},
cemetery: cemetery,
priests: priests,
signal: make(chan os.Signal),
stopSignal: make(chan struct{}),
SimpleLogger: &defaultLogger{},
cemetery: cemetery,
priests: priests,
signal: make(chan os.Signal),
stopSignal: make(chan struct{}),
afterStopSignalWaitSecond: AfterStopSignalWaitSecond,
}
h.
@ -78,6 +43,12 @@ type heaven struct {
signal chan os.Signal
stopSignal chan struct{}
afterStopSignalWaitSecond int
}
func (h *heaven) SetAfterStopSignalWaitSecond(sec int) {
h.afterStopSignalWaitSecond = sec
}
func getAngelType() reflect.Type {
@ -186,11 +157,11 @@ func (h *heaven) Stop() Heaven {
h.stopFlow()
close(h.stopSignal)
if AfterStopSignalWaitSecond > 0 {
h.Infof("WAIT %d SECOND TO STOP!!", AfterStopSignalWaitSecond)
if h.afterStopSignalWaitSecond > 0 {
h.Infof("WAIT %d SECOND TO STOP!!", h.afterStopSignalWaitSecond)
}
for i := 0; i < AfterStopSignalWaitSecond; i++ {
h.Infof("Stop in %d seconds.", AfterStopSignalWaitSecond-i)
for i := 0; i < h.afterStopSignalWaitSecond; i++ {
h.Infof("Stop in %d seconds.", h.afterStopSignalWaitSecond-i)
<-time.After(time.Second)
}
return h

View File

@ -112,6 +112,8 @@ type Heaven interface {
//AfterStop add a hook function which will execute after stop
AfterStop(Process) Heaven
SetAfterStopSignalWaitSecond(sec int)
}
type AfterReviveError error

View File

@ -24,8 +24,12 @@ func (p *Preparer) AfterStop(fn any) *Preparer {
return p
}
func (p *Preparer) SetAfterStopSignalWaitSecond(sec int) {
p.heaven.SetAfterStopSignalWaitSecond(sec)
}
func (p *Preparer) Run(fns ...any) {
AfterStopSignalWaitSecond = 0
p.SetAfterStopSignalWaitSecond(0)
for _, fn := range fns {
p.AfterStart(fn)
}
@ -53,3 +57,15 @@ func Prepare(priests ...Priest) *Preparer {
heaven: h,
}
}
// Run 开始运行一个Gone程序`gone.Run` 和 `gone.Serve` 的区别是:
// 1. gone.Serve启动的程序主协程会调用 Heaven.WaitEnd 挂起等待停机信号,可以用于服务程序的开发
// 2. gone.Run启动的程序主协程则不会挂起运行完就结束适合开发一致性运行的代码
func Run(priests ...Priest) {
Prepare(priests...).Run()
}
// Serve 开始服务,参考[Run](#Run)
func Serve(priests ...Priest) {
Prepare(priests...).Serve()
}