fix(archiemessaging): announce READY after channels bind; run notifying units as Type=notify

This commit is contained in:
sam committed 2026-10-17 17:15:34 +11:00
1 parent a2ade2ee42
commit fefbe19011
6 files changed
+141 -13

No files matched your search

+10 -4
View File
@@ -244,7 +244,10 @@ StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Type=simple
Type=notify
NotifyAccess=main
# Beats only while the run loop is polling, so a wedged loop is restarted.
WatchdogSec=30s
ExecStart=${ARCHIE_BIN_DIR}/archied -config ${ARCHIE_CONFIG_DIR}/config.toml
EnvironmentFile=-${ENV_FILE}
# archied reloads its configuration on SIGHUP. Without ExecReload,
@@ -266,7 +269,8 @@ Description=Archie State Store Service
After=network.target
[Service]
Type=simple
Type=notify
NotifyAccess=main
ExecStart=${ARCHIE_BIN_DIR}/archie-state-store -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:9090 -ready-addr 127.0.0.1:9091
EnvironmentFile=-${ENV_FILE}
Restart=on-failure
@@ -282,7 +286,8 @@ Description=Archie Gateway Service
After=network.target
[Service]
Type=simple
Type=notify
NotifyAccess=main
ExecStart=${ARCHIE_BIN_DIR}/archie-gateway -config ${ARCHIE_CONFIG_DIR}/config.toml -listen 127.0.0.1:8585
EnvironmentFile=-${ENV_FILE}
Restart=on-failure
@@ -318,7 +323,8 @@ After=network.target archie-gateway.service archie-state-store.service
Wants=archie-gateway.service archie-state-store.service
[Service]
Type=simple
Type=notify
NotifyAccess=main
ExecStart=${ARCHIE_BIN_DIR}/archie-messaging -config ${ARCHIE_CONFIG_DIR}/config.toml
EnvironmentFile=-${ENV_FILE}
Restart=on-failure
@@ -69,7 +69,7 @@ func (s *Service) ReloadChannel(_ context.Context, id string) error {
func (s *Service) lifecycleFor(id string) channels.Lifecycle {
return channels.Lifecycle{
Starting: func() { s.status.MarkStarting(id); s.signalPublish() },
Running: func() { s.status.MarkRunning(id); s.signalPublish() },
Running: func() { s.status.MarkRunning(id); s.signalPublish(); s.signalRunning(id) },
}
}
+5 -7
View File
@@ -15,12 +15,10 @@ import (
"github.com/samcharles93/archie-core/internal/channels/telegram"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/applystatus"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/messaging"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc"
"github.com/samcharles93/archie-core/internal/releaseupdate"
"github.com/samcharles93/archie-core/internal/sdnotify"
"github.com/samcharles93/archie-core/internal/secret"
)
@@ -36,7 +34,6 @@ type deps struct {
ExtensionChannels []*channelInstance
Log *slog.Logger
Chat messaging.ChatContract
Health *health.Registry
Settings *messaging.SettingsCommand
// Secrets resolves channel credential references, including those naming an
// extension engine.
@@ -98,8 +95,10 @@ type Service struct {
secrets *secret.Registry
log *slog.Logger
chat messaging.ChatContract
health *health.Registry
channels []*channelInstance
// runningSignal carries a channel's name each time it reports Running, so
// startup can announce readiness only once every channel has bound.
runningSignal chan string
// settingsSource re-reads the stored channel settings. Nil disables the
// reconcile loop.
settingsSource chatSettingsSource
@@ -132,7 +131,6 @@ func compose(ctx context.Context, d deps) (*Service, error) {
secrets: d.Secrets,
log: d.Log,
chat: d.Chat,
health: d.Health,
settingsSource: d.SettingsSource,
reporter: d.ApplyReporter,
reconcileInterval: d.ReconcileInterval,
@@ -148,6 +146,7 @@ func compose(ctx context.Context, d deps) (*Service, error) {
return nil, err
}
srv.channels = instances
srv.runningSignal = make(chan string, len(instances))
srv.status = status.NewManager(channelDescriptors(srv.channels))
srv.statusWriter = d.ChannelStatus
srv.publish = make(chan struct{}, 1)
@@ -249,8 +248,7 @@ func (s *Service) Start(ctx context.Context) error {
go s.runChannel(ctx, c)
}
// Notify systemd once every channel has started.
sdnotify.Ready(s.log)
s.announceReady(ctx, s.awaitedChannels())
<-ctx.Done()
+51
View File
@@ -2,10 +2,12 @@ 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.
@@ -19,3 +21,52 @@ func newReadinessRegistry(o Options, chat messaging.ChatContract) *health.Regist
}),
)
}
// 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)
}
@@ -0,0 +1,74 @@
package archiemessaging
import (
"context"
"encoding/json"
"log/slog"
"net"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/domain/messaging"
)
// gatedChannel reports Running only once bind is closed, standing in for a
// channel whose listener has not bound yet.
type gatedChannel struct{ bind chan struct{} }
func (c *gatedChannel) Name() string { return "gated" }
func (c *gatedChannel) Start(ctx context.Context, _ messaging.ChatContract, lifecycle channels.Lifecycle) error {
select {
case <-ctx.Done():
return nil
case <-c.bind:
}
lifecycle.Running()
<-ctx.Done()
return nil
}
func (c *gatedChannel) Stop(context.Context) error { return nil }
func (c *gatedChannel) ConfigSchema() json.RawMessage { return nil }
func (c *gatedChannel) ValidateConfig(map[string]any) error { return nil }
// READY=1 must not reach systemd before every channel has bound, or a channel
// that then fails to bind is reported ready.
func TestReadyWaitsForChannelsToBind(t *testing.T) {
addr := filepath.Join(t.TempDir(), "s")
sock, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: addr, Net: "unixgram"})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = sock.Close() })
t.Setenv("NOTIFY_SOCKET", addr)
ch := &gatedChannel{bind: make(chan struct{})}
inst := &channelInstance{name: "gated", channel: ch}
srv, err := compose(context.Background(), deps{Log: slog.Default(), ExtensionChannels: []*channelInstance{inst}})
if err != nil {
t.Fatalf("compose: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go func() { _ = srv.Start(ctx) }()
read := func(wait time.Duration) string {
_ = sock.SetReadDeadline(time.Now().Add(wait))
buf := make([]byte, 64)
n, err := sock.Read(buf)
if err != nil {
return ""
}
return string(buf[:n])
}
if got := read(200 * time.Millisecond); got != "" {
t.Fatalf("announced %q before the channel bound", got)
}
close(ch.bind)
if got := read(5 * time.Second); got != "READY=1" {
t.Fatalf("after bind got %q, want READY=1", got)
}
}
-1
View File
@@ -103,7 +103,6 @@ func Run(ctx context.Context, o Options) error {
ExtensionChannels: extensionChannels,
Log: log,
Chat: chat,
Health: health,
Settings: settings,
SettingsSource: settingsSource,
ApplyReporter: applyReporter,