From 26d2bc98f1efab45fa6b0e0a152a92014c1dcc88 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 7 Jul 2023 17:31:46 -0700 Subject: [PATCH] Minor quick fixes --- pkg/models/thread.go | 5 +++++ pkg/worker/barertc.go | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/models/thread.go b/pkg/models/thread.go index 93d804f..9d8c3c9 100644 --- a/pkg/models/thread.go +++ b/pkg/models/thread.go @@ -210,6 +210,11 @@ func (t *Thread) Save() error { // Delete a thread and all of its comments. func (t *Thread) Delete() error { + // Unlink the parent comment from the thread to resolve a foreign key constraint in Postgres. + if result := DB.Model(&Thread{}).Where("id = ?", t.ID).Update("comment_id", nil); result.Error != nil { + return fmt.Errorf("Thread.Delete: couldn't unlink parent comment: %s", result.Error) + } + // Remove all comments. result := DB.Where( "table_name = ? AND table_id = ?", diff --git a/pkg/worker/barertc.go b/pkg/worker/barertc.go index 051b517..04cb672 100644 --- a/pkg/worker/barertc.go +++ b/pkg/worker/barertc.go @@ -34,6 +34,13 @@ func GetChatStatistics() ChatStatistics { } } +// SetChatStatistics updates the cached chat statistics, holding a write lock briefly. +func SetChatStatistics(stats *ChatStatistics) { + chatStatisticsMu.Lock() + defer chatStatisticsMu.Unlock() + cachedChatStatistics = stats +} + var ( cachedChatStatistics *ChatStatistics chatStatisticsMu sync.RWMutex @@ -61,13 +68,9 @@ func DoCheckBareRTC() { log.Info("Refresh BareRTC") req, err := http.NewRequest(http.MethodGet, config.Current.BareRTC.URL+"/api/statistics", nil) - // Lock the cached statistics. - chatStatisticsMu.Lock() - defer chatStatisticsMu.Unlock() - if err != nil { log.Error("WatchBareRTC: couldn't make request: %s", err) - cachedChatStatistics = nil + SetChatStatistics(nil) return } @@ -78,11 +81,11 @@ func DoCheckBareRTC() { res, err := client.Do(req) if err != nil { log.Error("WatchBareRTC: request error: %s", err) - cachedChatStatistics = nil + SetChatStatistics(nil) return } else if res.StatusCode != http.StatusOK { log.Error("WatchBareRTC: didn't get expected 200 OK from statistics endpoint, instead got: %s", res.Status) - cachedChatStatistics = nil + SetChatStatistics(nil) return } @@ -95,6 +98,6 @@ func DoCheckBareRTC() { return } - cachedChatStatistics = &cs + SetChatStatistics(&cs) } }