PersistenceManager.coffee 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) ->) ->
  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. return callback null, body.lines, body.version, body.ranges, body.pathname
  43. else if res.statusCode == 404
  44. return callback(new Errors.NotFoundError("doc not not found: #{url}"))
  45. else
  46. return callback(new Error("error accessing web API: #{url} #{res.statusCode}"))
  47. setDoc: (project_id, doc_id, lines, version, ranges, _callback = (error) ->) ->
  48. timer = new Metrics.Timer("persistenceManager.setDoc")
  49. callback = (args...) ->
  50. timer.done()
  51. _callback(args...)
  52. url = "#{Settings.apis.web.url}/project/#{project_id}/doc/#{doc_id}"
  53. request {
  54. url: url
  55. method: "POST"
  56. json:
  57. lines: lines
  58. ranges: ranges
  59. version: version
  60. auth:
  61. user: Settings.apis.web.user
  62. pass: Settings.apis.web.pass
  63. sendImmediately: true
  64. jar: false
  65. timeout: MAX_HTTP_REQUEST_LENGTH
  66. }, (error, res, body) ->
  67. return callback(error) if error?
  68. if res.statusCode >= 200 and res.statusCode < 300
  69. return callback null
  70. else if res.statusCode == 404
  71. return callback(new Errors.NotFoundError("doc not not found: #{url}"))
  72. else
  73. return callback(new Error("error accessing web API: #{url} #{res.statusCode}"))