Add in track changes and doc updater flushing calls

This commit is contained in:
James Allen 2014-11-14 15:53:59 +00:00
parent 347ceaaf03
commit fd56655529
5 changed files with 118 additions and 1 deletions

View file

@ -28,6 +28,24 @@ module.exports = DocumentUpdaterManager =
logger.error {err, project_id, doc_id, url}, "doc updater returned a non-success status code: #{res.statusCode}" logger.error {err, project_id, doc_id, url}, "doc updater returned a non-success status code: #{res.statusCode}"
callback err callback err
flushProjectToMongoAndDelete: (project_id, callback = ()->) ->
logger.log project_id:project_id, "deleting project from document updater"
#timer = new metrics.Timer("delete.mongo.project")
url = "#{settings.apis.documentupdater.url}/project/#{project_id}"
request.del url, (err, res, body)->
#timer.done()
if err?
logger.error {err, project_id}, "error deleting project from document updater"
return callback(err)
else if 200 <= res.statusCode < 300
logger.log {project_id}, "deleted project from document updater"
return callback(null)
else
err = new Error("document updater returned a failure status code: #{res.statusCode}")
err.statusCode = res.statusCode
logger.error {err, project_id}, "document updater returned failure status code: #{res.statusCode}"
return callback(err)
queueChange: (project_id, doc_id, change, callback = ()->)-> queueChange: (project_id, doc_id, change, callback = ()->)->
jsonChange = JSON.stringify change jsonChange = JSON.stringify change
doc_key = "#{project_id}:#{doc_id}" doc_key = "#{project_id}:#{doc_id}"

View file

@ -0,0 +1,16 @@
settings = require "settings-sharelatex"
request = require "request"
logger = require "logger-sharelatex"
module.exports = TrackChangesManager =
flushProject: (project_id, callback = (error) ->) ->
logger.log project_id: project_id, "flushing project in track-changes api"
url = "#{settings.apis.trackchanges.url}/project/#{project_id}/flush"
request.post url, (error, res, body) ->
return callback(error) if error?
if 200 <= res.statusCode < 300
callback(null)
else
error = new Error("track-changes api responded with non-success code: #{res.statusCode}")
logger.error err: error, project_id: project_id, "error flushing project in track-changes api"
callback(error)

View file

@ -82,7 +82,6 @@ describe "receiveUpdate", ->
}] }]
describe "with an error", -> describe "with an error", ->
before (done) -> before (done) ->
@clientAErrors = [] @clientAErrors = []
@clientA.on "otUpdateError", (error) => @clientAErrors.push(error) @clientA.on "otUpdateError", (error) => @clientAErrors.push(error)

View file

@ -62,6 +62,42 @@ describe 'DocumentUpdaterManager', ->
.calledWith(err) .calledWith(err)
.should.equal true .should.equal true
describe 'flushProjectToMongoAndDelete', ->
beforeEach ->
@callback = sinon.stub()
describe "successfully", ->
beforeEach ->
@request.del = sinon.stub().callsArgWith(1, null, {statusCode: 204}, "")
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
it 'should delete the project from the document updater', ->
url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}"
@request.del.calledWith(url).should.equal true
it "should call the callback with no error", ->
@callback.calledWith(null).should.equal true
describe "when the document updater API returns an error", ->
beforeEach ->
@request.del = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null)
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
it "should return an error to the callback", ->
@callback.calledWith(@error).should.equal true
describe "when the document updater returns a failure error code", ->
beforeEach ->
@request.del = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "")
@DocumentUpdaterManager.flushProjectToMongoAndDelete @project_id, @callback
it "should return the callback with an error", ->
err = new Error("doc updater returned failure status code: 500")
err.statusCode = 500
@callback
.calledWith(err)
.should.equal true
describe 'queueChange', -> describe 'queueChange', ->
beforeEach -> beforeEach ->
@change = { @change = {

View file

@ -0,0 +1,48 @@
chai = require('chai')
chai.should()
sinon = require("sinon")
modulePath = "../../../app/js/TrackChangesManager"
SandboxedModule = require('sandboxed-module')
describe "TrackChangesManager", ->
beforeEach ->
@TrackChangesManager = SandboxedModule.require modulePath, requires:
"request" : @request = sinon.stub()
"settings-sharelatex": @settings =
apis:
trackchanges:
url: "trackchanges.sharelatex.com"
"logger-sharelatex": @logger = {log: sinon.stub(), error: sinon.stub()}
@project_id = "project-id-123"
@callback = sinon.stub()
describe "flushProject", ->
describe "with a successful response code", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, "")
@TrackChangesManager.flushProject @project_id, @callback
it "should flush the project in the track changes api", ->
@request.post
.calledWith("#{@settings.apis.trackchanges.url}/project/#{@project_id}/flush")
.should.equal true
it "should call the callback without an error", ->
@callback.calledWith(null).should.equal true
describe "with a failed response code", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
@TrackChangesManager.flushProject @project_id, @callback
it "should call the callback with an error", ->
@callback.calledWith(new Error("track-changes api responded with a non-success code: 500")).should.equal true
it "should log the error", ->
@logger.error
.calledWith({
err: new Error("track-changes api responded with a non-success code: 500")
project_id: @project_id
}, "error flushing project in track-changes api")
.should.equal true