This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/ops/registrar.go

48 lines
867 B
Go

package ops
import (
"git.kevincotugno.com/kcotugno/tacitus"
)
type Registrar struct {
Database tacitus.DatabaseClientService
Listener tacitus.ListenerService
logger tacitus.Logger
}
func (r *Registrar) Record(products ...string) error {
if err := r.Listener.Open(); err != nil {
return err
}
r.record()
r.Listener.Subscribe(products...)
return nil
}
func (r *Registrar) SetLogger(logger tacitus.Logger) {
r.Listener.SetLogger(logger)
r.logger = logger
}
func (r *Registrar) record() {
go func() {
for t := range r.Listener.Stream() {
go func(trade tacitus.Trade) {
_, err := r.Database.TradeService().CreateTrade(trade)
if err != nil {
r.logger.Info("Error inserting trade: %v", err)
}
}(t)
}
}()
go func() {
for err := range r.Listener.Error() {
r.logger.Info("Registrar received error: %v", err)
}
}()
}