Files
archie/internal/app/archiemessaging/channel_status.go
T
sam b703c3663b feat(messaging): route a channel reload, and refuse one that cannot reload
archie-core-np7l's service half -- the reload action and its truthful refusal. The
request surface that carries the instruction from the dashboard is its own change,
because it is its own object with its own lifetime (the reasoning is recorded in
docs/architecture/migration-decisions.md).

Service.ReloadChannel routes to the channel's own seam rather than reimplementing
it: the telegram front-end re-reads its token and allowlist, which is what its own
/restart path calls. A channel that cannot reload is refused with a reason instead
of accepted and ignored, because the dashboard offers the action on the strength of
the capability the descriptor declares, and an accepted-then-nothing reload is a
lie the operator cannot see through.

The capability and the action now come from one source. Before this a descriptor
declared ReloadSupported by NAME, so a channel could announce reload support and
have nothing behind it; reloadable() decides both, and the descriptor test builds
a real gateway with a seam rather than a fake named "telegram".

Verified: task check exit 0. Coverage: the refusal names the reason for a channel
that declares no support and for an unknown channel; the service reaches telegram's
own reload seam and the re-read config lands; and the declaration and the action
cannot drift, which is asserted by reloading every composed channel and requiring
the refusal specifically to be "does not support reload" -- a declared reload may
still fail on the configuration it re-reads, and that is a different fact.
2026-09-22 08:15:32 +10:00

141 lines
5.3 KiB
Go

package archiemessaging
import (
"context"
"fmt"
"time"
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/channels/status"
"github.com/samcharles93/archie-core/internal/channels/telegram"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// channelDescriptors declares the operator-facing identity of each composed
// channel. Only what an operator may see: no configuration value ever enters a
// descriptor, so the manager cannot become a second source for a token or a
// credential.
func channelDescriptors(instances []channelInstance) []status.Descriptor {
descriptors := make([]status.Descriptor, 0, len(instances))
for _, instance := range instances {
descriptors = append(descriptors, status.Descriptor{
ID: instance.name,
Name: instance.name,
Configured: true,
// The capability comes from the same place the action does, so a
// channel cannot declare reload support and then answer nothing.
ReloadSupported: reloadable(instance),
})
}
return descriptors
}
// reloadable reports whether this channel can re-read its configuration. It is
// the single source for both the capability a descriptor declares and the action
// a reload performs; deriving them separately is how a dashboard ends up offering
// a button that reports success and changes nothing (archie-core-np7l).
func reloadable(instance channelInstance) bool {
gateway, ok := instance.channel.(*telegram.Gateway)
return ok && gateway.Reload != nil
}
// ReloadChannel asks one channel to re-read its configuration. A channel that
// does not support reload is refused with a reason rather than accepted and
// ignored: the dashboard offers the action on the strength of the capability its
// descriptor declares, so an accepted-then-nothing reload is a lie the operator
// has no way to see through.
//
// The reload itself is the channel's own seam -- the telegram front-end re-reads
// its token and allowlist -- so this function adds routing and a truthful
// refusal, not a second implementation.
func (s *Service) ReloadChannel(_ context.Context, id string) error {
for _, instance := range s.channels {
if instance.name != id {
continue
}
gateway, ok := instance.channel.(*telegram.Gateway)
if !ok || gateway.Reload == nil {
return fmt.Errorf("messaging: channel %q does not support reload", id)
}
if err := gateway.Reload(gateway); err != nil {
return fmt.Errorf("messaging: reload %q: %w", id, err)
}
return nil
}
return fmt.Errorf("messaging: no channel %q", id)
}
// lifecycleFor returns the report a channel writes its own state through. Each
// channel gets its own closure, so one channel's failure marks only itself --
// which is the reason the manager is keyed by channel rather than being one flag
// on the service.
//
// Before this, Start handed every channel an empty channels.Lifecycle{}, so
// nothing recorded starting, running or failed anywhere and
// internal/channels/status.Manager had no production writer at all
// (archie-core-8cda.6.8). The channels have always reported: all three call
// 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); 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)
}
}
}
}
// ChannelStatus reports the last known state of every composed channel. It is the
// producer side of the dashboard surface: whatever carries this to the UI reads
// here rather than reconstructing it, and the manager stays the one place channel
// lifecycle is recorded.
func (s *Service) ChannelStatus() []status.Status {
return s.status.Snapshot()
}