Every process that layers database-owned settings now publishes the version it applied, and re-stamps it every 30 seconds so the report time doubles as a liveness signal. A record older than 90 seconds belongs to a process that has stopped, which is the only way this surface can tell a live process from one that applied a version and then died. RuntimeConfig returned the merged config and discarded the versions it read, so no caller could report what it had applied. It now returns them, and archied and archie-gateway report each kind through boot.runtimeConfig at boot and on SIGHUP. A validation failure reports every kind with the error, because the layering is all-or-nothing, and leaves the recorded version alone so a rejected edit does not erase the fact that an older one is live. The daemon also reports workflow execution settings as the watch applies them, and archie-messaging reports channel settings at startup. Reporting is best-effort by construction: the configuration a report describes has already been applied, so a failed write is logged and never returned. That keeps it off the reload's error path, where it would have raised a banner saying a reload failed when its config took effect. applystatus.New refuses a process name Processes() does not list. The reader renders known names, so an invented one would be invisible rather than wrong, which is the same thing the page means by a process that never started. Relates to archie-core-pskb.
77 lines
2.9 KiB
Go
77 lines
2.9 KiB
Go
package archiemessaging
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/controlplane"
|
|
"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/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()
|
|
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()
|
|
chatSettings, channelVersion, settingsErr := controlplane.NewRPCClient(stateStore.ControlPlane()).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, controlplane.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)
|
|
}
|
|
|
|
srv, err := compose(ctx, deps{
|
|
Config: cfg,
|
|
Log: log,
|
|
Chat: chat,
|
|
Health: health,
|
|
Settings: settings,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return srv.Start(ctx)
|
|
}
|