From fd56655529e35528b068385f0b0bea6134aed850 Mon Sep 17 00:00:00 2001 From: James Allen Date: Fri, 14 Nov 2014 15:53:59 +0000 Subject: [PATCH] Add in track changes and doc updater flushing calls --- .../app/coffee/DocumentUpdaterManager.coffee | 18 +++++++ .../app/coffee/TrackChangesManager.coffee | 16 +++++++ .../coffee/ReceiveUpdateTests.coffee | 1 - .../coffee/DocumentUpdaterManagerTests.coffee | 36 ++++++++++++++ .../coffee/TrackChangesManagerTests.coffee | 48 +++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 services/real-time/app/coffee/TrackChangesManager.coffee create mode 100644 services/real-time/test/unit/coffee/TrackChangesManagerTests.coffee diff --git a/services/real-time/app/coffee/DocumentUpdaterManager.coffee b/services/real-time/app/coffee/DocumentUpdaterManager.coffee index 2af68eef0c..8610b454ec 100644 --- a/services/real-time/app/coffee/DocumentUpdaterManager.coffee +++ b/services/real-time/app/coffee/DocumentUpdaterManager.coffee @@ -27,6 +27,24 @@ module.exports = DocumentUpdaterManager = err.statusCode = res.statusCode logger.error {err, project_id, doc_id, url}, "doc updater returned a non-success status code: #{res.statusCode}" 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 = ()->)-> jsonChange = JSON.stringify change diff --git a/services/real-time/app/coffee/TrackChangesManager.coffee b/services/real-time/app/coffee/TrackChangesManager.coffee new file mode 100644 index 0000000000..49fa956cf6 --- /dev/null +++ b/services/real-time/app/coffee/TrackChangesManager.coffee @@ -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) \ No newline at end of file diff --git a/services/real-time/test/acceptance/coffee/ReceiveUpdateTests.coffee b/services/real-time/test/acceptance/coffee/ReceiveUpdateTests.coffee index 9c95567d93..fd1b9a0dfe 100644 --- a/services/real-time/test/acceptance/coffee/ReceiveUpdateTests.coffee +++ b/services/real-time/test/acceptance/coffee/ReceiveUpdateTests.coffee @@ -82,7 +82,6 @@ describe "receiveUpdate", -> }] describe "with an error", -> - before (done) -> @clientAErrors = [] @clientA.on "otUpdateError", (error) => @clientAErrors.push(error) diff --git a/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee b/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee index 80caa45802..8a9d335b1a 100644 --- a/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee +++ b/services/real-time/test/unit/coffee/DocumentUpdaterManagerTests.coffee @@ -62,6 +62,42 @@ describe 'DocumentUpdaterManager', -> .calledWith(err) .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', -> beforeEach -> @change = { diff --git a/services/real-time/test/unit/coffee/TrackChangesManagerTests.coffee b/services/real-time/test/unit/coffee/TrackChangesManagerTests.coffee new file mode 100644 index 0000000000..69c11c87b1 --- /dev/null +++ b/services/real-time/test/unit/coffee/TrackChangesManagerTests.coffee @@ -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 +