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
857 B
Go
Raw Normal View History

2017-09-24 22:17:17 -07:00
package ops
import (
"github.com/kcotugno/tacitus"
)
type Registrar struct {
2017-09-27 20:37:34 -07:00
Database tacitus.DatabaseClientService
Listener tacitus.ListenerService
2017-09-24 22:17:17 -07:00
logger tacitus.Logger
}
func (r *Registrar) Record(products ...string) error {
2017-09-27 20:37:34 -07:00
if err := r.Listener.Open(); err != nil {
2017-09-24 22:17:17 -07:00
return err
}
r.record()
2017-09-27 20:37:34 -07:00
r.Listener.Subscribe(products...)
2017-09-24 22:17:17 -07:00
return nil
}
func (r *Registrar) SetLogger(logger tacitus.Logger) {
2017-09-27 20:37:34 -07:00
r.Listener.SetLogger(logger)
2017-09-24 22:17:17 -07:00
r.logger = logger
}
func (r *Registrar) record() {
2017-09-24 22:23:13 -07:00
go func() {
2017-09-27 20:37:34 -07:00
for t := range r.Listener.Stream() {
2017-09-24 22:23:13 -07:00
go func(trade tacitus.Trade) {
2017-09-27 20:37:34 -07:00
_, err := r.Database.TradeService().CreateTrade(trade)
2017-09-24 22:17:17 -07:00
if err != nil {
r.logger.Info("Error inserting trade: %v", err)
}
}(t)
}
}()
2017-09-24 22:23:13 -07:00
go func() {
2017-09-27 20:37:34 -07:00
for err := range r.Listener.Error() {
2017-09-24 22:17:17 -07:00
r.logger.Info("Registrar received error: %v", err)
}
}()
}