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-24 22:17:17 -07:00

68 lines
1.2 KiB
Go

package ops
import (
"github.com/kcotugno/tacitus"
"github.com/kcotugno/tacitus/gdax"
"github.com/kcotugno/tacitus/gdax/websocket"
"github.com/kcotugno/tacitus/postgres"
)
type Registrar struct {
db *postgres.Client
wc *gdax.ListenerService
logger tacitus.Logger
}
func NewRegistrar() *Registrar {
r := Registrar{}
r.db = postgres.NewClient()
r.wc = &gdax.ListenerService{}
r.wc.Client = websocket.NewClient()
return &r
}
func (r *Registrar) Record(products ...string) error {
r.db.Name = "gdax"
r.db.User = "gdax"
if err := r.db.Open(); err != nil {
return err
}
if err := r.wc.Open(); err != nil {
return err
}
r.record()
r.wc.Subscribe(products...)
return nil
}
func (r *Registrar) SetLogger(logger tacitus.Logger) {
r.db.SetLogger(logger)
r.wc.Logger = logger
r.logger = logger
}
func (r *Registrar) record() {
go func () {
for t := range r.wc.Stream() {
go func (trade tacitus.Trade) {
_, err := r.db.TradeService().CreateTrade(trade)
if err != nil {
r.logger.Info("Error inserting trade: %v", err)
}
}(t)
}
}()
go func () {
for err := range r.wc.Error() {
r.logger.Info("Registrar received error: %v", err)
}
}()
}