From 9a08048744ed4ea45772571fdd9de8a22ecdc483 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 24 Oct 2026 17:02:50 +1100 Subject: [PATCH] fix(archied): stop retrying task outcomes for a channel messaging does not have --- internal/app/archied/outcome_post.go | 11 ++++- internal/app/archied/outcome_post_test.go | 47 +++++++++++++++++++ internal/app/archiemessaging/compose.go | 2 +- .../messagingrpc/messagingrpc.go | 8 +++- 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 internal/app/archied/outcome_post_test.go diff --git a/internal/app/archied/outcome_post.go b/internal/app/archied/outcome_post.go index 743f2684c..e0c2d121e 100644 --- a/internal/app/archied/outcome_post.go +++ b/internal/app/archied/outcome_post.go @@ -7,6 +7,9 @@ import ( "strings" "time" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) @@ -41,7 +44,13 @@ func postPendingOutcomes(ctx context.Context, tasks storecontract.OutcomePoster, // An origin that names no chat can never be delivered; marking it // posted keeps it from being retried forever. if channel, chatID, ok := splitOrigin(t.Origin); ok { - if err := deliver(ctx, channel, chatID, outcomeText(t)); err != nil { + err := deliver(ctx, channel, chatID, outcomeText(t)) + switch { + case status.Code(err) == codes.NotFound: + // Messaging has no such channel (the dashboard chat, say), so + // a retry can never deliver it. + log.Info("task outcome has no messaging channel", "task", t.ID, "channel", channel) + case err != nil: log.Warn("post task outcome", "task", t.ID, "err", err) continue } diff --git a/internal/app/archied/outcome_post_test.go b/internal/app/archied/outcome_post_test.go new file mode 100644 index 000000000..75e6edcfa --- /dev/null +++ b/internal/app/archied/outcome_post_test.go @@ -0,0 +1,47 @@ +package archied + +import ( + "context" + "errors" + "log/slog" + "testing" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +type outcomeStore struct{ posted []int64 } + +func (s *outcomeStore) UnpostedOutcomes(context.Context) ([]*task.Task, error) { + return []*task.Task{{ID: 7, Origin: "web:chat-1"}}, nil +} + +func (s *outcomeStore) MarkOutcomePosted(_ context.Context, id int64) error { + s.posted = append(s.posted, id) + return nil +} + +func TestPostPendingOutcomes(t *testing.T) { + tests := []struct { + name string + err error + posted bool + }{ + {"delivered", nil, true}, + {"no such channel", status.Error(codes.NotFound, "messaging: no such channel"), true}, + {"channel stopped", status.Error(codes.FailedPrecondition, "messaging: channel unavailable"), false}, + {"messaging down", errors.New("connection refused"), false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + store := &outcomeStore{} + deliver := func(context.Context, string, string, string) error { return tt.err } + postPendingOutcomes(context.Background(), store, deliver, slog.New(slog.DiscardHandler)) + if got := len(store.posted) == 1; got != tt.posted { + t.Fatalf("posted = %v, want %v", got, tt.posted) + } + }) + } +} diff --git a/internal/app/archiemessaging/compose.go b/internal/app/archiemessaging/compose.go index e80b6a65f..b335995c5 100644 --- a/internal/app/archiemessaging/compose.go +++ b/internal/app/archiemessaging/compose.go @@ -296,5 +296,5 @@ func (s *Service) Deliver(ctx context.Context, channel, chatID, text string) err } return err } - return fmt.Errorf("%w: no channel %q", messagingrpc.ErrUnavailable, channel) + return fmt.Errorf("%w: %q", messagingrpc.ErrNoChannel, channel) } diff --git a/internal/infrastructure/messagingrpc/messagingrpc.go b/internal/infrastructure/messagingrpc/messagingrpc.go index 709674b43..4881f835e 100644 --- a/internal/infrastructure/messagingrpc/messagingrpc.go +++ b/internal/infrastructure/messagingrpc/messagingrpc.go @@ -20,9 +20,13 @@ import ( ) // ErrUnavailable marks a delivery the channel could not make now: the channel -// is unknown, stopped or cannot send. The caller may retry later. +// is stopped or cannot send. The caller may retry later. var ErrUnavailable = errors.New("messaging: channel unavailable") +// ErrNoChannel marks a delivery to a channel this service does not have. +// Retrying cannot succeed. +var ErrNoChannel = errors.New("messaging: no such channel") + // Deliverer sends text to one chat on a named channel. type Deliverer interface { Deliver(ctx context.Context, channel, chatID, text string) error @@ -47,6 +51,8 @@ func (s *server) Deliver(ctx context.Context, req *pb.DeliverRequest) (*pb.Deliv switch { case err == nil: return &pb.DeliverResponse{}, nil + case errors.Is(err, ErrNoChannel): + return nil, status.Error(codes.NotFound, err.Error()) case errors.Is(err, ErrUnavailable): return nil, status.Error(codes.FailedPrecondition, err.Error()) default: