Use requestretry node package

This commit is contained in:
Shane Kilkelly 2018-02-12 15:16:21 +00:00
parent 8117061674
commit 0efc8136cb
3 changed files with 17 additions and 18 deletions

View file

@ -1,29 +1,15 @@
settings = require "settings-sharelatex"
logger = require "logger-sharelatex"
_ = require "underscore"
request = require "request"
request = require "requestretry"
Errors = require '../Errors/Errors'
makeRequest = (opts, callback)->
retryTimings = [1, 2, 4, 8, 16, 32, 32, 32]
if settings.apis?.analytics?.url?
urlPath = opts.url
opts.url = "#{settings.apis.analytics.url}#{urlPath}"
iteration = 0
_go = () ->
request opts, (err, response, data) ->
if err?
if iteration == retryTimings.length or !opts.retryOnFail
logger.err {err, url: opts.url},
"Error in analytics request, retries failed"
return callback(err)
backoffSeconds = retryTimings[iteration]
iteration += 1
setTimeout(_go, backoffSeconds * 1000)
else
callback(null, response, data)
_go()
request opts, callback
else
callback(new Errors.ServiceNotConfiguredError('Analytics service not configured'))
@ -51,7 +37,8 @@ module.exports =
method:"POST"
timeout:1000
url: "/user/#{user_id}/event"
retryOnFail: true
maxAttempts: 20
retryDelay: 5000
if settings.overleaf?
opts.qs = {fromV2: 1}
makeRequest opts, callback
@ -69,7 +56,8 @@ module.exports =
qs:
userId: userId
projectId: projectId
retryOnFail: true
maxAttempts: 20
retryDelay: 5000
if settings.overleaf?
opts.qs.fromV2 = 1
makeRequest opts, callback

View file

@ -6926,6 +6926,11 @@
"from": "request@>=2.69.0 <3.0.0",
"resolved": "https://registry.npmjs.org/request/-/request-2.83.0.tgz"
},
"requestretry": {
"version": "1.13.0",
"from": "requestretry@latest",
"resolved": "https://registry.npmjs.org/requestretry/-/requestretry-1.13.0.tgz"
},
"requests": {
"version": "0.1.7",
"from": "requests@>=0.1.7 <0.2.0",
@ -9309,6 +9314,11 @@
"resolved": "https://registry.npmjs.org/websocket-extensions/-/websocket-extensions-0.1.3.tgz",
"dev": true
},
"when": {
"version": "3.7.8",
"from": "when@>=3.7.7 <4.0.0",
"resolved": "https://registry.npmjs.org/when/-/when-3.7.8.tgz"
},
"which": {
"version": "1.0.9",
"from": "which@>=1.0.5 <1.1.0",

View file

@ -71,6 +71,7 @@
"pug": "^2.0.0-beta6",
"redis-sharelatex": "git+https://github.com/sharelatex/redis-sharelatex.git#v1.0.4",
"request": "^2.69.0",
"requestretry": "^1.13.0",
"requests": "^0.1.7",
"rimraf": "2.2.6",
"rolling-rate-limiter": "git+https://github.com/ShaneKilkelly/rolling-rate-limiter.git#master",