Files
archie/internal/app/archiemessaging/readiness.go
T

73 lines
2.2 KiB
Go

package archiemessaging
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/infrastructure/readiness"
"github.com/samcharles93/archie-core/internal/sdnotify"
)
// newReadinessRegistry builds the health registry for the Messaging Service.
// Messaging is ready when the Gateway Service responds to Snapshot within
// the dependency timeout.
func newReadinessRegistry(o Options, chat messaging.ChatContract) *health.Registry {
return health.NewRegistry(
readiness.NewContractProbe("gateway", o.DependencyTimeout, func(ctx context.Context) error {
_, err := chat.Snapshot(ctx)
return err
}),
)
}
// readyWait bounds how long startup waits for channels to bind before
// announcing anyway, so a channel that never reports Running cannot hang the
// unit until TimeoutStartSec. Such a channel shows as failed on the dashboard.
const readyWait = 30 * time.Second
// signalRunning records that a channel has bound. It never blocks: only the
// first report per channel matters, and a restart's report is dropped.
func (s *Service) signalRunning(id string) {
select {
case s.runningSignal <- id:
default:
}
}
// awaitedChannels names the channels that will bind at startup. A disabled
// channel has nothing to run and is not waited for.
func (s *Service) awaitedChannels() map[string]bool {
awaited := map[string]bool{}
for _, c := range s.channels {
c.mu.Lock()
if c.channel != nil {
awaited[c.name] = true
}
c.mu.Unlock()
}
return awaited
}
// announceReady sends READY=1 once every awaited channel has reported Running,
// so a channel that fails to bind (a port already in use) is never preceded by
// a readiness announcement.
func (s *Service) announceReady(ctx context.Context, awaited map[string]bool) {
timeout := time.NewTimer(readyWait)
defer timeout.Stop()
for len(awaited) > 0 {
select {
case <-ctx.Done():
return
case <-timeout.C:
s.log.Warn("announcing readiness before every channel bound", "waiting_for", len(awaited))
sdnotify.Ready(s.log)
return
case id := <-s.runningSignal:
delete(awaited, id)
}
}
sdnotify.Ready(s.log)
}