2024-11-09 13:16:11 +00:00
|
|
|
package adapter
|
|
|
|
|
2024-11-11 08:04:27 +00:00
|
|
|
import E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
|
2024-11-09 13:16:11 +00:00
|
|
|
type StartStage uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
StartStateInitialize StartStage = iota
|
|
|
|
StartStateStart
|
|
|
|
StartStatePostStart
|
|
|
|
StartStateStarted
|
|
|
|
)
|
|
|
|
|
|
|
|
var ListStartStages = []StartStage{
|
|
|
|
StartStateInitialize,
|
|
|
|
StartStateStart,
|
|
|
|
StartStatePostStart,
|
|
|
|
StartStateStarted,
|
|
|
|
}
|
|
|
|
|
2024-11-11 08:04:27 +00:00
|
|
|
func (s StartStage) String() string {
|
2024-11-09 13:16:11 +00:00
|
|
|
switch s {
|
|
|
|
case StartStateInitialize:
|
|
|
|
return "initialize"
|
|
|
|
case StartStateStart:
|
|
|
|
return "start"
|
|
|
|
case StartStatePostStart:
|
|
|
|
return "post-start"
|
|
|
|
case StartStateStarted:
|
2024-11-11 08:04:27 +00:00
|
|
|
return "finish-start"
|
2024-11-09 13:16:11 +00:00
|
|
|
default:
|
|
|
|
panic("unknown stage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-10 08:46:59 +00:00
|
|
|
type Lifecycle interface {
|
|
|
|
Start(stage StartStage) error
|
2024-11-09 13:16:11 +00:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2024-11-10 08:46:59 +00:00
|
|
|
type LifecycleService interface {
|
|
|
|
Name() string
|
|
|
|
Lifecycle
|
2024-11-09 13:16:11 +00:00
|
|
|
}
|
2024-11-11 08:04:27 +00:00
|
|
|
|
|
|
|
func Start(stage StartStage, services ...Lifecycle) error {
|
|
|
|
for _, service := range services {
|
|
|
|
err := service.Start(stage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func StartNamed(stage StartStage, services []LifecycleService) error {
|
|
|
|
for _, service := range services {
|
|
|
|
err := service.Start(stage)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, stage.String(), " ", service.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|