Skip to content

Commit

Permalink
add a check for last successful fetch for patreon, so that in case pa…
Browse files Browse the repository at this point in the history
…treon API fails patreon slots are not removed (botlabs-gg#1750)

Co-authored-by: Ashish <ashishjh-bst@users.noreply.github.com>
  • Loading branch information
ashishjh-bst and ashishjh-bst authored Nov 1, 2024
1 parent d137547 commit 85ca471
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
27 changes: 21 additions & 6 deletions common/patreon/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ import (
var logger = common.GetFixedPrefixLogger("patreon")

type Poller struct {
mu sync.RWMutex
config *oauth2.Config
token *oauth2.Token
client *patreonapi.Client

activePatrons []*Patron
mu sync.RWMutex
config *oauth2.Config
token *oauth2.Token
client *patreonapi.Client
lastSuccesfulFetchAt time.Time
isLastFetchSuccess bool
activePatrons []*Patron
}

var (
Expand Down Expand Up @@ -118,11 +119,22 @@ func (p *Poller) Run() {
}
}

func (p *Poller) IsLastFetchSuccess() bool {
if p.activePatrons == nil || len(p.activePatrons) < 1 {
return false
}
if time.Since(p.lastSuccesfulFetchAt) < time.Minute*5 {
return p.isLastFetchSuccess
}
return false
}

func (p *Poller) Poll() {
// Get your campaign data
campaignResponse, err := p.client.FetchCampaigns()
if err != nil || len(campaignResponse.Data) < 1 {
logger.WithError(err).Error("Failed fetching campaign")
p.isLastFetchSuccess = false
return
}

Expand All @@ -141,6 +153,7 @@ func (p *Poller) Poll() {

if err != nil {
logger.WithError(err).Error("Failed fetching pledges")
p.isLastFetchSuccess = false
return
}

Expand Down Expand Up @@ -208,6 +221,8 @@ func (p *Poller) Poll() {
// Swap the stored ones, this dosent mutate the existing returned slices so we dont have to do any copying on each request woo
p.mu.Lock()
p.activePatrons = patrons
p.lastSuccesfulFetchAt = time.Now()
p.isLastFetchSuccess = true
p.mu.Unlock()
}

Expand Down
6 changes: 5 additions & 1 deletion premium/patreonpremiumsource/patreonpremiumsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func RunPoller() {

for {
<-ticker.C

if !patreon.ActivePoller.IsLastFetchSuccess() {
logger.Warn("Last fetch was not successful, skipping update")
continue
}
err := UpdatePremiumSlots(context.Background())
if err != nil {
logger.WithError(err).Error("Failed updating premium slots for patrons")
Expand All @@ -75,7 +80,6 @@ func UpdatePremiumSlots(ctx context.Context) error {
tx.Rollback()
return errors.WithMessage(err, "PremiumSlots")
}

patrons := patreon.ActivePoller.GetPatrons()
if len(patrons) == 0 {
tx.Rollback()
Expand Down

0 comments on commit 85ca471

Please sign in to comment.