Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Feb 9, 2024
1 parent 24d29c2 commit 25de97c
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions ocis/pkg/runtime/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ import (
var (
// runset keeps track of which services to start supervised.
runset map[string]struct{}

// wait funcs run after the service group has been started.
_waitFuncs = []func(*ociscfg.Config) error{pingNats, pingGateway, nil, wait(time.Second), nil}
)

type serviceFuncMap map[string]func(*ociscfg.Config) suture.Service
Expand Down Expand Up @@ -107,7 +110,7 @@ func NewService(options ...Option) (*Service, error) {
globalCtx, cancelGlobal := context.WithCancel(context.Background())

s := &Service{
Services: make([]serviceFuncMap, 5),
Services: make([]serviceFuncMap, len(_waitFuncs)),
Additional: make(serviceFuncMap),
Log: l,

Expand All @@ -123,20 +126,25 @@ func NewService(options ...Option) (*Service, error) {
s.Services[priority] = make(serviceFuncMap)
}
s.Services[priority][name] = NewSutureServiceBuilder(exec)
// s.ServicesRegistry[name] = NewSutureServiceBuilder(exec)
}

// start nats first - it is used as service registry
// nats is in priority group 0. It needs to start before all other services
reg(0, opts.Config.Nats.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Nats.Context = ctx
cfg.Nats.Commons = cfg.Commons
return nats.Execute(cfg.Nats)
})

// gateway is in priority group 1. It needs to start before the reva services
reg(1, opts.Config.Gateway.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Gateway.Context = ctx
cfg.Gateway.Commons = cfg.Commons
return gateway.Execute(cfg.Gateway)
})

// priority group 2 is empty for now

// most services are in priority group 3
reg(3, opts.Config.AppProvider.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.AppProvider.Context = ctx
cfg.AppProvider.Commons = cfg.Commons
Expand Down Expand Up @@ -277,38 +285,39 @@ func NewService(options ...Option) (*Service, error) {
cfg.Webfinger.Commons = cfg.Commons
return webfinger.Execute(cfg.Webfinger)
})

reg(4, opts.Config.Frontend.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Frontend.Context = ctx
cfg.Frontend.Commons = cfg.Commons
return frontend.Execute(cfg.Frontend)
})
reg(4, opts.Config.IDP.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
reg(3, opts.Config.IDP.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.IDP.Context = ctx
cfg.IDP.Commons = cfg.Commons
return idp.Execute(cfg.IDP)
})
reg(4, opts.Config.Proxy.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
reg(3, opts.Config.Proxy.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Proxy.Context = ctx
cfg.Proxy.Commons = cfg.Commons
return proxy.Execute(cfg.Proxy)
})
reg(4, opts.Config.Sharing.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
reg(3, opts.Config.Sharing.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Sharing.Context = ctx
cfg.Sharing.Commons = cfg.Commons
return sharing.Execute(cfg.Sharing)
})
reg(4, opts.Config.SSE.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
reg(3, opts.Config.SSE.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.SSE.Context = ctx
cfg.SSE.Commons = cfg.Commons
return sse.Execute(cfg.SSE)
})
reg(4, opts.Config.OCM.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
reg(3, opts.Config.OCM.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.OCM.Context = ctx
cfg.OCM.Commons = cfg.Commons
return ocm.Execute(cfg.OCM)
})

// out of some unknown reason ci gets angry when frontend service starts in priority group 3
// this is not reproducible locally, it can be start when nats and gateway are already running
reg(4, opts.Config.Frontend.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.Frontend.Context = ctx
cfg.Frontend.Commons = cfg.Commons
return frontend.Execute(cfg.Frontend)
})
// populate optional services
areg := func(name string, exec func(context.Context, *ociscfg.Config) error) {
s.Additional[name] = NewSutureServiceBuilder(exec)
Expand Down Expand Up @@ -409,11 +418,10 @@ func Start(o ...Option) error {
// trap will block on halt channel for interruptions.
go trap(s, halt)

waiters := []func(*ociscfg.Config) error{pingNats, pingGateway, nil, nil, nil}
for i, service := range s.Services {
scheduleServiceTokens(s, service)
if waiters[i] != nil {
if err := waiters[i](s.cfg); err != nil {
if _waitFuncs[i] != nil {
if err := _waitFuncs[i](s.cfg); err != nil {
s.Log.Fatal().Err(err).Msg("could not connect to service")
}
}
Expand Down Expand Up @@ -513,11 +521,14 @@ func pingNats(cfg *ociscfg.Config) error {
return err
}

func pingGateway(cfg *ociscfg.Config) error {
// we need to get a gateway address from somewhere. We can use any one.
// address := cfg.Graph.Reva.Address
b := backoff.NewExponentialBackOff()
func pingGateway(_ *ociscfg.Config) error {
// init grpc connection
_, err := ogrpc.NewClient()
if err != nil {
return err
}

b := backoff.NewExponentialBackOff()
o := func() error {
n := b.NextBackOff()
_, err := pool.GetGatewayServiceClient("com.owncloud.api.gateway")
Expand All @@ -530,3 +541,10 @@ func pingGateway(cfg *ociscfg.Config) error {
err = backoff.Retry(o, b)
return err
}

func wait(d time.Duration) func(cfg *ociscfg.Config) error {
return func(cfg *ociscfg.Config) error {
time.Sleep(d)
return nil
}
}

0 comments on commit 25de97c

Please sign in to comment.