Files
akvorado/cmd/components.go
Vincent Bernat 78fb01c223 chore: fix some small issues detected by golangci-lint
But not using it as some linters are either plain incorrect (the one
suggesting to not use nil for `c.t.Context()`) or just
debatable (checking for err value is a good practice, but there are
good reasons to opt out in some cases).
2022-08-10 17:44:32 +02:00

50 lines
1.2 KiB
Go

// SPDX-FileCopyrightText: 2022 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package cmd
import (
"fmt"
"akvorado/common/daemon"
"akvorado/common/reporter"
)
// StartStopComponents activate/deactivate components in order.
func StartStopComponents(r *reporter.Reporter, daemonComponent daemon.Component, otherComponents []interface{}) error {
components := append([]interface{}{r, daemonComponent}, otherComponents...)
startedComponents := []interface{}{}
defer func() {
for _, cmp := range startedComponents {
if stopperC, ok := cmp.(stopper); ok {
if err := stopperC.Stop(); err != nil {
r.Err(err).Msg("unable to stop component, ignoring")
}
}
}
}()
for _, cmp := range components {
if starterC, ok := cmp.(starter); ok {
if err := starterC.Start(); err != nil {
return fmt.Errorf("unable to start component: %w", err)
}
}
startedComponents = append([]interface{}{cmp}, startedComponents...)
}
r.Info().
Str("version", Version).Str("build-date", BuildDate).
Msg("akvorado has started")
<-daemonComponent.Terminated()
r.Info().Msg("stopping all components")
return nil
}
type starter interface {
Start() error
}
type stopper interface {
Stop() error
}