|
|
@ -9,39 +9,45 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
|
|
|
|
// Starter is the interface wraps the Start method.
|
|
|
|
Starter interface {
|
|
|
|
Starter interface {
|
|
|
|
Start()
|
|
|
|
Start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Stopper is the interface wraps the Stop method.
|
|
|
|
Stopper interface {
|
|
|
|
Stopper interface {
|
|
|
|
Stop()
|
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Service is the interface that groups Start and Stop methods.
|
|
|
|
Service interface {
|
|
|
|
Service interface {
|
|
|
|
Starter
|
|
|
|
Starter
|
|
|
|
Stopper
|
|
|
|
Stopper
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ServiceGroup struct {
|
|
|
|
// A Group is a group of services.
|
|
|
|
|
|
|
|
Group struct {
|
|
|
|
services []Service
|
|
|
|
services []Service
|
|
|
|
stopOnce func()
|
|
|
|
stopOnce func()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewServiceGroup() *ServiceGroup {
|
|
|
|
// NewGroup returns a Group.
|
|
|
|
sg := new(ServiceGroup)
|
|
|
|
func NewGroup() *Group {
|
|
|
|
|
|
|
|
sg := new(Group)
|
|
|
|
sg.stopOnce = syncx.Once(sg.doStop)
|
|
|
|
sg.stopOnce = syncx.Once(sg.doStop)
|
|
|
|
return sg
|
|
|
|
return sg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (sg *ServiceGroup) Add(service Service) {
|
|
|
|
// Add adds service into sg.
|
|
|
|
|
|
|
|
func (sg *Group) Add(service Service) {
|
|
|
|
sg.services = append(sg.services, service)
|
|
|
|
sg.services = append(sg.services, service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start starts the ServiceGroup.
|
|
|
|
// Start starts the Group.
|
|
|
|
// There should not be any logic code after calling this method, because this method is a blocking one.
|
|
|
|
// 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.
|
|
|
|
// Also, quitting this method will close the logx output.
|
|
|
|
func (sg *ServiceGroup) Start() {
|
|
|
|
func (sg *Group) Start() {
|
|
|
|
proc.AddShutdownListener(func() {
|
|
|
|
proc.AddShutdownListener(func() {
|
|
|
|
log.Println("Shutting down...")
|
|
|
|
log.Println("Shutting down...")
|
|
|
|
sg.stopOnce()
|
|
|
|
sg.stopOnce()
|
|
|
@ -50,11 +56,12 @@ func (sg *ServiceGroup) Start() {
|
|
|
|
sg.doStart()
|
|
|
|
sg.doStart()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (sg *ServiceGroup) Stop() {
|
|
|
|
// Stop stops the Group.
|
|
|
|
|
|
|
|
func (sg *Group) Stop() {
|
|
|
|
sg.stopOnce()
|
|
|
|
sg.stopOnce()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (sg *ServiceGroup) doStart() {
|
|
|
|
func (sg *Group) doStart() {
|
|
|
|
routineGroup := threading.NewRoutineGroup()
|
|
|
|
routineGroup := threading.NewRoutineGroup()
|
|
|
|
|
|
|
|
|
|
|
|
for i := range sg.services {
|
|
|
|
for i := range sg.services {
|
|
|
@ -67,18 +74,20 @@ func (sg *ServiceGroup) doStart() {
|
|
|
|
routineGroup.Wait()
|
|
|
|
routineGroup.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (sg *ServiceGroup) doStop() {
|
|
|
|
func (sg *Group) doStop() {
|
|
|
|
for _, service := range sg.services {
|
|
|
|
for _, service := range sg.services {
|
|
|
|
service.Stop()
|
|
|
|
service.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithStart wraps a start func as a Service.
|
|
|
|
func WithStart(start func()) Service {
|
|
|
|
func WithStart(start func()) Service {
|
|
|
|
return startOnlyService{
|
|
|
|
return startOnlyService{
|
|
|
|
start: start,
|
|
|
|
start: start,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithStarter wraps a Starter as a Service.
|
|
|
|
func WithStarter(start Starter) Service {
|
|
|
|
func WithStarter(start Starter) Service {
|
|
|
|
return starterOnlyService{
|
|
|
|
return starterOnlyService{
|
|
|
|
Starter: start,
|
|
|
|
Starter: start,
|
|
|
|