Skip to content

Commit a5b81c0

Browse files
authored
Merge pull request #75 from partyoffice/feature/only-refresh-recently-updated-sessions
Only refresh recently updated sessions
2 parents 1af133c + a7d75c0 commit a5b81c0

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

listeningSession/listeningSession.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func RequestSong(session model.FullListeningSession, trackId string, username st
237237
return errors.New("rolling back transaction")
238238
}
239239

240-
queue, err := GetLimitedQueue(session.SimpleListeningSession, 3)
240+
queue, err := GetLimitedQueueInTransaction(session.SimpleListeningSession, 3, tx)
241241
if err != nil {
242242
return err
243243
}
@@ -313,6 +313,9 @@ func createNewSongRequestInTransaction(session model.FullListeningSession, track
313313
return model.SongRequest{}, NewInternalError("could not save new request", err)
314314
}
315315

316+
session.UpdatedAt = time.Now()
317+
tx.Save(session.SimpleListeningSession)
318+
316319
return newSongRequest, nil
317320
}
318321

@@ -342,6 +345,9 @@ func UpdateSessionIfNecessary(session model.FullListeningSession) *SpotifeteErro
342345
if err != nil {
343346
return NewInternalError("could not update session", err)
344347
}
348+
349+
session.UpdatedAt = time.Now()
350+
database.GetConnection().Save(session.SimpleListeningSession)
345351
}
346352

347353
return updatePlaylistIfNecessary(session, queue)

listeningSession/polling.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@ func pollSessionsLoop() {
1717
}
1818

1919
func pollSessions() {
20+
21+
sessionsToUpdate := findSessionsToUpdate()
22+
23+
for _, session := range sessionsToUpdate {
24+
UpdateSessionIfNecessary(session)
25+
}
26+
}
27+
28+
func findSessionsToUpdate() []model.FullListeningSession {
29+
2030
activeSessions := FindFullListeningSessions(model.SimpleListeningSession{
2131
Active: true,
2232
})
2333

24-
for _, session := range activeSessions {
25-
UpdateSessionIfNecessary(session)
34+
recentlyUpdatedThreshold := time.Now().Add(-1 * time.Hour)
35+
36+
var recentlyUpdatedSessions []model.FullListeningSession
37+
for _, s := range activeSessions {
38+
if s.UpdatedAt.After(recentlyUpdatedThreshold) {
39+
recentlyUpdatedSessions = append(recentlyUpdatedSessions, s)
40+
}
2641
}
42+
43+
return recentlyUpdatedSessions
2744
}

listeningSession/songRequest.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,34 @@ func FindSongRequestCountInTransaction(filter interface{}, tx *gorm.DB) (int64,
5252

5353
func GetFullQueue(session model.SimpleListeningSession) ([]model.SongRequest, error) {
5454

55-
query := buildGetQueueQuery(session)
55+
return GetFullQueueInTransaction(session, database.GetConnection())
56+
}
57+
58+
func GetFullQueueInTransaction(session model.SimpleListeningSession, tx *gorm.DB) ([]model.SongRequest, error) {
59+
60+
query := buildGetQueueQuery(session, tx)
5661
return FindSongRequests(query)
5762
}
5863

5964
func GetLimitedQueue(session model.SimpleListeningSession, limit int) ([]model.SongRequest, error) {
6065

61-
query := buildGetQueueQuery(session).Limit(limit)
66+
return GetLimitedQueueInTransaction(session, limit, database.GetConnection())
67+
}
68+
69+
func GetLimitedQueueInTransaction(session model.SimpleListeningSession, limit int, tx *gorm.DB) ([]model.SongRequest, error) {
70+
71+
query := buildGetQueueQuery(session, tx).Limit(limit)
6272
return FindSongRequests(query)
6373
}
6474

65-
func buildGetQueueQuery(session model.SimpleListeningSession) *gorm.DB {
75+
func buildGetQueueQuery(session model.SimpleListeningSession, tx *gorm.DB) *gorm.DB {
6676

6777
filter := map[string]interface{}{
6878
"session_id": session.ID,
6979
"played": false,
7080
}
7181

72-
return database.GetConnection().Where(filter).Order("locked desc, weight asc, created_at asc")
82+
return tx.Where(filter).Order("locked desc, weight asc, created_at asc")
7383
}
7484

7585
func GetQueueLastUpdated(session model.SimpleListeningSession) time.Time {

0 commit comments

Comments
 (0)