go-zero/core/service/servicegroup.go

119 lines
2.2 KiB
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package service
import (
"log"
"github.com/zeromicro/go-zero/core/proc"
"github.com/zeromicro/go-zero/core/syncx"
"github.com/zeromicro/go-zero/core/threading"
2020-07-26 17:09:05 +08:00
)
type (
// Starter is the interface wraps the Start method.
2020-07-26 17:09:05 +08:00
Starter interface {
Start()
}
// Stopper is the interface wraps the Stop method.
2020-07-26 17:09:05 +08:00
Stopper interface {
Stop()
}
// Service is the interface that groups Start and Stop methods.
2020-07-26 17:09:05 +08:00
Service interface {
Starter
Stopper
}
// A ServiceGroup is a group of services.
// Attention: the starting order of the added services is not guaranteed.
ServiceGroup struct {
2020-07-26 17:09:05 +08:00
services []Service
stopOnce func()
}
)
// NewServiceGroup returns a ServiceGroup.
func NewServiceGroup() *ServiceGroup {
sg := new(ServiceGroup)
2020-07-26 17:09:05 +08:00
sg.stopOnce = syncx.Once(sg.doStop)
return sg
}
// Add adds service into sg.
func (sg *ServiceGroup) Add(service Service) {
// push front, stop with reverse order.
sg.services = append([]Service{service}, sg.services...)
2020-07-26 17:09:05 +08:00
}
// Start starts the ServiceGroup.
2020-07-26 17:09:05 +08:00
// There should not be any logic code after calling this method, because this method is a blocking one.
// Also, quitting this method will close the logx output.
func (sg *ServiceGroup) Start() {
2020-07-26 17:09:05 +08:00
proc.AddShutdownListener(func() {
log.Println("Shutting down...")
sg.stopOnce()
})
sg.doStart()
}
// Stop stops the ServiceGroup.
func (sg *ServiceGroup) Stop() {
2020-07-26 17:09:05 +08:00
sg.stopOnce()
}
func (sg *ServiceGroup) doStart() {
2020-07-26 17:09:05 +08:00
routineGroup := threading.NewRoutineGroup()
for i := range sg.services {
service := sg.services[i]
routineGroup.RunSafe(func() {
service.Start()
})
}
routineGroup.Wait()
}
func (sg *ServiceGroup) doStop() {
2020-07-26 17:09:05 +08:00
for _, service := range sg.services {
service.Stop()
}
}
// WithStart wraps a start func as a Service.
2020-07-26 17:09:05 +08:00
func WithStart(start func()) Service {
return startOnlyService{
start: start,
}
}
// WithStarter wraps a Starter as a Service.
2020-07-26 17:09:05 +08:00
func WithStarter(start Starter) Service {
return starterOnlyService{
Starter: start,
}
}
type (
2021-04-15 19:49:17 +08:00
stopper struct{}
2020-07-26 17:09:05 +08:00
startOnlyService struct {
start func()
stopper
}
starterOnlyService struct {
Starter
stopper
}
)
func (s stopper) Stop() {
}
func (s startOnlyService) Start() {
s.start()
}