Skip to content

Commit 5daf98c

Browse files
committed
fix(matrix): Stop Sync Go routine and correct logs
Signed-off-by: Richard Palethorpe <[email protected]>
1 parent c23e655 commit 5daf98c

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

services/connectors/matrix.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connectors
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"sync"
78
"time"
89

@@ -114,7 +115,7 @@ func (m *Matrix) cancelActiveJobForRoom(roomID string) {
114115
func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
115116
if m.roomID != evt.RoomID.String() && m.roomMode { // If we have a roomID and it's not the same as the event room
116117
// Skip messages from other rooms
117-
xlog.Info("Skipping reply to room", evt.RoomID, m.roomID)
118+
xlog.Info("Skipping reply to room", "event room", evt.RoomID, "config room", m.roomID)
118119
return
119120
}
120121

@@ -126,16 +127,11 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
126127
// Skip if message does not mention the bot
127128
mentioned := false
128129
if evt.Content.AsMessage().Mentions != nil {
129-
for _, mention := range evt.Content.AsMessage().Mentions.UserIDs {
130-
if mention == m.client.UserID {
131-
mentioned = true
132-
break
133-
}
134-
}
130+
mentioned = slices.Contains(evt.Content.AsMessage().Mentions.UserIDs, m.client.UserID)
135131
}
136132

137133
if !mentioned && !m.roomMode {
138-
xlog.Info("Skipping reply because it does not mention the bot", evt.RoomID, m.roomID)
134+
xlog.Info("Skipping reply because it does not mention the bot", "mentions", evt.Content.AsMessage().Mentions.UserIDs)
139135
return
140136
}
141137

@@ -181,7 +177,7 @@ func (m *Matrix) handleRoomMessage(a *agent.Agent, evt *event.Event) {
181177
job.Cancel()
182178
for i, j := range m.activeJobs[evt.RoomID.String()] {
183179
if j.UUID == job.UUID {
184-
m.activeJobs[evt.RoomID.String()] = append(m.activeJobs[evt.RoomID.String()][:i], m.activeJobs[evt.RoomID.String()][i+1:]...)
180+
m.activeJobs[evt.RoomID.String()] = slices.Delete(m.activeJobs[evt.RoomID.String()], i, i+1)
185181
break
186182
}
187183
}
@@ -252,21 +248,21 @@ func (m *Matrix) Start(a *agent.Agent) {
252248
// Start syncing
253249
go func() {
254250
for {
255-
err := client.SyncWithContext(a.Context())
256-
257-
xlog.Info("Syncing")
258-
if err != nil {
259-
xlog.Error(fmt.Sprintf("Error syncing: %v", err))
260-
time.Sleep(5 * time.Second)
251+
select {
252+
case <-a.Context().Done():
253+
xlog.Info("Context cancelled, stopping sync loop")
254+
return
255+
default:
256+
err := client.SyncWithContext(a.Context())
257+
258+
xlog.Info("Syncing")
259+
if err != nil {
260+
xlog.Error(fmt.Sprintf("Error syncing: %v", err))
261+
time.Sleep(5 * time.Second)
262+
}
261263
}
262264
}
263265
}()
264-
265-
// Handle shutdown
266-
go func() {
267-
<-a.Context().Done()
268-
client.StopSync()
269-
}()
270266
}
271267

272268
// MatrixConfigMeta returns the metadata for Matrix connector configuration fields

0 commit comments

Comments
 (0)