mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-19 04:56:46 +01:00
Mohammed Sohail
1ddff06502
* BREAKING: remove events interface -> use jetstream struct * in-built chain subscription to update otx status * potential fix for tasks: defer rollback nonce error detection
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package events
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
const (
|
|
backOffTimer = 2 * time.Second
|
|
durableId = "cic-custodial"
|
|
pullStream = "CHAIN"
|
|
pullSubject = "CHAIN.*"
|
|
)
|
|
|
|
func (js *JetStream) ChainSubscription(ctx context.Context, store store.Store) error {
|
|
subOpts := []nats.SubOpt{
|
|
nats.ManualAck(),
|
|
nats.Bind(pullStream, durableId),
|
|
}
|
|
|
|
natsSub, err := js.jsCtx.PullSubscribe(pullSubject, durableId, subOpts...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
js.logg.Info("jetstream chain sub: shutdown signal received")
|
|
return nil
|
|
default:
|
|
events, err := natsSub.Fetch(1)
|
|
if err != nil {
|
|
if errors.Is(err, nats.ErrTimeout) {
|
|
// Supressed retry
|
|
js.logg.Error("jetstream chain sub: fetch NATS timeout", "error", err)
|
|
time.Sleep(backOffTimer)
|
|
continue
|
|
} else {
|
|
js.logg.Error("jetstream chain sub: fetch other error", "error", err)
|
|
}
|
|
}
|
|
if len(events) > 0 {
|
|
// TODO: Unmarshal
|
|
// TODO: UpdateOtxStatus
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|