Skip to content

Commit c83e510

Browse files
committed
feat: retry feature is made
1 parent f2a7eb5 commit c83e510

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

app/forumer/run.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
"darkbot/app/forumer/forum_types"
88
"darkbot/app/settings/logus"
99
"darkbot/app/settings/types"
10+
"darkbot/app/settings/utils"
1011
"fmt"
1112
"strings"
13+
"sync"
1214
"time"
1315

1416
"github.com/bwmarrin/discordgo"
@@ -26,6 +28,7 @@ type Forumer struct {
2628
cache map[ThreadCacheKey]*forum_types.Post
2729
// Keeping track as list, to realize which ones are old ones to delete
2830
cache_keys []ThreadCacheKey
31+
cache_mu sync.Mutex
2932
}
3033

3134
type ThreadCacheKey string
@@ -73,6 +76,9 @@ func (v *Forumer) GetPost(thread *forum_types.LatestThread, new_post_callback fu
7376
post, err = v.post_requester.GetDetailedPost(thread)
7477
logus.CheckError(err, "failed get detailed post for thread=", logus.Thread(thread))
7578
new_post_callback(post)
79+
80+
v.cache_mu.Lock()
81+
defer v.cache_mu.Unlock()
7682
v.cache[thread_key] = post
7783
v.cache_keys = append(v.cache_keys, thread_key)
7884
}
@@ -208,10 +214,48 @@ func (v *Forumer) update() {
208214
}
209215
}
210216

217+
func (v *Forumer) RetryMsgs() {
218+
v.cache_mu.Lock()
219+
var copied_keys []ThreadCacheKey
220+
copied_keys = append(copied_keys, v.cache_keys...)
221+
utils.ReverseSlice(copied_keys)
222+
v.cache_mu.Unlock()
223+
224+
for _, cache_key := range copied_keys {
225+
v.cache_mu.Lock()
226+
old_post, ok := v.cache[cache_key]
227+
v.cache_mu.Unlock()
228+
229+
if !ok {
230+
continue
231+
}
232+
233+
channelIDs, _ := v.Channels.List()
234+
for _, channel := range channelIDs {
235+
msgs, err := v.Discorder.GetLatestMessages(channel)
236+
if logus.CheckError(err, "failed to get discord latest msgs") {
237+
continue
238+
}
239+
v.TrySendMsg(channel, old_post, msgs)
240+
}
241+
242+
time.Sleep(time.Second * 5)
243+
}
244+
}
245+
211246
func (v *Forumer) Run() {
247+
delay := time.Second * 10
248+
go func() {
249+
for {
250+
logus.Debug("retrying to send msgs")
251+
v.RetryMsgs()
252+
time.Sleep(delay)
253+
}
254+
}()
255+
212256
for {
213257
logus.Debug("trying new forumer cycle")
214258
v.update()
215-
time.Sleep(time.Second * 10)
259+
time.Sleep(delay)
216260
}
217261
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package utils
2+
3+
import "reflect"
4+
5+
func ReverseSlice(s interface{}) {
6+
size := reflect.ValueOf(s).Len()
7+
swap := reflect.Swapper(s)
8+
for i, j := 0, size-1; i < j; i, j = i+1, j-1 {
9+
swap(i, j)
10+
}
11+
}

0 commit comments

Comments
 (0)