fix(archied): stop retrying task outcomes for a channel messaging does not have
This commit is contained in:
1 parent
793569f6e1
commit
9a08048744
4 files changed
+65
-3
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user