PersistenceManager.coffee 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Settings = require "settings-sharelatex"
  2. Errors = require "./Errors"
  3. Metrics = require "./Metrics"
  4. logger = require "logger-sharelatex"
  5. request = (require("requestretry")).defaults({
  6. maxAttempts: 2
  7. retryDelay: 10
  8. })
  9. # We have to be quick with HTTP calls because we're holding a lock that
  10. # expires after 30 seconds. We can't let any errors in the rest of the stack
  11. # hold us up, and need to bail out quickly if there is a problem.
  12. MAX_HTTP_REQUEST_LENGTH = 5000 # 5 seconds
  13. module.exports = PersistenceManager =
  14. getDoc: (project_id, doc_id, _callback = (error, lines, version, ranges, pathname, projectHistoryId) ->) ->
  15. timer = new Metrics.Timer("persistenceManager.getDoc")
  16. callback = (args...) ->
  17. timer.done()
  18. _callback(args...)
  19. url = "#{Settings.apis.web.url}/project/#{project_id}/doc/#{doc_id}"
  20. request {
  21. url: url
  22. method: "GET"
  23. headers:
  24. "accept": "application/json"
  25. auth:
  26. user: Settings.apis.web.user
  27. pass: Settings.apis.web.pass
  28. sendImmediately: true
  29. jar: false
  30. timeout: MAX_HTTP_REQUEST_LENGTH
  31. }, (error, res, body) ->
  32. return callback(error) if error?
  33. if res.statusCode >= 200 and res.statusCode < 300
  34. try
  35. body = JSON.parse body
  36. catch e
  37. return callback(e)
  38. if !body.lines?
  39. return callback(new Error("web API response had no doc lines"))
  40. if !body.version? or not body.version instanceof Number
  41. return callback(new Error("web API response had no valid doc version"))
  42. if !body.pathname?
  43. return callback(new Error("web API response had no valid doc pathname"))
  44. return callback null, body.lines, body.version, body.ranges, body.pathname, body.projectHistoryId
  45. else if res.statusCode == 404
  46. return callback(new Errors.NotFoundError("doc not not found: #{url}"))
  47. else
  48. return callback(new Error("error accessing web API: #{url} #{res.statusCode}"))
  49. setDoc: (project_id, doc_id, lines, version, ranges, _callback = (error) ->) ->
  50. timer = new Metrics.Timer("persistenceManager.setDoc")
  51. callback = (args...) ->
  52. timer.done()
  53. _callback(args...)
  54. url = "#{Settings.apis.web.url}/project/#{project_id}/doc/#{doc_id}"
  55. request {
  56. url: url
  57. method: "POST"
  58. json:
  59. lines: lines
  60. ranges: ranges
  61. version: version
  62. auth:
  63. user: Settings.apis.web.user
  64. pass: Settings.apis.web.pass
  65. sendImmediately: true
  66. jar: false
  67. timeout: MAX_HTTP_REQUEST_LENGTH
  68. }, (error, res, body) ->
  69. return callback(error) if error?
  70. if res.statusCode >= 200 and res.statusCode < 300
  71. return callback null
  72. else if res.statusCode == 404
  73. return callback(new Errors.NotFoundError("doc not not found: #{url}"))
  74. else
  75. return callback(new Error("error accessing web API: #{url} #{res.statusCode}"))