Retry Spotify token refresh

This commit is contained in:
Max Isom 2022-07-16 13:58:24 -05:00
parent 28bcd4b45e
commit e489b10565
No known key found for this signature in database
GPG key ID: 25C9B1A7F6798880
4 changed files with 23 additions and 5 deletions

View file

@ -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). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [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 ## [2.1.0] - 2022-06-25
- `/loop` command that plays the current song on loop - `/loop` command that plays the current song on loop

View file

@ -103,6 +103,7 @@
"p-event": "^5.0.1", "p-event": "^5.0.1",
"p-limit": "^4.0.0", "p-limit": "^4.0.0",
"p-queue": "^7.2.0", "p-queue": "^7.2.0",
"p-retry": "4.6.2",
"parse-duration": "1.0.2", "parse-duration": "1.0.2",
"read-pkg": "7.1.0", "read-pkg": "7.1.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",

View file

@ -1,6 +1,7 @@
import {inject, injectable} from 'inversify'; import {inject, injectable} from 'inversify';
import SpotifyWebApi from 'spotify-web-api-node'; import SpotifyWebApi from 'spotify-web-api-node';
import Youtube from 'youtube.ts'; import Youtube from 'youtube.ts';
import pRetry from 'p-retry';
import {TYPES} from '../types.js'; import {TYPES} from '../types.js';
import Config from './config.js'; import Config from './config.js';
@ -30,9 +31,10 @@ export default class ThirdParty {
} }
private async refreshSpotifyToken() { private async refreshSpotifyToken() {
await pRetry(async () => {
const auth = await this.spotify.clientCredentialsGrant(); const auth = await this.spotify.clientCredentialsGrant();
this.spotify.setAccessToken(auth.body.access_token); this.spotify.setAccessToken(auth.body.access_token);
this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken.bind(this), (auth.body.expires_in / 2) * 1000); this.spotifyTokenTimerId = setTimeout(this.refreshSpotifyToken.bind(this), (auth.body.expires_in / 2) * 1000);
}, {retries: 5});
} }
} }

View file

@ -441,6 +441,11 @@
dependencies: dependencies:
"@types/node" "*" "@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@*": "@types/spotify-api@*":
version "0.0.15" version "0.0.15"
resolved "https://registry.yarnpkg.com/@types/spotify-api/-/spotify-api-0.0.15.tgz#c5f257159194f085cc05b70c0012ac80c6103199" 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" eventemitter3 "^4.0.7"
p-timeout "^5.0.2" 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: p-timeout@^5.0.2:
version "5.0.2" version "5.0.2"
resolved "https://registry.yarnpkg.com/p-timeout/-/p-timeout-5.0.2.tgz#d12964c4b2f988e15f72b455c2c428d82a0ec0a0" 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" onetime "^5.1.0"
signal-exit "^3.0.2" signal-exit "^3.0.2"
retry@0.13.1: retry@0.13.1, retry@^0.13.1:
version "0.13.1" version "0.13.1"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658" resolved "https://registry.yarnpkg.com/retry/-/retry-0.13.1.tgz#185b1587acf67919d63b357349e03537b2484658"
integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg== integrity sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==