Fix start stage

This commit is contained in:
世界
2024-11-11 16:04:27 +08:00
parent e3ffffc645
commit 1edb80adcc
7 changed files with 67 additions and 54 deletions

View File

@@ -44,7 +44,7 @@ func (m *Manager) Start(stage adapter.StartStage) error {
for _, inbound := range m.inbounds {
err := adapter.LegacyStart(inbound, stage)
if err != nil {
return E.Cause(err, stage.Action(), " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
}
}
return nil
@@ -118,7 +118,7 @@ func (m *Manager) Create(ctx context.Context, router adapter.Router, logger log.
for _, stage := range adapter.ListStartStages {
err = adapter.LegacyStart(inbound, stage)
if err != nil {
return E.Cause(err, stage.Action(), " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
return E.Cause(err, stage, " inbound/", inbound.Type(), "[", inbound.Tag(), "]")
}
}
}

View File

@@ -1,5 +1,7 @@
package adapter
import E "github.com/sagernet/sing/common/exceptions"
type StartStage uint8
const (
@@ -16,7 +18,7 @@ var ListStartStages = []StartStage{
StartStateStarted,
}
func (s StartStage) Action() string {
func (s StartStage) String() string {
switch s {
case StartStateInitialize:
return "initialize"
@@ -25,7 +27,7 @@ func (s StartStage) Action() string {
case StartStatePostStart:
return "post-start"
case StartStateStarted:
return "start-after-started"
return "finish-start"
default:
panic("unknown stage")
}
@@ -40,3 +42,23 @@ type LifecycleService interface {
Name() string
Lifecycle
}
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
}

View File

@@ -14,7 +14,7 @@ func LegacyStart(starter any, stage StartStage) error {
}); isStarter {
return starter.Start()
}
case StartStatePostStart:
case StartStateStarted:
if postStarter, isPostStarter := starter.(interface {
PostStart() error
}); isPostStarter {

View File

@@ -61,7 +61,7 @@ func (m *Manager) Start(stage adapter.StartStage) error {
for _, outbound := range outbounds {
err := adapter.LegacyStart(outbound, stage)
if err != nil {
return E.Cause(err, stage.Action(), " outbound/", outbound.Type(), "[", outbound.Tag(), "]")
return E.Cause(err, stage, " outbound/", outbound.Type(), "[", outbound.Tag(), "]")
}
}
}
@@ -234,7 +234,7 @@ func (m *Manager) Create(ctx context.Context, router adapter.Router, logger log.
for _, stage := range adapter.ListStartStages {
err = adapter.LegacyStart(outbound, stage)
if err != nil {
return E.Cause(err, stage.Action(), " outbound/", outbound.Type(), "[", outbound.Tag(), "]")
return E.Cause(err, stage, " outbound/", outbound.Type(), "[", outbound.Tag(), "]")
}
}
}