mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-25 13:17:42 +00:00
try refreshing token on mailbox error
This commit is contained in:
parent
9a37a05bf0
commit
d2909b8003
1 changed files with 25 additions and 15 deletions
|
@ -186,6 +186,24 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
|
|||
nc.statsd.Histogram("apollo.queue.delay", latency, []string{}, rate)
|
||||
}
|
||||
|
||||
if err = nc.db.BeginFunc(ctx, func(tx pgx.Tx) error {
|
||||
stmt := `
|
||||
UPDATE accounts
|
||||
SET last_checked_at = $1
|
||||
WHERE id = $2`
|
||||
_, err := tx.Exec(ctx, stmt, now, account.ID)
|
||||
return err
|
||||
}); err != nil {
|
||||
nc.logger.WithFields(logrus.Fields{
|
||||
"accountID": id,
|
||||
"err": err,
|
||||
}).Error("failed to update last_checked_at for account")
|
||||
return
|
||||
}
|
||||
|
||||
retried := false
|
||||
|
||||
refreshToken:
|
||||
rac := nc.reddit.NewAuthenticatedClient(account.RefreshToken, account.AccessToken)
|
||||
if account.ExpiresAt < int64(now) {
|
||||
nc.logger.WithFields(logrus.Fields{
|
||||
|
@ -226,6 +244,13 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
|
|||
msgs, err := rac.MessageInbox(account.LastMessageID)
|
||||
|
||||
if err != nil {
|
||||
// Check if maybe we need to refresh the token.
|
||||
if !retried {
|
||||
retried = true
|
||||
account.ExpiresAt = 0
|
||||
goto refreshToken
|
||||
}
|
||||
|
||||
nc.logger.WithFields(logrus.Fields{
|
||||
"accountID": id,
|
||||
"err": err,
|
||||
|
@ -238,21 +263,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) {
|
|||
"count": len(msgs.MessageListing.Messages),
|
||||
}).Debug("fetched messages")
|
||||
|
||||
if err = nc.db.BeginFunc(ctx, func(tx pgx.Tx) error {
|
||||
stmt := `
|
||||
UPDATE accounts
|
||||
SET last_checked_at = $1
|
||||
WHERE id = $2`
|
||||
_, err := tx.Exec(ctx, stmt, now, account.ID)
|
||||
return err
|
||||
}); err != nil {
|
||||
nc.logger.WithFields(logrus.Fields{
|
||||
"accountID": id,
|
||||
"err": err,
|
||||
}).Error("failed to update last_checked_at for account")
|
||||
return
|
||||
}
|
||||
|
||||
if len(msgs.MessageListing.Messages) == 0 {
|
||||
nc.logger.WithFields(logrus.Fields{
|
||||
"accountID": id,
|
||||
|
|
Loading…
Reference in a new issue