feat(messaging): serve MessagingService.Deliver and deliver chat schedules through it
This commit is contained in:
22 files changed
+689
-46
No files matched your search
@@ -48,6 +48,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/extension"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/taskactions"
|
||||
@@ -167,14 +168,14 @@ type boot struct {
|
||||
|
||||
chatTasks gateway.TaskCreator
|
||||
defaultChatIdentity string
|
||||
// messaging delivers outbound chat messages through the Messaging Service.
|
||||
messaging *messagingrpc.Client
|
||||
|
||||
startGateways []func()
|
||||
trees *worktree.Manager
|
||||
identityRunners []*daemon.IdentityRunner
|
||||
d *daemon.Daemon
|
||||
// schedulingEngine is the cron/scheduling ticker engine (setupScheduling).
|
||||
// Nil when no chat task creator is configured, in which case startServices
|
||||
// leaves it unstarted rather than running with no reachable job kind.
|
||||
schedulingEngine *scheduling.Engine
|
||||
|
||||
// kindWorkflows/labelWorkflows are the resolved kind/label -> workflow
|
||||
@@ -469,6 +470,12 @@ func (b *boot) setupGatewayClient() error {
|
||||
}
|
||||
b.addCleanup(cleanup)
|
||||
b.chat = &webui.ChatService{Contract: contract}
|
||||
messaging, closeMessaging, err := composeMessaging(b.cfg.Services, b.secrets)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.addCleanup(closeMessaging)
|
||||
b.messaging = messaging
|
||||
b.setupReadinessProbes()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
|
||||
@@ -28,3 +29,13 @@ func composeChatContract(services config.Services, secrets *secret.Registry, opt
|
||||
}
|
||||
return client, cleanup, nil
|
||||
}
|
||||
|
||||
// composeMessaging builds the Messaging Service client. The connection is
|
||||
// lazy, so an absent Messaging Service fails each delivery, not the daemon.
|
||||
func composeMessaging(services config.Services, secrets *secret.Registry) (*messagingrpc.Client, func(), error) {
|
||||
target, err := services.RequireTarget(config.ServiceNameMessaging)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return messagingrpc.Dial(target, services.ResolvedToken(config.ServiceNameMessaging, secrets.Getenv))
|
||||
}
|
||||
@@ -51,30 +51,32 @@ func schedulingConfig(input configuration.SchedulingInput, bus *events.Bus) (sch
|
||||
// subsystem, and the corresponding Stop is registered here via addCleanup so
|
||||
// shutdown ordering matches every other daemon-lifecycle subsystem.
|
||||
//
|
||||
// Only the "workflow" kind is wired: it rides gateway.TaskCreator, the same
|
||||
// non-forge-backed task creation path chat's /spawn uses, which is the one
|
||||
// production capability here that does not require inventing a channel
|
||||
// courier or a synthetic forge owner/repo. A deployment with no chat task
|
||||
// creator configured (no [chat] identity profiles) leaves the engine
|
||||
// unstarted rather than running with no reachable job kind.
|
||||
// The "chat" kind delivers through the Messaging Service. The "workflow" kind
|
||||
// rides gateway.TaskCreator, the path chat's /spawn uses, so it is wired only
|
||||
// when [chat] identity profiles configure one.
|
||||
func (b *boot) setupScheduling() error {
|
||||
cfg, err := schedulingConfig(b.doc.Scheduling, b.bus)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: %w", err)
|
||||
}
|
||||
|
||||
if b.chatTasks == nil {
|
||||
b.log.Warn("scheduling: no chat task creator configured; ticker engine not started")
|
||||
return nil
|
||||
}
|
||||
|
||||
store := scheduleResourceStore{client: b.controlPlane}
|
||||
|
||||
workflowRunner, err := crondelivery.NewWorkflowTask(store, spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity})
|
||||
chatRunner, err := crondelivery.NewChatCourier(store, func(ctx context.Context, target scheduling.Target, text string) error {
|
||||
return b.messaging.Deliver(ctx, target.Channel, target.ChatID, text)
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build workflow runner: %w", err)
|
||||
return fmt.Errorf("scheduling: build chat runner: %w", err)
|
||||
}
|
||||
router, err := crondelivery.NewRouter(store, map[string]scheduling.Runner{scheduling.KindWorkflow: workflowRunner}, cfg.Events)
|
||||
runners := map[string]scheduling.Runner{scheduling.KindChat: chatRunner}
|
||||
if b.chatTasks != nil {
|
||||
workflowRunner, err := crondelivery.NewWorkflowTask(store, spawnTaskSubmitter{creator: b.chatTasks, identity: b.defaultChatIdentity})
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build workflow runner: %w", err)
|
||||
}
|
||||
runners[scheduling.KindWorkflow] = workflowRunner
|
||||
}
|
||||
router, err := crondelivery.NewRouter(store, runners, cfg.Events)
|
||||
if err != nil {
|
||||
return fmt.Errorf("scheduling: build router: %w", err)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package archiemessaging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"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/sdnotify"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
)
|
||||
@@ -263,3 +265,22 @@ func (s *Service) Stop() {
|
||||
}
|
||||
s.log.Info("messaging service stopped")
|
||||
}
|
||||
|
||||
// Deliver sends text to chatID through the named running channel.
|
||||
func (s *Service) Deliver(ctx context.Context, channel, chatID, text string) error {
|
||||
for _, instance := range s.channels {
|
||||
if instance.name != channel {
|
||||
continue
|
||||
}
|
||||
sender, ok := instance.current().(channels.Sender)
|
||||
if !ok {
|
||||
return fmt.Errorf("%w: channel %q cannot send", messagingrpc.ErrUnavailable, channel)
|
||||
}
|
||||
err := sender.Send(ctx, chatID, text)
|
||||
if errors.Is(err, channels.ErrNotRunning) {
|
||||
return fmt.Errorf("%w: channel %q is not running", messagingrpc.ErrUnavailable, channel)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("%w: no channel %q", messagingrpc.ErrUnavailable, channel)
|
||||
}
|
||||
@@ -30,6 +30,7 @@ type ResolvedConfig struct {
|
||||
type projection struct {
|
||||
gateway ServiceTarget
|
||||
stateStore ServiceTarget
|
||||
messaging config.ServiceConnection
|
||||
telegram config.TelegramConfig
|
||||
workDir string
|
||||
botUser string
|
||||
@@ -87,6 +88,7 @@ func project(cfg config.Config) projection {
|
||||
Token: cfg.Services.Get(config.ServiceNameGateway).TargetToken,
|
||||
},
|
||||
stateStore: ServiceTarget{Target: cfg.Services.Get(config.ServiceNameState).Target, Token: cfg.Services.Get(config.ServiceNameState).TargetToken},
|
||||
messaging: cfg.Services.Get(config.ServiceNameMessaging),
|
||||
telegram: cfg.Chat.Telegram,
|
||||
workDir: cfg.WorkDir,
|
||||
botUser: cfg.BotUser,
|
||||
@@ -102,6 +104,9 @@ func withEnvTokens(o Options) Options {
|
||||
if o.StateStore.Token == "" {
|
||||
o.StateStore.Token = os.Getenv("STATE_STORE_TOKEN")
|
||||
}
|
||||
if o.Token == "" {
|
||||
o.Token = os.Getenv("MESSAGING_TOKEN")
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
@@ -118,6 +123,12 @@ func merge(o Options, p projection) Options {
|
||||
if o.StateStore.Token == "" {
|
||||
o.StateStore.Token = p.stateStore.Token
|
||||
}
|
||||
if o.Listen == "" {
|
||||
o.Listen = p.messaging.Listen
|
||||
}
|
||||
if o.Token == "" {
|
||||
o.Token = p.messaging.TargetToken
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
// Process defaults.
|
||||
const (
|
||||
defaultGatewayTarget = "127.0.0.1:8585"
|
||||
defaultListen = "127.0.0.1:8586"
|
||||
defaultDependencyTimeout = 5 * time.Second
|
||||
defaultShutdownTimeout = 5 * time.Second
|
||||
)
|
||||
@@ -29,8 +30,12 @@ type Options struct {
|
||||
Config string
|
||||
Overlay string
|
||||
|
||||
Gateway ServiceTarget
|
||||
StateStore ServiceTarget
|
||||
Gateway ServiceTarget
|
||||
StateStore ServiceTarget
|
||||
// Listen is where MessagingService is served; Token is the bearer token
|
||||
// its callers present on a non-loopback address.
|
||||
Listen string
|
||||
Token string
|
||||
DependencyTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
@@ -39,6 +44,9 @@ func withDefaults(o Options) Options {
|
||||
if o.Gateway.Target == "" {
|
||||
o.Gateway.Target = defaultGatewayTarget
|
||||
}
|
||||
if o.Listen == "" {
|
||||
o.Listen = defaultListen
|
||||
}
|
||||
if o.DependencyTimeout <= 0 {
|
||||
o.DependencyTimeout = defaultDependencyTimeout
|
||||
}
|
||||
@@ -60,7 +68,7 @@ func (o Options) validate() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return validateServiceTarget("messaging listen", ServiceTarget{Target: o.Listen, Token: o.Token})
|
||||
}
|
||||
|
||||
func validateServiceTarget(serviceName string, target ServiceTarget) error {
|
||||
|
||||
@@ -4,6 +4,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
@@ -14,6 +17,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/extension"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/messagingrpc"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/secretengine"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
|
||||
"github.com/samcharles93/archie-core/internal/secret"
|
||||
@@ -101,7 +105,28 @@ func Run(ctx context.Context, o Options) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return serve(ctx, cfg.Options, srv, log)
|
||||
}
|
||||
|
||||
// serve runs srv and serves MessagingService over it until ctx is cancelled.
|
||||
func serve(ctx context.Context, o Options, srv *Service, log *slog.Logger) error {
|
||||
opts, err := messagingrpc.ServerOptions(o.Listen, o.Token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", o.Listen)
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen messaging (%s): %w", o.Listen, err)
|
||||
}
|
||||
server := grpc.NewServer(opts...)
|
||||
messagingrpc.RegisterServer(server, srv)
|
||||
go func() {
|
||||
if err := server.Serve(listener); err != nil {
|
||||
log.Error("messaging gRPC server stopped", "err", err)
|
||||
}
|
||||
}()
|
||||
defer server.GracefulStop()
|
||||
log.Info("messaging service listening", "listen", o.Listen)
|
||||
return srv.Start(ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,13 @@ func validateSchedules(jobs []scheduling.JobSpec) error {
|
||||
return fmt.Errorf("duplicate schedule %q", job.ID)
|
||||
}
|
||||
seen[job.ID] = struct{}{}
|
||||
if job.Kind != scheduling.KindWorkflow {
|
||||
switch job.Kind {
|
||||
case scheduling.KindWorkflow:
|
||||
case scheduling.KindChat:
|
||||
if job.Target.Channel == "" || job.Target.ChatID == "" || strings.TrimSpace(job.Payload.Text) == "" {
|
||||
return fmt.Errorf("chat schedule %q needs a channel, chat ID and message", job.ID)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("schedule %q has unsupported kind %q", job.ID, job.Kind)
|
||||
}
|
||||
if err := job.Schedule.Validate(); err != nil {
|
||||
|
||||
Reference in new issue
Block a user