From e489b10565829c41972e5271d5d56647e499c8a6 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 16 Jul 2022 13:58:24 -0500 Subject: [PATCH] Retry Spotify token refresh --- CHANGELOG.md | 2 ++ package.json | 1 + src/services/third-party.ts | 10 ++++++---- yarn.lock | 15 ++++++++++++++- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d12e55d..289d174 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- Retry refreshing Spotify access token if a request fails (should fix https://github.com/codetheweb/muse/issues/719) ## [2.1.0] - 2022-06-25 - `/loop` command that plays the current song on loop diff --git a/package.json b/package.json index 7ca16f3..cf30bd8 100644 --- a/package.json +++ b/package.json @@ -103,6 +103,7 @@ "p-event": "^5.0.1", "p-limit": "^4.0.0", "p-queue": "^7.2.0", + "p-retry": "4.6.2", "parse-duration": "1.0.2", "read-pkg": "7.1.0", "reflect-metadata": "^0.1.13", diff --git a/src/services/third-party.ts b/src/services/third-party.ts index e6a8357..706391a 100644 --- a/src/services/third-party.ts +++ b/src/services/third-party.ts @@ -1,6 +1,7 @@ import {inject, injectable} from 'inversify'; import SpotifyWebApi from 'spotify-web-api-node'; import Youtube from 'youtube.ts'; +import pRetry from 'p-retry'; import {TYPES} from '../types.js'; import Config from './config.js'; @@ -30,9 +31,10 @@ export default class ThirdParty { } private async refreshSpotifyToken() { - const auth = await this.spotify.clientCredentialsGrant(); - this.spotify.setAccessToken(auth.body.access_token); - - this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken.bind(this), (auth.body.expires_in / 2) * 1000); + await pRetry(async () => { + const auth = await this.spotify.clientCredentialsGrant(); + this.spotify.setAccessToken(auth.body.access_token); + this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken.bind(this), (auth.body.expires_in / 2) * 1000); + }, {retries: 5}); } } diff --git a/yarn.lock b/yarn.lock index b228176..cc2cc35 100644 --- a/yarn.lock +++ b/yarn.lock @@ -441,6 +441,11 @@ dependencies: "@types/node" "*" +"@types/retry@0.12.0": + version "0.12.0" + resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d" + integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA== + "@types/spotify-api@*": version "0.0.15" resolved "https://registry.yarnpkg.com/@types/spotify-api/-/spotify-api-0.0.15.tgz#c5f257159194f085cc05b70c0012ac80c6103199" @@ -3233,6 +3238,14 @@ p-queue@^7.2.0: eventemitter3 "^4.0.7" p-timeout "^5.0.2" +p-retry@4.6.2: + version "4.6.2" + resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-4.6.2.tgz#9baae7184057edd4e17231cee04264106e092a16" + integrity sha512-312Id396EbJdvRONlngUx0NydfrIQ5lsYu0znKVUzVvArzEIt08V1qhtyESbGVd1FGX7UKtiFp5uwKZdM8wIuQ== + dependencies: + "@types/retry" "0.12.0" + retry "^0.13.1" + p-timeout@^5.0.2: version "5.0.2" resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-5.0.2.tgz#d12964c4b2f988e15f72b455c2c428d82a0ec0a0" @@ -3666,7 +3679,7 @@ restore-cursor@^4.0.0: onetime "^5.1.0" signal-exit "^3.0.2" -retry@0.13.1: +retry@0.13.1, retry@^0.13.1: version "0.13.1" resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658" integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==