Skip to content

Commit

Permalink
Retry failed requests to GitHub (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyBitz committed Jun 21, 2019
1 parent f87970a commit da28860
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Packages
const fetch = require('node-fetch')
const retry = require('async-retry')
const convertStream = require('stream-to-string')
const ms = require('ms')

Expand Down Expand Up @@ -40,13 +41,17 @@ module.exports = class Cache {
headers.Authorization = `token ${token}`
}

const { status, body } = await fetch(url, { headers })
const { status, body } = await retry(async () => {
const response = await fetch(url, { headers })

if (status !== 200) {
throw new Error(
`Tried to cache RELEASES, but failed fetching ${url}, status ${status}`
)
}
if (response.status !== 200) {
throw new Error(
`Tried to cache RELEASES, but failed fetching ${url}, status ${status}`
)
}

return response;
}, { retries: 3 });

const content = await convertStream(body)
const matches = content.match(/[^ ]*\.nupkg/gim)
Expand All @@ -71,13 +76,17 @@ module.exports = class Cache {
headers.Authorization = `token ${token}`
}

const response = await fetch(url, { headers })
const response = await retry(async () => {
const response = await fetch(url, { headers })

if (response.status !== 200) {
throw new Error(
`GitHub API responded with ${response.status} for url ${url}`
)
}
if (response.status !== 200) {
throw new Error(
`GitHub API responded with ${response.status} for url ${url}`
)
}

return response;
}, { retries: 3 });

const data = await response.json()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
]
},
"dependencies": {
"async-retry": "1.2.3",
"date-fns": "1.29.0",
"express-useragent": "1.0.12",
"fetch": "1.1.0",
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==

async-retry@1.2.3:
version "1.2.3"
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.2.3.tgz#a6521f338358d322b1a0012b79030c6f411d1ce0"
integrity sha512-tfDb02Th6CE6pJUF2gjW5ZVjsgwlucVXOEQMvEX9JgSJMs9gAX+Nz3xRuJBKuUYjTSYORqvDBORdAQ3LU59g7Q==
dependencies:
retry "0.12.0"

async@^1.4.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/async/-/async-1.5.2.tgz#ec6a61ae56480c0c3cb241c95618e20892f9672a"
Expand Down Expand Up @@ -5269,6 +5276,11 @@ ret@~0.1.10:
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

retry@0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=

right-align@^0.1.1:
version "0.1.3"
resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef"
Expand Down

1 comment on commit da28860

@vercel
Copy link

@vercel vercel bot commented on da28860 Jun 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully aliased the URL https://hazel-33otiz5zl.zeit.sh to the following aliases.

Please sign in to comment.