configure notifier and make slack notification optional

This commit is contained in:
c9s 2020-10-26 13:40:43 +08:00
parent ac0a26b005
commit 931c646fde
2 changed files with 22 additions and 10 deletions

View File

@ -23,14 +23,22 @@ func (m *Notifiability) AddNotifier(notifier Notifier) {
m.notifiers = append(m.notifiers, notifier) m.notifiers = append(m.notifiers, notifier)
} }
func (m *Notifiability) Notify(msg string, args ...interface{}) { func (m *Notifiability) Notify(msg string, args ...interface{}) (err error) {
for _, n := range m.notifiers { for _, n := range m.notifiers {
n.Notify(msg, args...) if err2 := n.Notify(msg, args...); err2 != nil {
err = err2
}
} }
return err
} }
func (m *Notifiability) NotifyTo(channel, msg string, args ...interface{}) { func (m *Notifiability) NotifyTo(channel, msg string, args ...interface{}) (err error) {
for _, n := range m.notifiers { for _, n := range m.notifiers {
n.NotifyTo(channel, msg, args...) if err2 := n.NotifyTo(channel, msg, args...); err2 != nil {
err = err2
}
} }
return err
} }

View File

@ -67,14 +67,17 @@ func compileRunFile(filepath string, config *config.Config) error {
} }
func runConfig(ctx context.Context, config *config.Config) error { func runConfig(ctx context.Context, config *config.Config) error {
// configure notifiers
slackToken := viper.GetString("slack-token") slackToken := viper.GetString("slack-token")
if len(slackToken) == 0 { if len(slackToken) > 0 {
return errSlackTokenUndefined log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel")))
} }
log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel"))) notifierSet := &bbgo.Notifiability{}
if len(slackToken) > 0 {
var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel")) var notifier = slacknotifier.New(slackToken, viper.GetString("slack-channel"))
notifierSet.AddNotifier(notifier)
}
db, err := cmdutil.ConnectMySQL() db, err := cmdutil.ConnectMySQL()
if err != nil { if err != nil {
@ -82,9 +85,10 @@ func runConfig(ctx context.Context, config *config.Config) error {
} }
environ := bbgo.NewDefaultEnvironment(db) environ := bbgo.NewDefaultEnvironment(db)
environ.ReportTrade(notifier) environ.ReportTrade(notifierSet)
trader := bbgo.NewTrader(environ) trader := bbgo.NewTrader(environ)
trader.AddNotifier(notifierSet)
for _, entry := range config.ExchangeStrategies { for _, entry := range config.ExchangeStrategies {
for _, mount := range entry.Mounts { for _, mount := range entry.Mounts {