PersistenceManager.coffee 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, lastUpdatedAt, lastUpdatedBy,_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. lastUpdatedBy: lastUpdatedBy
  63. lastUpdatedAt: lastUpdatedAt
  64. auth:
  65. user: Settings.apis.web.user
  66. pass: Settings.apis.web.pass
  67. sendImmediately: true
  68. jar: false
  69. timeout: MAX_HTTP_REQUEST_LENGTH
  70. }, (error, res, body) ->
  71. return callback(error) if error?
  72. if res.statusCode >= 200 and res.statusCode < 300
  73. return callback null
  74. else if res.statusCode == 404
  75. return callback(new Errors.NotFoundError("doc not not found: #{url}"))
  76. else
  77. return callback(new Error("error accessing web API: #{url} #{res.statusCode}"))