make message handling more readable, log current goroutines

This commit is contained in:
gempir 2021-01-08 22:19:20 +01:00
parent abf1711530
commit 292a292c74
5 changed files with 115 additions and 92 deletions

View file

@ -10,7 +10,7 @@ import (
log "github.com/sirupsen/logrus"
)
func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
func (b *Bot) handlePrivateMessageCommands(message twitch.PrivateMessage) {
if contains(b.cfg.Admins, message.User.Name) {
if strings.HasPrefix(message.Message, "!justlog status") || strings.HasPrefix(message.Message, "!status") {
uptime := humanize.TimeSince(b.startTime)

View file

@ -127,97 +127,9 @@ func (b *Bot) newClient() *twitch.Client {
b.worker = append(b.worker, &worker{client, []string{}})
log.Infof("[bot] creating new twitch connection, new total: %d", len(b.worker))
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogPrivateMessageForUser(message.User, message)
if err != nil {
log.Error(err.Error())
}
}()
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogPrivateMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
b.handlePrivateMessage(message)
})
client.OnUserNoticeMessage(func(message twitch.UserNoticeMessage) {
log.Debug(message.Raw)
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForUser(message.User.ID, message)
if err != nil {
log.Error(err.Error())
}
}()
if _, ok := message.Tags["msg-param-recipient-id"]; ok {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message)
if err != nil {
log.Error(err.Error())
}
}()
}
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
})
client.OnClearChatMessage(func(message twitch.ClearChatMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogClearchatMessageForUser(message.TargetUserID, message)
if err != nil {
log.Error(err.Error())
}
}()
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogClearchatMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
})
client.OnPrivateMessage(b.handlePrivateMessage)
client.OnUserNoticeMessage(b.handleUserNotice)
client.OnClearChatMessage(b.handleClearChat)
return client
}
@ -227,3 +139,90 @@ func (b *Bot) initialJoins() {
b.Join(channel.Login)
}
}
func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogPrivateMessageForUser(message.User, message)
if err != nil {
log.Error(err.Error())
}
}()
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogPrivateMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
b.handlePrivateMessageCommands(message)
}
func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForUser(message.User.ID, message)
if err != nil {
log.Error(err.Error())
}
}()
if _, ok := message.Tags["msg-param-recipient-id"]; ok {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message)
if err != nil {
log.Error(err.Error())
}
}()
}
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogUserNoticeMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
}
func (b *Bot) handleClearChat(message twitch.ClearChatMessage) {
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogClearchatMessageForUser(message.TargetUserID, message)
if err != nil {
log.Error(err.Error())
}
}()
go func() {
if !b.shouldLog(message.Channel, message.GetType()) {
return
}
err := b.fileLogger.LogClearchatMessageForChannel(message)
if err != nil {
log.Error(err.Error())
}
}()
}

1
go.mod
View file

@ -10,5 +10,6 @@ require (
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
google.golang.org/appengine v1.6.7
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
)

11
go.sum
View file

@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gempir/go-twitch-irc/v2 v2.5.0 h1:aybXNoyDNQaa4vHhXb0UpIDmspqutQUmXIYUFsjgecU=
github.com/gempir/go-twitch-irc/v2 v2.5.0/go.mod h1:120d2SdlRYg8tRnZwsyNPeS+mWPn+YmNEzB7Bv/CDGE=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@ -23,8 +25,17 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

12
main.go
View file

@ -2,6 +2,8 @@ package main
import (
"flag"
"runtime"
"time"
"github.com/gempir/justlog/api"
"github.com/gempir/justlog/archiver"
@ -9,6 +11,7 @@ import (
"github.com/gempir/justlog/config"
"github.com/gempir/justlog/filelog"
"github.com/gempir/justlog/helix"
log "github.com/sirupsen/logrus"
)
func main() {
@ -32,5 +35,14 @@ func main() {
apiServer := api.NewServer(cfg, bot, &fileLogger, &helixClient, cfg.Channels)
go apiServer.Init()
go runtimeMonitoring()
bot.Connect()
}
func runtimeMonitoring() {
for {
log.Infof("Current amount of gorountines: %d", runtime.NumGoroutine())
time.Sleep(time.Minute)
}
}