Skip to content

Commit

Permalink
fix: impl porter connection status & instance info
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Feb 15, 2024
1 parent 6613071 commit 833921e
Show file tree
Hide file tree
Showing 10 changed files with 244 additions and 89 deletions.
2 changes: 1 addition & 1 deletion app/sephirah/cmd/sephirah/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion app/sephirah/internal/service/librariansephirahservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/tuihub/librarian/app/sephirah/internal/biz/bizyesod"
"github.com/tuihub/librarian/app/sephirah/internal/model/converter"
"github.com/tuihub/librarian/app/sephirah/internal/supervisor"
"github.com/tuihub/librarian/internal/conf"
"github.com/tuihub/librarian/internal/lib/libapp"
"github.com/tuihub/librarian/internal/lib/libauth"
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"
Expand All @@ -33,6 +34,7 @@ type LibrarianSephirahServiceService struct {
app *libapp.Settings
auth *libauth.Auth
authFunc func(context.Context) (context.Context, error)
info *pb.ServerInstanceSummary
}

func NewLibrarianSephirahServiceService(
Expand All @@ -47,8 +49,15 @@ func NewLibrarianSephirahServiceService(
app *libapp.Settings,
auth *libauth.Auth,
authFunc func(context.Context) (context.Context, error),
config *conf.SephirahServer,
) pb.LibrarianSephirahServiceServer {
t.CreateConfiguredAdmin()
if config == nil {
config = new(conf.SephirahServer)
}
if config.GetInfo() == nil {
config.Info = new(conf.SephirahServer_Info)
}
return &LibrarianSephirahServiceService{
UnimplementedLibrarianSephirahServiceServer: pb.UnimplementedLibrarianSephirahServiceServer{},
t: t,
Expand All @@ -61,6 +70,13 @@ func NewLibrarianSephirahServiceService(
app: app,
auth: auth,
authFunc: authFunc,
info: &pb.ServerInstanceSummary{
Name: config.GetInfo().GetName(),
Description: config.GetInfo().GetDescription(),
WebsiteUrl: config.GetInfo().GetWebsiteUrl(),
LogoUrl: config.GetInfo().GetLogoUrl(),
BackgroundUrl: config.GetInfo().GetBackgroundUrl(),
},
}
}

Expand All @@ -77,6 +93,6 @@ func (s *LibrarianSephirahServiceService) GetServerInformation(_ context.Context
},
CurrentTime: timestamppb.New(time.Now()),
FeatureSummary: converter.ToPBServerFeatureSummary(s.s.GetFeatureSummary()),
ServerInstanceSummary: nil,
ServerInstanceSummary: s.info,
}, nil
}
3 changes: 3 additions & 0 deletions app/sephirah/internal/service/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ func (s *LibrarianSephirahServiceService) ListPorters(ctx context.Context, req *
if err != nil {
return nil, err
}
for i := range res {
res[i].ConnectionStatus = s.s.GetInstanceConnectionStatus(ctx, res[i].Address)
}
return &pb.ListPortersResponse{
Paging: &librarian.PagingResponse{TotalSize: total},
Porters: converter.ToPBPorterList(res),
Expand Down
15 changes: 14 additions & 1 deletion app/sephirah/internal/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,16 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit // TODO
continue
}
ins = s.knownInstances[address]
ins.ConnectionStatus = modeltiphereth.PorterConnectionStatusConnected
if ins.Status == modeltiphereth.PorterInstanceStatusActive {
// enable & check ownership
if err2 := s.enablePorterInstance(ctx, ins); err2 != nil {
logger.Errorf("%s", err2.Error())
ins.ConnectionStatus = modeltiphereth.PorterConnectionStatusActivationFailed
// bad instance, can't enable
continue
}
ins.ConnectionStatus = modeltiphereth.PorterConnectionStatusActive
}
if reflect.DeepEqual(ins.FeatureSummary, feature) {
// no change, but alive
Expand All @@ -134,7 +137,7 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit // TODO
Address: address,
FeatureSummary: feature,
Status: modeltiphereth.PorterInstanceStatusUnspecified,
ConnectionStatus: modeltiphereth.PorterConnectionStatusUnspecified,
ConnectionStatus: modeltiphereth.PorterConnectionStatusConnected,
}
}
// new instance or feature changed
Expand All @@ -148,6 +151,16 @@ func (s *Supervisor) RefreshAliveInstances( //nolint:gocognit // TODO
return newInstances, nil
}

func (s *Supervisor) GetInstanceConnectionStatus(
ctx context.Context,
address string,
) modeltiphereth.PorterConnectionStatus {
if s.aliveInstances[address] == nil {
return modeltiphereth.PorterConnectionStatusDisconnected
}
return s.aliveInstances[address].ConnectionStatus
}

// EnablePorterInstance enable porter instance, can be called multiple times.
func (s *Supervisor) enablePorterInstance(ctx context.Context, instance *modeltiphereth.PorterInstance) error {
if instance == nil {
Expand Down
1 change: 1 addition & 0 deletions app/sephirah/pkg/service/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

func NewSephirahService(
*conf.SephirahServer,
*conf.SephirahData,
*conf.Porter,
*conf.Consul,
Expand Down
4 changes: 2 additions & 2 deletions app/sephirah/pkg/service/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/librarian/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 833921e

Please sign in to comment.