Skip to content

Commit

Permalink
Retry Spotify token refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
codetheweb committed Jul 16, 2022
1 parent 28bcd4b commit e489b10
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions src/services/third-party.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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});
}
}
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit e489b10

Please sign in to comment.