From 745ef9ee9b866d7b9d9989a307561b1004b2885a Mon Sep 17 00:00:00 2001 From: ehlxr Date: Thu, 12 Dec 2019 17:57:34 +0800 Subject: [PATCH] add send message aggregation --- main.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 94a109a..59c56b9 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,18 @@ package main import ( + "bytes" "fmt" - "github.com/ehlxr/monitor/pkg" - "github.com/hpcloud/tail" "strings" "time" + + "github.com/ehlxr/monitor/pkg" + "github.com/hpcloud/tail" log "unknwon.dev/clog/v2" - dt "github.com/JetBlink/dingtalk-notify-go-sdk" "regexp" + + dt "github.com/JetBlink/dingtalk-notify-go-sdk" ) var ( @@ -66,6 +69,19 @@ func tailFile() { pkg.Opts.KeyWord, pkg.Opts.KeyWordIgnoreCase) + var buffer bytes.Buffer + go func() { + ticker := time.NewTicker(10 * time.Second) + for { + <-ticker.C + log.Info("will send msg to dingtalk...") + if buffer.Len() > 0 { + sendMsg(buffer.String()) + buffer.Reset() + } + } + }() + for line := range tf.Lines { text := line.Text if pkg.Opts.KeyWordIgnoreCase { @@ -75,12 +91,16 @@ func tailFile() { keys := strings.Split(pkg.Opts.KeyWord, ",") for _, key := range keys { if ok, _ := regexp.Match(strings.TrimSpace(key), []byte(text)); ok { - if limiter.IsAvailable() { - sendMsg(line.Text) - } else { - log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.", - line.Text) - } + //if limiter.IsAvailable() { + // sendMsg(line.Text) + //} else { + // log.Error("dingTalk 1 m allow send 20 msg. msg %v discarded.", + // line.Text) + //} + + buffer.WriteString(line.Text) + buffer.WriteByte('\n') + break } }