Skip to content

Commit

Permalink
v0.1.7: All scheduled tasks are now executed in Go routines with rand…
Browse files Browse the repository at this point in the history
…om delay on API heavy tasks
  • Loading branch information
wneessen committed Aug 26, 2022
1 parent 19ed9d7 commit 2913611
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
54 changes: 31 additions & 23 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,12 @@ func (b *Bot) Run() error {
signal.Notify(sc)

// Timer events
var rn int
rn = int(b.Config.Timer.FHSpam)
rn, err = crypto.RandNum(int(b.Config.Timer.FHSpam))
rd, err := crypto.RandDuration(b.Config.Timer.FHSpam, "m")
if err != nil {
ll.Warn().Msgf("failed to generate random number for FH timer: %s", err)
rn = int(b.Config.Timer.FHSpam)
rd = time.Minute * time.Duration(b.Config.Timer.FHSpam)
}
fht := time.NewTicker(time.Duration(int64(rn)+b.Config.Timer.FHSpam) * time.Minute)
fht := time.NewTicker(rd)
defer fht.Stop()
trt := time.NewTicker(b.Config.Timer.TRUpdate)
defer trt.Stop()
Expand Down Expand Up @@ -170,33 +168,43 @@ func (b *Bot) Run() error {
return nil
}
case <-fht.C:
if err := b.ScheduledEventSoTFlameheart(); err != nil {
ll.Error().Msgf("failed to process scheuled flameheart event: %s", err)
}
go func() {
if err := b.ScheduledEventSoTFlameheart(); err != nil {
ll.Error().Msgf("failed to process scheuled flameheart event: %s", err)
}
}()

// Reset the duration
rn, err = crypto.RandNum(int(b.Config.Timer.FHSpam))
rd, err := crypto.RandDuration(b.Config.Timer.FHSpam, "m")
if err != nil {
ll.Warn().Msgf("failed to generate random number for FH timer: %s", err)
rn = int(b.Config.Timer.FHSpam)
rd = time.Minute * time.Duration(b.Config.Timer.FHSpam)
}
fht.Reset(time.Duration(int64(rn)+b.Config.Timer.FHSpam) * time.Minute)
fht.Reset(rd)
case <-trt.C:
if err := b.ScheduledEventUpdateTradeRoutes(); err != nil {
ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err)
}
go func() {
if err := b.ScheduledEventUpdateTradeRoutes(); err != nil {
ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err)
}
}()
case <-ust.C:
if err := b.ScheduledEventUpdateUserStats(); err != nil {
ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err)
}
go func() {
if err := b.ScheduledEventUpdateUserStats(); err != nil {
ll.Error().Msgf("failed to process scheuled traderoute update event: %s", err)
}
}()
case <-rct.C:
if err := b.ScheduledEventCheckRATCookies(); err != nil {
ll.Error().Msgf("failed to process scheuled RAT cookie check event: %s", err)
}
go func() {
if err := b.ScheduledEventCheckRATCookies(); err != nil {
ll.Error().Msgf("failed to process scheuled RAT cookie check event: %s", err)
}
}()
case <-ddt.C:
if err := b.ScheduledEventUpdateDailyDeeds(); err != nil {
ll.Error().Msgf("failed to process scheuled daily deeds update event: %s", err)
}
go func() {
if err := b.ScheduledEventUpdateDailyDeeds(); err != nil {
ll.Error().Msgf("failed to process scheuled daily deeds update event: %s", err)
}
}()
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions bot/sc_handler_sot_userstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/wneessen/arrgo/crypto"
"github.com/wneessen/arrgo/model"
"golang.org/x/text/language"
"golang.org/x/text/message"
"net/http"
"time"
)

// SoTUserOverview represents the JSON structure of the Sea of Thieves user overview API response
Expand Down Expand Up @@ -226,6 +228,11 @@ func (b *Bot) ScheduledEventUpdateUserStats() error {
ll.Error().Msgf("failed to store user stats in DB: %s", err)
continue
}
rd, err := crypto.RandDuration(10, "s")
if err != nil {
rd = time.Second * 10
}
time.Sleep(rd)
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions bot/se_handler_checkratcookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bot
import (
"errors"
"fmt"
"github.com/wneessen/arrgo/crypto"
"github.com/wneessen/arrgo/model"
"net/http"
"time"
Expand Down Expand Up @@ -89,6 +90,11 @@ func (b *Bot) ScheduledEventCheckRATCookies() error {
}
}
}
rd, err := crypto.RandDuration(10, "s")
if err != nil {
rd = time.Second * 10
}
time.Sleep(rd)
}
return nil
}
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ type Config struct {
EncryptionKey string `fig:"enc_key"`
}
Timer struct {
FHSpam int64 `fig:"flameheart_spam" default:"60"`
FHSpam int `fig:"flameheart_spam" default:"60"`
TRUpdate time.Duration `fig:"traderoutes_update" default:"12h"`
USUpdate time.Duration `fig:"userstats_update" default:"6h"`
RCCheck time.Duration `fig:"ratcookie_check" default:"6h"`
DDUpdate time.Duration `fig:"dailydeed_update" default:"24h"`
ULUpdate time.Duration `fig:"userledger_update" default:"6h"`
}
confPath string
confFile string
Expand Down
15 changes: 15 additions & 0 deletions crypto/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"
"strings"
"time"
)

// Range of characters for the different types of string generations
Expand Down Expand Up @@ -89,6 +90,20 @@ func RandNum(n int) (int, error) {
return rn, nil
}

// RandDuration returns a random time.Duration in u with a maximum value of the given n
func RandDuration(n int, u string) (time.Duration, error) {
var d time.Duration
rn, err := RandNum(n)
if err != nil {
return d, err
}
d, err = time.ParseDuration(fmt.Sprintf("%d%s", rn, u))
if err != nil {
return d, err
}
return d, nil
}

// getCharRange returns the range of characters as controlled by the s and h bools
func getCharRange(s, h bool) string {
var cr string
Expand Down

0 comments on commit 2913611

Please sign in to comment.