feat(health): only run health server when running in Docker

This commit is contained in:
Quentin McGaw
2024-06-13 19:29:56 +00:00
parent c1bf7a49c1
commit 776206eec8
4 changed files with 51 additions and 5 deletions

View File

@@ -50,7 +50,8 @@ RUN git init && \
rm -rf .git/
FROM --platform=$BUILDPLATFORM base AS build
RUN mkdir -p /tmp/data
RUN mkdir -p /tmp/data && \
touch /tmp/isdocker
ARG VERSION=unknown
ARG CREATED="an unknown date"
ARG COMMIT=unknown
@@ -69,8 +70,10 @@ HEALTHCHECK --interval=60s --timeout=5s --start-period=10s --retries=2 CMD ["/up
ARG UID=1000
ARG GID=1000
USER ${UID}:${GID}
WORKDIR /updater
ENTRYPOINT ["/updater/ddns-updater"]
COPY --from=build --chown=${UID}:${GID} /tmp/data /updater/data
COPY --from=build --chown=${UID}:${GID} /tmp/isdocker /updater/isdocker
ENV \
# Core
CONFIG= \

View File

@@ -19,6 +19,7 @@ import (
"github.com/qdm12/ddns-updater/internal/health"
"github.com/qdm12/ddns-updater/internal/healthchecksio"
"github.com/qdm12/ddns-updater/internal/models"
"github.com/qdm12/ddns-updater/internal/noop"
jsonparams "github.com/qdm12/ddns-updater/internal/params"
persistence "github.com/qdm12/ddns-updater/internal/persistence/json"
"github.com/qdm12/ddns-updater/internal/provider"
@@ -205,10 +206,7 @@ func _main(ctx context.Context, reader *reader.Reader, args []string, logger log
updaterService := update.NewService(db, updater, ipGetter, config.Update.Period,
config.Update.Cooldown, logger, resolver, timeNow, hioClient)
isHealthy := health.MakeIsHealthy(db, resolver)
healthLogger := logger.New(log.SetComponent("healthcheck server"))
healthServer, err := health.NewServer(*config.Health.ServerAddress,
healthLogger, isHealthy)
healthServer, err := createHealthServer(db, resolver, logger, *config.Health.ServerAddress)
if err != nil {
return fmt.Errorf("creating health server: %w", err)
}
@@ -348,3 +346,15 @@ func exitHealthchecksio(hioClient *healthchecksio.Client,
logger.Error(err.Error())
}
}
//nolint:ireturn
func createHealthServer(db health.AllSelecter, resolver health.LookupIPer,
logger log.LoggerInterface, serverAddress string) (
healthServer goservices.Service, err error) {
if !health.IsDocker() {
return noop.New("healthcheck server"), nil
}
isHealthy := health.MakeIsHealthy(db, resolver)
healthLogger := logger.New(log.SetComponent("healthcheck server"))
return health.NewServer(serverAddress, healthLogger, isHealthy)
}

View File

@@ -0,0 +1,8 @@
package health
import "os"
func IsDocker() (ok bool) {
_, err := os.Stat("isdocker")
return err == nil
}

25
internal/noop/service.go Normal file
View File

@@ -0,0 +1,25 @@
package noop
import "context"
type Service struct {
name string
}
func New(name string) *Service {
return &Service{
name: name,
}
}
func (s *Service) String() string {
return s.name + " (no-op)"
}
func (s *Service) Start(_ context.Context) (_ <-chan error, _ error) {
return nil, nil //nolint:nilnil
}
func (s *Service) Stop() (stopErr error) {
return nil
}