Skip to content

Commit

Permalink
refact / split APIServer.Run() method (#3215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Sep 12, 2024
1 parent 584a19f commit d5c587c
Showing 1 changed file with 67 additions and 57 deletions.
124 changes: 67 additions & 57 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,72 @@ func (s *APIServer) Router() (*gin.Engine, error) {
return s.router, nil
}

func (s *APIServer) apicPush() error {
if err := s.apic.Push(); err != nil {
log.Errorf("capi push: %s", err)
return err
}

return nil
}

func (s *APIServer) apicPull() error {
if err := s.apic.Pull(); err != nil {
log.Errorf("capi pull: %s", err)
return err
}

return nil
}

func (s *APIServer) papiPull() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}

return nil
}

func (s *APIServer) papiSync() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}

return nil
}

func (s *APIServer) initAPIC() {
s.apic.pushTomb.Go(s.apicPush)
s.apic.pullTomb.Go(s.apicPull)

// csConfig.API.Server.ConsoleConfig.ShareCustomScenarios
if s.apic.apiClient.IsEnrolled() {
if s.consoleConfig.IsPAPIEnabled() {
if s.papi.URL != "" {
log.Info("Starting PAPI decision receiver")
s.papi.pullTomb.Go(s.papiPull)
s.papi.syncTomb.Go(s.papiSync)
} else {
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
}
}

s.apic.metricsTomb.Go(func() error {
s.apic.SendMetrics(make(chan bool))
return nil
})

s.apic.metricsTomb.Go(func() error {
s.apic.SendUsageMetrics()
return nil
})
}

func (s *APIServer) Run(apiReady chan bool) error {
defer trace.CatchPanic("lapi/runServer")

Expand All @@ -316,63 +382,7 @@ func (s *APIServer) Run(apiReady chan bool) error {
}

if s.apic != nil {
s.apic.pushTomb.Go(func() error {
if err := s.apic.Push(); err != nil {
log.Errorf("capi push: %s", err)
return err
}

return nil
})

s.apic.pullTomb.Go(func() error {
if err := s.apic.Pull(); err != nil {
log.Errorf("capi pull: %s", err)
return err
}

return nil
})

// csConfig.API.Server.ConsoleConfig.ShareCustomScenarios
if s.apic.apiClient.IsEnrolled() {
if s.consoleConfig.IsPAPIEnabled() {
if s.papi.URL != "" {
log.Info("Starting PAPI decision receiver")
s.papi.pullTomb.Go(func() error {
if err := s.papi.Pull(); err != nil {
log.Errorf("papi pull: %s", err)
return err
}

return nil
})

s.papi.syncTomb.Go(func() error {
if err := s.papi.SyncDecisions(); err != nil {
log.Errorf("capi decisions sync: %s", err)
return err
}

return nil
})
} else {
log.Warnf("papi_url is not set in online_api_credentials.yaml, can't synchronize with the console. Run cscli console enable console_management to add it.")
}
} else {
log.Warningf("Machine is not allowed to synchronize decisions, you can enable it with `cscli console enable console_management`")
}
}

s.apic.metricsTomb.Go(func() error {
s.apic.SendMetrics(make(chan bool))
return nil
})

s.apic.metricsTomb.Go(func() error {
s.apic.SendUsageMetrics()
return nil
})
s.initAPIC()
}

s.httpServerTomb.Go(func() error {
Expand Down

0 comments on commit d5c587c

Please sign in to comment.