Fix signup registration receipts and activation worker lifetime (#57)
This commit is contained in:
1 parent
bd675357bb
commit
abca15f047
6 files changed
+148
-13
No files matched your search
@@ -45,8 +45,6 @@ func Run(ctx context.Context, c Config, logger mlogger.Logger) error {
|
||||
defer cancel()
|
||||
_ = client.Disconnect(cleanup)
|
||||
}()
|
||||
startup, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
defer cancel()
|
||||
repo, err := store.New(logger, client.Database())
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -57,7 +55,7 @@ func Run(ctx context.Context, c Config, logger mlogger.Logger) error {
|
||||
}
|
||||
defer rt.Close()
|
||||
svc := service.New(repo, catalog, sender, string(c.Email.Driver), logger)
|
||||
broker, err := bus.New(startup, rt.Durable, rt.DurableAdmin, repo, svc, logger, c.Queue)
|
||||
broker, err := bus.New(ctx, rt.Durable, rt.DurableAdmin, repo, svc, logger, c.Queue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -35,9 +35,13 @@ func New(ctx context.Context, transport reliable.Transport, admin reliable.Admin
|
||||
}
|
||||
settings := reliable.DurableSettings{Enabled: true, Stream: config.Stream, Provisioning: config.Provisioning}
|
||||
consumer := reliable.DurableConsumerSettings{Durable: "email", Subject: subject, AckWaitSeconds: 60, MaxDeliver: -1, DeliverPolicy: reliable.DurableDeliverAll}
|
||||
if err := admin.EnsureConsumerResources(ctx, settings, consumer); err != nil {
|
||||
startup, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
defer cancel()
|
||||
if err := admin.EnsureConsumerResources(startup, settings, consumer); err != nil {
|
||||
return nil, merrors.OpaqueWrap(merrors.ErrServiceUnavailable, err, "configure notification messaging")
|
||||
}
|
||||
// NATS retains this context and unsubscribes when it ends. The worker must
|
||||
// live until service shutdown, independently of the provisioning deadline.
|
||||
subscription, err := transport.OpenConsumer(ctx, settings, consumer)
|
||||
if err != nil {
|
||||
return nil, merrors.OpaqueWrap(merrors.ErrServiceUnavailable, err, "subscribe notification worker")
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
merrors "git.sendico.io/tech/sxchnge/shared/errors"
|
||||
"git.sendico.io/tech/sxchnge/shared/messaging/reliable"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type outbox struct{ jobs []model.Notification }
|
||||
@@ -23,6 +24,50 @@ type publisher struct {
|
||||
records []reliable.Record
|
||||
}
|
||||
|
||||
type consumerTransport struct {
|
||||
reliable.Transport
|
||||
reliable.Subscription
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (t *consumerTransport) OpenConsumer(ctx context.Context, _ reliable.DurableSettings, _ reliable.DurableConsumerSettings) (reliable.Subscription, error) {
|
||||
t.ctx = ctx
|
||||
return t.Subscription, nil
|
||||
}
|
||||
|
||||
type consumerAdmin struct {
|
||||
reliable.Admin
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (a *consumerAdmin) EnsureConsumerResources(ctx context.Context, _ reliable.DurableSettings, _ reliable.DurableConsumerSettings) error {
|
||||
a.ctx = ctx
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSubscriptionOutlivesProvisioning(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
transport := &consumerTransport{}
|
||||
admin := &consumerAdmin{}
|
||||
if _, err := New(ctx, transport, admin, &outbox{}, nil, zap.NewNop(), DefaultConfig()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if admin.ctx.Err() != context.Canceled {
|
||||
t.Fatal("provisioning context was not released")
|
||||
}
|
||||
if _, ok := admin.ctx.Deadline(); !ok {
|
||||
t.Fatal("provisioning has no deadline")
|
||||
}
|
||||
if err := transport.ctx.Err(); err != nil {
|
||||
t.Fatalf("subscription ended with provisioning: %v", err)
|
||||
}
|
||||
cancel()
|
||||
if transport.ctx.Err() != context.Canceled {
|
||||
t.Fatal("subscription does not follow service shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *publisher) Publish(_ context.Context, r reliable.Record) error {
|
||||
if p.fail {
|
||||
return merrors.ServiceUnavailable("offline")
|
||||
|
||||
Reference in new issue
Block a user