Files
gocryptotrader/engine/comms_relayer.go
Scott 12263997c0 Bugfix: Submit/Cancel order scripting errors + Ordermanager GetOrder & CancelOrder improvements (#584)
* Fixes Submit and Cancel order functions for scripting. Adds tests to ensure that wrapper functions actually execute as expected. Adds more coverage. Fixes a few other tests

* Fixes issues

* Simplifies tests

* Update cancel order to properly cancel an order. Adds example script. Adds sweet tests.

* Fixes bad test setup for cancelling orders

* Adds new order manager GetOrderInfo function to call the wrapper function and then store it in the order manager

* Addresses order concerns

* LOWERS THE CASE OF ALL EXCHANGE NAMES BECAUSE ORDER MANAGER NEVER CARED ABOUT CASING OR YOUR FAMILY

* Removes asset and currency requirement from cancelling orders via validation test

* Fixes old cancel order assumptions that had requirements for currency and asset

* Moves all the logs and events to the dedicated Cancel function instead of exclusively doing it in CancelAllOrders

* Adds more detail logging. Fixes lbank fee test

* Addresses comms manager issues

* Removes go routine for pushing events. Better to let them fail
2020-10-27 17:05:43 +11:00

99 lines
2.3 KiB
Go

package engine
import (
"errors"
"sync/atomic"
"github.com/thrasher-corp/gocryptotrader/communications"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/log"
)
// commsManager starts the NTP manager
type commsManager struct {
started int32
stopped int32
shutdown chan struct{}
relayMsg chan base.Event
comms *communications.Communications
}
func (c *commsManager) Started() bool {
return atomic.LoadInt32(&c.started) == 1
}
func (c *commsManager) Start() (err error) {
if atomic.AddInt32(&c.started, 1) != 1 {
return errors.New("communications manager already started")
}
defer func() {
if err != nil {
atomic.CompareAndSwapInt32(&c.started, 1, 0)
}
}()
log.Debugln(log.CommunicationMgr, "Communications manager starting...")
commsCfg := Bot.Config.GetCommunicationsConfig()
c.comms, err = communications.NewComm(&commsCfg)
if err != nil {
return err
}
c.shutdown = make(chan struct{})
c.relayMsg = make(chan base.Event)
go c.run()
log.Debugln(log.CommunicationMgr, "Communications manager started.")
return nil
}
func (c *commsManager) GetStatus() (map[string]base.CommsStatus, error) {
if !c.Started() {
return nil, errors.New("communications manager not started")
}
return c.comms.GetStatus(), nil
}
func (c *commsManager) Stop() error {
if atomic.LoadInt32(&c.started) == 0 {
return errors.New("communications manager not started")
}
if atomic.AddInt32(&c.stopped, 1) != 1 {
return errors.New("communications manager is already stopped")
}
close(c.shutdown)
log.Debugln(log.CommunicationMgr, "Communications manager shutting down...")
return nil
}
func (c *commsManager) PushEvent(evt base.Event) {
if !c.Started() {
return
}
select {
case c.relayMsg <- evt:
default:
log.Errorf(log.CommunicationMgr, "Failed to send, no receiver when pushing event [%v]", evt)
}
}
func (c *commsManager) run() {
defer func() {
// TO-DO shutdown comms connections for connected services (Slack etc)
atomic.CompareAndSwapInt32(&c.stopped, 1, 0)
atomic.CompareAndSwapInt32(&c.started, 1, 0)
log.Debugln(log.CommunicationMgr, "Communications manager shutdown.")
}()
for {
select {
case msg := <-c.relayMsg:
c.comms.PushEvent(msg)
case <-c.shutdown:
return
}
}
}