48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
|
|
gatewayoutbox "github.com/tech/sendico/gateway/common/outbox"
|
|
"github.com/tech/sendico/pkg/db/transaction"
|
|
me "github.com/tech/sendico/pkg/messaging/envelope"
|
|
)
|
|
|
|
type chainOutboxProvider interface {
|
|
Outbox() gatewayoutbox.Store
|
|
}
|
|
|
|
type chainTransactionProvider interface {
|
|
TransactionFactory() transaction.Factory
|
|
}
|
|
|
|
func (s *Service) outboxStore() gatewayoutbox.Store {
|
|
provider, ok := s.storage.(chainOutboxProvider)
|
|
if !ok || provider == nil {
|
|
return nil
|
|
}
|
|
return provider.Outbox()
|
|
}
|
|
|
|
func (s *Service) startOutboxReliableProducer() error {
|
|
if s == nil || s.storage == nil {
|
|
return nil
|
|
}
|
|
return s.outbox.Start(s.logger, s.producer, s.outboxStore(), s.msgCfg)
|
|
}
|
|
|
|
func (s *Service) sendWithOutbox(ctx context.Context, env me.Envelope) error {
|
|
if err := s.startOutboxReliableProducer(); err != nil {
|
|
return err
|
|
}
|
|
return s.outbox.Send(ctx, env)
|
|
}
|
|
|
|
func (s *Service) executeTransaction(ctx context.Context, cb transaction.Callback) (any, error) {
|
|
provider, ok := s.storage.(chainTransactionProvider)
|
|
if !ok || provider == nil || provider.TransactionFactory() == nil {
|
|
return cb(ctx)
|
|
}
|
|
return provider.TransactionFactory().CreateTransaction().Execute(ctx, cb)
|
|
}
|