feat(state): carry channel lifecycle from the Messaging Service to the dashboard
archie-core-8cda.6.8's carrier. The service now records channel lifecycle and has nowhere to put it: /api/channels answered with nothing because webui.Server.Channels had no production source, and reload answered 501 for the same reason. This adds the surface the register decided on, following the split archie-core-ymut established for config snapshots -- a pair of administrative RPCs whose writer is one process and whose reader is the UI -- and wires both ends. Wire and store: PutChannelStatus / ListChannelStatus, with ChannelStatus carrying id, name, state, detail, configured, reload_supported and observed_at. The store holds one row per channel and replaces the reported set, so a channel the reporter no longer mentions is deleted rather than left standing as a stale "running"; an empty report clears the table, because hosting no channels is a fact worth reporting. A row without an id is refused: it could never be replaced or removed by a later report. Writer: the service publishes on every lifecycle transition through a coalescing signal -- a burst of transitions is one write, since a reader only wants the latest state of every channel -- plus a start-up report and a final report after shutdown, so a stopped service leaves the store honest. A failed write is logged and retried on the next signal; the dashboard losing channel state is worth a warning and never worth failing a channel start over. Reader: webui.Server.Channels becomes a source rather than a concrete manager, and webui.RemoteChannelStatus reads the store the way RemoteConfigView reads the config snapshot. A store it cannot reach reports nothing rather than a stale "running", and a state the reader does not recognise is carried through rather than dropped. The contract's own index gains the group in the same change that adds the RPCs -- a surface the grouped table does not list is one nobody finds -- and the count moves to 51. The register records why the surface is the State Store rather than a resource kind, and why reload gets its own surface rather than a field here: reading status must not be able to retrigger an action, a transient request is not distinguishable from state once anything polls it, and it would invert the writer/reader relationship the precedent establishes. Verified: task check exit 0. New coverage: the store's replace-the-set semantics and its refusal of an id-less row, the reader's mapping including an unknown state, and the publisher's start-up, transition and final reports.
This commit is contained in:
23 files changed
+1445
-406
No files matched your search
@@ -362,6 +362,9 @@ func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps {
|
||||
if as, ok := b.st.(storecontract.ApplyStatusStore); ok {
|
||||
deps.ApplyStatus = as
|
||||
}
|
||||
if cs, ok := b.st.(storecontract.ChannelStatusStore); ok {
|
||||
deps.ChannelStatus = cs
|
||||
}
|
||||
return deps
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/channels"
|
||||
"github.com/samcharles93/archie-core/internal/channels/status"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// channelDescriptors declares the operator-facing identity of each composed
|
||||
@@ -38,8 +42,57 @@ func channelDescriptors(instances []channelInstance) []status.Descriptor {
|
||||
// ReportStarting and ReportRunning on whichever lifecycle they are handed.
|
||||
func (s *Service) lifecycleFor(id string) channels.Lifecycle {
|
||||
return channels.Lifecycle{
|
||||
Starting: func() { s.status.MarkStarting(id) },
|
||||
Running: func() { s.status.MarkRunning(id) },
|
||||
Starting: func() { s.status.MarkStarting(id); s.signalPublish() },
|
||||
Running: func() { s.status.MarkRunning(id); s.signalPublish() },
|
||||
}
|
||||
}
|
||||
|
||||
// signalPublish asks the publisher to report the current set. The signal
|
||||
// coalesces: a burst of transitions is one write, because a reader only ever
|
||||
// wants the latest state of every channel.
|
||||
func (s *Service) signalPublish() {
|
||||
if s.statusWriter == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case s.publish <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// publishStatus writes the current set, or deletes it when the service hosts no
|
||||
// channels -- an empty report is a report, and leaving the previous set standing
|
||||
// would show channels that are gone.
|
||||
func (s *Service) publishStatus(ctx context.Context) error {
|
||||
reported := s.ChannelStatus()
|
||||
rows := make([]storecontract.ChannelStatus, 0, len(reported))
|
||||
for _, channel := range reported {
|
||||
rows = append(rows, storecontract.ChannelStatus{
|
||||
ID: channel.ID,
|
||||
Name: channel.Name,
|
||||
State: string(channel.State),
|
||||
Detail: channel.Detail,
|
||||
Configured: channel.Configured,
|
||||
ReloadSupported: channel.ReloadSupported,
|
||||
ObservedAt: time.Now(),
|
||||
})
|
||||
}
|
||||
return s.statusWriter.PutChannelStatus(ctx, rows)
|
||||
}
|
||||
|
||||
// publishLoop reports channel state until ctx ends. A failed write is logged and
|
||||
// retried on the next signal: the dashboard losing channel state is worth a
|
||||
// warning, and never worth failing a channel start over.
|
||||
func (s *Service) publishLoop(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-s.publish:
|
||||
if err := s.publishStatus(ctx); err != nil && ctx.Err() == nil {
|
||||
s.log.Warn("publishing channel status failed", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
)
|
||||
|
||||
// recordingChannelStatusStore records every report the service publishes.
|
||||
type recordingChannelStatusStore struct {
|
||||
mu sync.Mutex
|
||||
writes [][]storecontract.ChannelStatus
|
||||
}
|
||||
|
||||
func (s *recordingChannelStatusStore) PutChannelStatus(_ context.Context, channels []storecontract.ChannelStatus) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.writes = append(s.writes, append([]storecontract.ChannelStatus(nil), channels...))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *recordingChannelStatusStore) ChannelStatus(context.Context) ([]storecontract.ChannelStatus, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// waitForPublishedState polls the recorded reports until one carries the channel
|
||||
// in want, so the assertion races the publisher rather than the clock.
|
||||
func waitForPublishedState(t *testing.T, store *recordingChannelStatusStore, id, want string) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
store.mu.Lock()
|
||||
for _, write := range store.writes {
|
||||
for _, channel := range write {
|
||||
if channel.ID == id && channel.State == want {
|
||||
store.mu.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
store.mu.Unlock()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
t.Fatalf("no report carried %s as %q; reports were %+v", id, want, store.writes)
|
||||
}
|
||||
|
||||
// TestServicePublishesChannelStatus is the point of archie-core-8cda.6.8: the
|
||||
// report has to reach the store the dashboard reads, not stay in this process.
|
||||
// Without this the surface exists and no operator ever sees it.
|
||||
func TestServicePublishesChannelStatus(t *testing.T) {
|
||||
writer := &recordingChannelStatusStore{}
|
||||
srv, err := compose(t.Context(), deps{
|
||||
ChannelStatus: writer,
|
||||
Config: ResolvedConfig{
|
||||
Options: Options{ShutdownTimeout: 2 * time.Second},
|
||||
WebhookAddr: fmt.Sprintf("127.0.0.1:%d", freePort(t)),
|
||||
Webhook: config.WebhookRoute{Path: "/smoke"},
|
||||
},
|
||||
Log: slog.Default(),
|
||||
Chat: &recordingChatContract{routes: make(chan messaging.Inbound, 1), reply: "ok"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("compose: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
started := make(chan error, 1)
|
||||
go func() { started <- srv.Start(ctx) }()
|
||||
|
||||
// The start-up report and the channel's own "starting" coalesce by design --
|
||||
// signals coalesce, because a reader only ever wants the latest state of every
|
||||
// channel -- so the first published state is either "configured" or "starting"
|
||||
// depending on which side wins. What must hold is that the transitions reach
|
||||
// the store, and that the last word is the truth.
|
||||
waitForPublishedState(t, writer, "webhook", "running")
|
||||
|
||||
srv.Stop()
|
||||
select {
|
||||
case err := <-started:
|
||||
if err != nil {
|
||||
t.Fatalf("srv.Start returned %v, want a clean stop", err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("srv.Start did not return after Stop")
|
||||
}
|
||||
// The final report is what leaves the store honest about a stopped service.
|
||||
waitForPublishedState(t, writer, "webhook", "stopped")
|
||||
}
|
||||
@@ -16,15 +16,19 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"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/secret"
|
||||
)
|
||||
|
||||
type deps struct {
|
||||
Config ResolvedConfig
|
||||
Log *slog.Logger
|
||||
Chat messaging.ChatContract
|
||||
Health *health.Registry
|
||||
Settings *messaging.SettingsCommand
|
||||
// ChannelStatus is where the service publishes channel lifecycle for the
|
||||
// dashboard. Optional: nil leaves the report in-process only.
|
||||
ChannelStatus storecontract.ChannelStatusStore
|
||||
Config ResolvedConfig
|
||||
Log *slog.Logger
|
||||
Chat messaging.ChatContract
|
||||
Health *health.Registry
|
||||
Settings *messaging.SettingsCommand
|
||||
}
|
||||
|
||||
type channelInstance struct {
|
||||
@@ -38,7 +42,14 @@ type Service struct {
|
||||
// status owns channel lifecycle facts, one entry per composed channel. It is
|
||||
// the single writer channels report through (see lifecycleFor), and the
|
||||
// producer a dashboard surface reads.
|
||||
status *status.Manager
|
||||
status *status.Manager
|
||||
// statusWriter publishes that report where the dashboard reads it. Nil means
|
||||
// this composition has no store to publish to, which is the honest state for a
|
||||
// test or a process run without one.
|
||||
statusWriter storecontract.ChannelStatusStore
|
||||
// publish coalesces a burst of transitions into one write: a reader only ever
|
||||
// wants the latest state of every channel.
|
||||
publish chan struct{}
|
||||
cfg ResolvedConfig
|
||||
log *slog.Logger
|
||||
chat messaging.ChatContract
|
||||
@@ -95,6 +106,8 @@ func compose(ctx context.Context, d deps) (*Service, error) {
|
||||
}
|
||||
|
||||
srv.status = status.NewManager(channelDescriptors(srv.channels))
|
||||
srv.statusWriter = d.ChannelStatus
|
||||
srv.publish = make(chan struct{}, 1)
|
||||
return srv, nil
|
||||
}
|
||||
|
||||
@@ -161,6 +174,13 @@ func (s *Service) Start(ctx context.Context) error {
|
||||
|
||||
s.log.Info("messaging service started", "gateway_target", s.cfg.Options.Gateway.Target, "channels", len(s.channels))
|
||||
|
||||
if s.statusWriter != nil {
|
||||
go s.publishLoop(ctx)
|
||||
// Report the composed set before any channel reports itself, so a dashboard
|
||||
// opened during startup shows configured channels rather than none.
|
||||
s.signalPublish()
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, ch := range s.channels {
|
||||
c := ch
|
||||
@@ -191,6 +211,16 @@ func (s *Service) Start(ctx context.Context) error {
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if s.statusWriter != nil {
|
||||
// The final report carries the stopped states the loop above recorded.
|
||||
// Its own context, because ctx is already cancelled.
|
||||
finalCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), s.cfg.Options.ShutdownTimeout)
|
||||
defer cancel()
|
||||
if err := s.publishStatus(finalCtx); err != nil {
|
||||
s.log.Warn("publishing final channel status failed", "err", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/applystatus"
|
||||
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
@@ -32,6 +33,9 @@ func Run(ctx context.Context, o Options) error {
|
||||
health := newReadinessRegistry(cfg.Options, chat)
|
||||
var settings *messaging.SettingsCommand
|
||||
var closeStateStore func()
|
||||
// channelStatusStore is the same client, held where compose can reach it: the
|
||||
// channel report is published to the store this process already dials.
|
||||
var channelStatusStore storecontract.ChannelStatusStore
|
||||
if cfg.Options.StateStore.Target != "" {
|
||||
stateStore, closeClient, dialErr := staterpc.Dial(cfg.Options.StateStore.Target, cfg.Options.StateStore.Token)
|
||||
if dialErr != nil {
|
||||
@@ -59,14 +63,16 @@ func Run(ctx context.Context, o Options) error {
|
||||
return fmt.Errorf("resolve database webhook secret: %w", settingsErr)
|
||||
}
|
||||
settings = messaging.NewSettingsCommand(messagingControlPlane{client: stateStore.ControlPlane()}).WithIdentities(stateStore)
|
||||
channelStatusStore = stateStore
|
||||
}
|
||||
|
||||
srv, err := compose(ctx, deps{
|
||||
Config: cfg,
|
||||
Log: log,
|
||||
Chat: chat,
|
||||
Health: health,
|
||||
Settings: settings,
|
||||
ChannelStatus: channelStatusStore,
|
||||
Config: cfg,
|
||||
Log: log,
|
||||
Chat: chat,
|
||||
Health: health,
|
||||
Settings: settings,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -75,6 +75,13 @@ func compose(d deps) *webui.Server {
|
||||
if snapshots, ok := d.Store.(storecontract.ConfigSnapshotStore); ok {
|
||||
srv.ConfigSource = webui.RemoteConfigView(snapshots)
|
||||
}
|
||||
// Channel lifecycle follows the same split as the config view: the process
|
||||
// hosting the channels publishes, this one reads. Withholding it left
|
||||
// /api/channels answering with nothing while channels were running
|
||||
// (archie-core-8cda.6.8).
|
||||
if channels, ok := d.Store.(storecontract.ChannelStatusStore); ok {
|
||||
srv.Channels = webui.RemoteChannelStatus(channels)
|
||||
}
|
||||
// Mappings and bindings are ratified State Store contracts, and the same
|
||||
// client already carries them: withholding them would degrade two pages
|
||||
// that have an owner, which is a different thing from the intake surfaces
|
||||
|
||||
Reference in new issue
Block a user