Files
archie/internal/app/archiemessaging/run.go
T
sam 9ccb6e2418 feat(messaging): restart only the channel whose settings changed
archie-core-zfb0.4. Channel settings were restart-required as a whole: a
change to one channel needed a process restart that restarted every channel.

The Messaging Service now reconciles the stored channel-settings resource on
the apply-status restamp interval (docs/prds/plugin-settings-live.md), re-layers
it over the file document exactly as boot does, and restarts only the channel
whose transport settings changed. Every other channel keeps serving.

A restart is stop-and-start, not an in-place reload: each channel gets its own
supervisor and a rebuild factory, so email and webhook -- which have no reload
seam -- are restarted too. Telegram's Start still calls dropPendingUpdates
before it long-polls, so the rule holds on the reconcile path as on /restart. A
replacement is built before the running channel is touched, so a configuration
this build cannot run leaves the running channel serving.

The composed set and the bound listen addresses are process bindings: enabling
or disabling a channel, or changing chat.webhook_addr / chat.email.listen_addr,
is refused and reported for a restart rather than applied. A channel rebuild
that fails is not recorded as applied, so it is retried on the next tick. The
applied version reaches apply status, and ChannelSettingsKind flips to live.

Files: internal/app/archiemessaging/{compose,reconcile,supervise,run}.go,
controlplane ApplyMode, archied runtime watch, configuration.md, the config
skill matrix and the regenerated dev-docs artifact.

Verified: go test ./internal/app/archiemessaging/... (race clean); go test
./cmd/archie-messaging/... ./internal/app/controlplane/... ./internal/app/archied/...
./internal/config/...; go vet and golangci-lint run on those packages (0 issues);
go fix ./... and golangci-lint fmt produced no diff; task docs:artifact:check.
2026-10-02 11:31:05 +10:00

95 lines
3.9 KiB
Go

package archiemessaging
import (
"context"
"fmt"
"log/slog"
"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"
"github.com/samcharles93/archie-core/internal/secret"
)
// Run runs the standalone Messaging Service process until ctx is cancelled.
func Run(ctx context.Context, o Options) error {
log := slog.Default().With("service", "archie-messaging")
cfg, err := Resolve(o, log)
if err != nil {
return fmt.Errorf("resolve messaging options: %w", err)
}
chat, closeGateway, err := gatewayrpc.Dial(cfg.Options.Gateway.Target, cfg.Options.Gateway.Token)
if err != nil {
return fmt.Errorf("dial gateway (%s): %w", cfg.Options.Gateway.Target, err)
}
defer closeGateway()
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
// settingsSource and applyReporter are the live channel-settings path: the
// control-plane client to re-read the resource, and the record of what this
// process applied. Both stay nil without a State Store, which leaves the
// service with no live path.
var settingsSource chatSettingsSource
var applyReporter *applystatus.Reporter
var appliedVersion int64
if cfg.Options.StateStore.Target != "" {
stateStore, closeClient, dialErr := staterpc.Dial(cfg.Options.StateStore.Target, cfg.Options.StateStore.Token)
if dialErr != nil {
return fmt.Errorf("dial state store control plane (%s): %w", cfg.Options.StateStore.Target, dialErr)
}
closeStateStore = closeClient
defer closeStateStore()
controlPlaneClient := controlplanerpc.NewRPCClient(stateStore.ControlPlane())
chatSettings, channelVersion, settingsErr := controlPlaneClient.RuntimeChatConfig(ctx, config.ChatConfig{
Telegram: cfg.Telegram, Email: cfg.Email, Webhook: cfg.Webhook, WebhookAddr: cfg.WebhookAddr, ShowToolCalls: cfg.ShowToolCalls,
})
if settingsErr != nil {
return fmt.Errorf("load channel settings: %w", settingsErr)
}
reporter := applystatus.New(applystatus.Messaging, stateStore, log)
reporter.Report(ctx, controlplanerpc.ChannelSettingsKind, channelVersion, nil)
go reporter.Run(ctx)
secrets := secret.NewRegistry()
cfg.Telegram, cfg.Email, cfg.Webhook, cfg.WebhookAddr, cfg.ShowToolCalls = chatSettings.Telegram, chatSettings.Email, chatSettings.Webhook, chatSettings.WebhookAddr, chatSettings.ShowToolCalls
cfg.TelegramToken, settingsErr = resolveTelegramToken(chatSettings.Telegram, secrets)
if settingsErr != nil {
return fmt.Errorf("resolve database telegram token: %w", settingsErr)
}
cfg.WebhookSecret, settingsErr = resolveWebhookSecret(chatSettings.Webhook, secrets)
if settingsErr != nil {
return fmt.Errorf("resolve database webhook secret: %w", settingsErr)
}
settings = messaging.NewSettingsCommand(messagingControlPlane{client: stateStore.ControlPlane()}).WithIdentities(stateStore)
channelStatusStore = stateStore
settingsSource, applyReporter, appliedVersion = controlPlaneClient, reporter, channelVersion
}
srv, err := compose(ctx, deps{
ChannelStatus: channelStatusStore,
Config: cfg,
Log: log,
Chat: chat,
Health: health,
Settings: settings,
SettingsSource: settingsSource,
ApplyReporter: applyReporter,
AppliedVersion: appliedVersion,
})
if err != nil {
return err
}
return srv.Start(ctx)
}