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
2017-09-27 20:37:34 -07:00

49 lines
887 B
Go

package ops
import (
"github.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.Database.SetLogger(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)
}
}()
}