status-go/protocol/messenger_auto_message.go
frank 38308d48f2
feat_: log on panic (#5849)
* feat_: log error and stacktrace when panic in goroutine

* test_: add test TestSafeGo

* chore_: rename logAndCall to call

* chore_: rename SafeGo to Go

* chore_: make lint-fix

* chore_: use t.Cleanup

* chore_: Revert "chore_: use t.Cleanup"

This reverts commit 4eb420d179cc0e208e84c13cb941e6b3d1ed9819.

* chore_: Revert "chore_: make lint-fix"

This reverts commit fcc995f157e671a4229b47419c3a0e4004b5fdab.

* chore_: Revert "chore_: rename SafeGo to Go"

This reverts commit a6d73d6df583f313032d79aac62f66328039cb55.

* chore_: Revert "chore_: rename logAndCall to call"

This reverts commit 8fbe993bedb9fbba67349a44f151e2dd5e3bc4cc.

* chore_: Revert "test_: add test TestSafeGo"

This reverts commit a1fa91839f3960398980c6bf456e6462ec944819.

* chore_: Revert "feat_: log error and stacktrace when panic in goroutine"

This reverts commit f612dd828fa2ce410d0e806fe773ecbe3e86a68a.

* feat_: log error and stacktrace when panic in goroutine

* chore_: make lint-fix

* chore_: rename logAndCall to call

* chore_: renaming LogOnPanic

* chore_: update rest goroutine function calls

* chore_: make lint-fix
2024-09-27 06:37:32 +08:00

78 lines
1.9 KiB
Go

package protocol
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
gocommon "github.com/status-im/status-go/common"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/signal"
)
// autoMessageInterval is how often we should send a message
const autoMessageInterval = 120 * time.Second
const autoMessageChatID = "status-bot"
func (m *Messenger) AutoMessageEnabled() (bool, error) {
return m.settings.AutoMessageEnabled()
}
func (m *Messenger) startAutoMessageLoop() error {
enabled, err := m.AutoMessageEnabled()
if err != nil {
m.logger.Error("[auto message] failed to start auto message loop", zap.Error(err))
return err
}
if !enabled {
return nil
}
m.logger.Info("[auto message] starting auto message loop")
ticker := time.NewTicker(autoMessageInterval)
count := 0
go func() {
defer gocommon.LogOnPanic()
for {
select {
case <-ticker.C:
count++
timestamp := time.Now().Format(time.RFC3339)
msg := common.NewMessage()
msg.Text = fmt.Sprintf("%d\n%s", count, timestamp)
msg.ChatId = autoMessageChatID
msg.LocalChatID = autoMessageChatID
msg.ContentType = protobuf.ChatMessage_TEXT_PLAIN
resp, err := m.SendChatMessage(context.Background(), msg)
if err != nil {
m.logger.Error("[auto message] failed to send message", zap.Error(err))
continue
}
signal.SendNewMessages(resp)
err = m.UpdateMessageOutgoingStatus(msg.ID, common.OutgoingStatusDelivered)
if err != nil {
m.logger.Error("[auto message] failed to mark message as delivered", zap.Error(err))
continue
}
//send signal to client that message status updated
if m.config.messengerSignalsHandler != nil {
m.config.messengerSignalsHandler.MessageDelivered(autoMessageChatID, msg.ID)
}
case <-m.quit:
ticker.Stop()
return
}
}
}()
return nil
}