Flush to the track changes api using the project id as well

This commit is contained in:
James Allen 2014-03-19 15:56:44 +00:00
parent 4f878e000b
commit 2d28f1903f
5 changed files with 23 additions and 21 deletions

View file

@ -47,7 +47,7 @@ module.exports = DocOpsManager =
pushDocOp: (project_id, doc_id, op, callback = (error) ->) ->
RedisManager.pushDocOp doc_id, op, (error, version) ->
return callback(error) if error?
TrackChangesManager.pushUncompressedHistoryOp doc_id, op, (error) ->
TrackChangesManager.pushUncompressedHistoryOp project_id, doc_id, op, (error) ->
return callback(error) if error?
callback null, version

View file

@ -5,13 +5,13 @@ RedisManager = require "./RedisManager"
crypto = require("crypto")
module.exports = TrackChangesManager =
flushDocChanges: (doc_id, callback = (error) ->) ->
flushDocChanges: (project_id, doc_id, callback = (error) ->) ->
if !settings.apis?.trackchanges?
logger.warn doc_id: doc_id, "track changes API is not configured, so not flushing"
return callback()
url = "#{settings.apis.trackchanges.url}/doc/#{doc_id}/flush"
logger.log doc_id: doc_id, url: url, "flushing doc in track changes api"
url = "#{settings.apis.trackchanges.url}/project/#{project_id}/doc/#{doc_id}/flush"
logger.log project_id: project_id, doc_id: doc_id, url: url, "flushing doc in track changes api"
request.post url, (error, res, body)->
if error?
return callback(error)
@ -22,7 +22,7 @@ module.exports = TrackChangesManager =
return callback(error)
FLUSH_EVERY_N_OPS: 50
pushUncompressedHistoryOp: (doc_id, op, callback = (error) ->) ->
pushUncompressedHistoryOp: (project_id, doc_id, op, callback = (error) ->) ->
RedisManager.getHistoryLoadManagerThreshold (error, threshold) ->
return callback(error) if error?
if TrackChangesManager.getLoadManagerBucket(doc_id) < threshold
@ -31,10 +31,10 @@ module.exports = TrackChangesManager =
if length > 0 and length % TrackChangesManager.FLUSH_EVERY_N_OPS == 0
# Do this in the background since it uses HTTP and so may be too
# slow to wait for when processing a doc update.
logger.log length: length, doc_id: doc_id, "flushing track changes api"
TrackChangesManager.flushDocChanges doc_id, (error) ->
logger.log length: length, doc_id: doc_id, project_id: project_id, "flushing track changes api"
TrackChangesManager.flushDocChanges project_id, doc_id, (error) ->
if error?
logger.error err: error, doc_id: doc_id, "error flushing doc to track changes api"
logger.error err: error, doc_id: doc_id, project_id: project_id, "error flushing doc to track changes api"
callback()
else
callback()

View file

@ -6,7 +6,7 @@ module.exports = MockTrackChangesApi =
callback()
run: () ->
app.post "/doc/:doc_id/flush", (req, res, next) =>
app.post "/project/:project_id/doc/:doc_id/flush", (req, res, next) =>
@flushDoc req.params.doc_id, (error) ->
if error?
res.send 500

View file

@ -311,7 +311,7 @@ describe "DocOpsManager", ->
beforeEach ->
@op = "mock-op"
@RedisManager.pushDocOp = sinon.stub().callsArgWith(2, null, @version = 42)
@TrackChangesManager.pushUncompressedHistoryOp = sinon.stub().callsArg(2)
@TrackChangesManager.pushUncompressedHistoryOp = sinon.stub().callsArg(3)
@DocOpsManager.pushDocOp @project_id, @doc_id, @op, @callback
it "should push the op in to the docOps list", ->
@ -321,7 +321,7 @@ describe "DocOpsManager", ->
it "should push the op into the pushUncompressedHistoryOp", ->
@TrackChangesManager.pushUncompressedHistoryOp
.calledWith(@doc_id, @op)
.calledWith(@project_id, @doc_id, @op)
.should.equal true
it "should call the callback with the version", ->

View file

@ -10,6 +10,7 @@ describe "TrackChangesManager", ->
"settings-sharelatex": @Settings = {}
"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
"./RedisManager": @RedisManager = {}
@project_id = "mock-project-id"
@doc_id = "mock-doc-id"
@callback = sinon.stub()
@ -21,11 +22,11 @@ describe "TrackChangesManager", ->
describe "successfully", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 204)
@TrackChangesManager.flushDocChanges @doc_id, @callback
@TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
it "should send a request to the track changes api", ->
@request.post
.calledWith("#{@Settings.apis.trackchanges.url}/doc/#{@doc_id}/flush")
.calledWith("#{@Settings.apis.trackchanges.url}/project/#{@project_id}/doc/#{@doc_id}/flush")
.should.equal true
it "should return the callback", ->
@ -34,7 +35,7 @@ describe "TrackChangesManager", ->
describe "when the track changes api returns an error", ->
beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 500)
@TrackChangesManager.flushDocChanges @doc_id, @callback
@TrackChangesManager.flushDocChanges @project_id, @doc_id, @callback
it "should return the callback with an error", ->
@callback.calledWith(new Error("track changes api return non-success code: 500")).should.equal true
@ -42,7 +43,7 @@ describe "TrackChangesManager", ->
describe "pushUncompressedHistoryOp", ->
beforeEach ->
@op = "mock-op"
@TrackChangesManager.flushDocChanges = sinon.stub().callsArg(1)
@TrackChangesManager.flushDocChanges = sinon.stub().callsArg(2)
describe "when the doc is under the load manager threshold", ->
beforeEach ->
@ -52,7 +53,7 @@ describe "TrackChangesManager", ->
describe "pushing the op", ->
beforeEach ->
@RedisManager.pushUncompressedHistoryOp = sinon.stub().callsArgWith(2, null, 1)
@TrackChangesManager.pushUncompressedHistoryOp @doc_id, @op, @callback
@TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
it "should push the op into redis", ->
@RedisManager.pushUncompressedHistoryOp
@ -69,25 +70,26 @@ describe "TrackChangesManager", ->
beforeEach ->
@RedisManager.pushUncompressedHistoryOp =
sinon.stub().callsArgWith(2, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
@TrackChangesManager.pushUncompressedHistoryOp @doc_id, @op, @callback
@TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
it "should tell the track changes api to flush", ->
@TrackChangesManager.flushDocChanges
.calledWith(@doc_id)
.calledWith(@project_id, @doc_id)
.should.equal true
describe "when TrackChangesManager errors", ->
beforeEach ->
@RedisManager.pushUncompressedHistoryOp =
sinon.stub().callsArgWith(2, null, 2 * @TrackChangesManager.FLUSH_EVERY_N_OPS)
@TrackChangesManager.flushDocChanges = sinon.stub().callsArgWith(1, @error = new Error("oops"))
@TrackChangesManager.pushUncompressedHistoryOp @doc_id, @op, @callback
@TrackChangesManager.flushDocChanges = sinon.stub().callsArgWith(2, @error = new Error("oops"))
@TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
it "should log out the error", ->
@logger.error
.calledWith(
err: @error
doc_id: @doc_id
project_id: @project_id
"error flushing doc to track changes api"
)
.should.equal true
@ -97,7 +99,7 @@ describe "TrackChangesManager", ->
@RedisManager.getHistoryLoadManagerThreshold = sinon.stub().callsArgWith(0, null, 40)
@TrackChangesManager.getLoadManagerBucket = sinon.stub().returns(50)
@RedisManager.pushUncompressedHistoryOp = sinon.stub().callsArgWith(2, null, 1)
@TrackChangesManager.pushUncompressedHistoryOp @doc_id, @op, @callback
@TrackChangesManager.pushUncompressedHistoryOp @project_id, @doc_id, @op, @callback
it "should not push the op", ->
@RedisManager.pushUncompressedHistoryOp.called.should.equal false