fix(matrix): Stop Sync Go routine and correct logs (#154)

Signed-off-by: Richard Palethorpe <io@richiejp.com>
This commit is contained in:
Richard Palethorpe
2025-05-13 12:10:23 +01:00
committed by GitHub
parent c23e655f44
commit 1a53d24890

View File

@@ -3,6 +3,7 @@ package connectors
import ( import (
"context" "context"
"fmt" "fmt"
"slices"
"sync" "sync"
"time" "time"
@@ -114,7 +115,7 @@ func (m *Matrix) cancelActiveJobForRoom(roomID string) {
func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) { func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
if m.roomID != evt.RoomID.String() && m.roomMode { // If we have a roomID and it's not the same as the event room if m.roomID != evt.RoomID.String() && m.roomMode { // If we have a roomID and it's not the same as the event room
// Skip messages from other rooms // Skip messages from other rooms
xlog.Info("Skipping reply to room", evt.RoomID, m.roomID) xlog.Info("Skipping reply to room", "event room", evt.RoomID, "config room", m.roomID)
return return
} }
@@ -126,16 +127,11 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
// Skip if message does not mention the bot // Skip if message does not mention the bot
mentioned := false mentioned := false
if evt.Content.AsMessage().Mentions != nil { if evt.Content.AsMessage().Mentions != nil {
for _, mention := range evt.Content.AsMessage().Mentions.UserIDs { mentioned = slices.Contains(evt.Content.AsMessage().Mentions.UserIDs, m.client.UserID)
if mention == m.client.UserID {
mentioned = true
break
}
}
} }
if !mentioned && !m.roomMode { if !mentioned && !m.roomMode {
xlog.Info("Skipping reply because it does not mention the bot", evt.RoomID, m.roomID) xlog.Info("Skipping reply because it does not mention the bot", "mentions", evt.Content.AsMessage().Mentions.UserIDs)
return return
} }
@@ -181,7 +177,7 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
job.Cancel() job.Cancel()
for i, j := range m.activeJobs[evt.RoomID.String()] { for i, j := range m.activeJobs[evt.RoomID.String()] {
if j.UUID == job.UUID { if j.UUID == job.UUID {
m.activeJobs[evt.RoomID.String()] = append(m.activeJobs[evt.RoomID.String()][:i], m.activeJobs[evt.RoomID.String()][i+1:]...) m.activeJobs[evt.RoomID.String()] = slices.Delete(m.activeJobs[evt.RoomID.String()], i, i+1)
break break
} }
} }
@@ -252,21 +248,21 @@ func (m *Matrix) Start(a *agent.Agent) {
// Start syncing // Start syncing
go func() { go func() {
for { for {
err := client.SyncWithContext(a.Context()) select {
case <-a.Context().Done():
xlog.Info("Context cancelled, stopping sync loop")
return
default:
err := client.SyncWithContext(a.Context())
xlog.Info("Syncing") xlog.Info("Syncing")
if err != nil { if err != nil {
xlog.Error(fmt.Sprintf("Error syncing: %v", err)) xlog.Error(fmt.Sprintf("Error syncing: %v", err))
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
}
} }
} }
}() }()
// Handle shutdown
go func() {
<-a.Context().Done()
client.StopSync()
}()
} }
// MatrixConfigMeta returns the metadata for Matrix connector configuration fields // MatrixConfigMeta returns the metadata for Matrix connector configuration fields