From a45200dfe33b7ede51144b65cb3e0160267da4c1 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 19 May 2014 16:10:41 +0100 Subject: [PATCH] Automatically clear CLSI cache when compile fails --- .../coffee/Features/Compile/ClsiManager.coffee | 3 +++ .../Features/Compile/CompileController.coffee | 5 ++++- .../Features/Compile/CompileManager.coffee | 8 ++++++++ .../coffee/Compile/ClsiManagerTests.coffee | 13 +++++++++++++ .../Compile/CompileControllerTests.coffee | 13 ++++++++++--- .../coffee/Compile/CompileManagerTests.coffee | 17 +++++++++++++++++ 6 files changed, 55 insertions(+), 4 deletions(-) diff --git a/services/web/app/coffee/Features/Compile/ClsiManager.coffee b/services/web/app/coffee/Features/Compile/ClsiManager.coffee index b27a70ec70..b7930f5cf3 100644 --- a/services/web/app/coffee/Features/Compile/ClsiManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiManager.coffee @@ -26,6 +26,9 @@ module.exports = ClsiManager = return callback(error) if error? callback null, body?.split("\n") or [] + deleteAuxFiles: (project_id, callback = (error) ->) -> + request.del "#{Settings.apis.clsi.url}/project/#{project_id}", callback + _postToClsi: (project_id, req, callback = (error, response) ->) -> request.post { url: "#{Settings.apis.clsi.url}/project/#{project_id}/compile" diff --git a/services/web/app/coffee/Features/Compile/CompileController.coffee b/services/web/app/coffee/Features/Compile/CompileController.coffee index 09b4bf51ba..aa4fab9e30 100644 --- a/services/web/app/coffee/Features/Compile/CompileController.coffee +++ b/services/web/app/coffee/Features/Compile/CompileController.coffee @@ -1,6 +1,7 @@ Metrics = require "../../infrastructure/Metrics" Project = require("../../models/Project").Project CompileManager = require("./CompileManager") +ClsiManager = require("./ClsiManager") logger = require "logger-sharelatex" request = require "request" Settings = require "settings-sharelatex" @@ -35,7 +36,9 @@ module.exports = CompileController = deleteAuxFiles: (req, res, next) -> project_id = req.params.Project_id - CompileController.proxyToClsi("/project/#{project_id}", req, res, next) + ClsiManager.deleteAuxFiles project_id, (error) -> + return next(error) if error? + res.send(200) compileAndDownloadPdf: (req, res, next)-> project_id = req.params.project_id diff --git a/services/web/app/coffee/Features/Compile/CompileManager.coffee b/services/web/app/coffee/Features/Compile/CompileManager.coffee index 0bef4b9796..a49338ed9b 100644 --- a/services/web/app/coffee/Features/Compile/CompileManager.coffee +++ b/services/web/app/coffee/Features/Compile/CompileManager.coffee @@ -33,8 +33,16 @@ module.exports = CompileManager = ClsiManager.sendRequest project_id, (error, status, outputFiles) -> return callback(error) if error? logger.log files: outputFiles, "output files" + + if status == "failure" + # Sometimes compiles fail because the project gets in a broken + # state in LaTeX. So always clear cache on a failure. + # Can do this in the background. + ClsiManager.deleteAuxFiles project_id + callback(null, status, outputFiles) + getLogLines: (project_id, callback)-> Metrics.inc "editor.raw-logs" ClsiManager.getLogLines project_id, (error, logLines)-> diff --git a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee index 446c33ca90..6791443c9a 100644 --- a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee @@ -72,6 +72,19 @@ describe "ClsiManager", -> it "should call the callback with a failure statue", -> @callback.calledWith(null, @status).should.equal true + describe "deleteAuxFiles", -> + beforeEach -> + @request.del = sinon.stub().callsArg(1) + @ClsiManager.deleteAuxFiles @project_id, @callback + + it "should call the delete method in the CLSI", -> + @request.del + .calledWith("#{@settings.apis.clsi.url}/project/#{@project_id}") + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true + describe "_buildRequest", -> beforeEach -> @project = diff --git a/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee b/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee index f6c0712819..0820c0431c 100644 --- a/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee @@ -21,6 +21,7 @@ describe "CompileController", -> "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() } "../../infrastructure/Metrics": @Metrics = { inc: sinon.stub() } "./CompileManager":@CompileManager + "./ClsiManager": @ClsiManager "../Authentication/AuthenticationController": @AuthenticationController = {} @project_id = "project-id" @next = sinon.stub() @@ -144,14 +145,20 @@ describe "CompileController", -> describe "deleteAuxFiles", -> beforeEach -> - @CompileController.proxyToClsi = sinon.stub() + @ClsiManager.deleteAuxFiles = sinon.stub().callsArg(1) @req.params = Project_id: @project_id + @res.send = sinon.stub() @CompileController.deleteAuxFiles @req, @res, @next it "should proxy to the CLSI", -> - @CompileController.proxyToClsi - .calledWith("/project/#{@project_id}", @req, @res, @next) + @ClsiManager.deleteAuxFiles + .calledWith(@project_id) + .should.equal true + + it "should return a 200", -> + @res.send + .calledWith(200) .should.equal true describe "compileAndDownloadPdf", -> diff --git a/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee index f1b963a8c5..24de557aac 100644 --- a/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee @@ -75,6 +75,23 @@ describe "CompileManager", -> @logger.log .calledWith(project_id: @project_id, user_id: @user_id, "compiling project") .should.equal true + + describe "when the compile fails", -> + beforeEach -> + @CompileManager._checkIfAutoCompileLimitHasBeenHit = (_, cb)-> cb(null, true) + @ClsiManager.deleteAuxFiles = sinon.stub() + @ClsiManager.sendRequest = sinon.stub().callsArgWith(1, null, @status = "failure") + @CompileManager.compile @project_id, @user_id, {}, @callback + + it "should call the callback", -> + @callback + .calledWith(null, @status) + .should.equal true + + it "should clear the CLSI cache", -> + @ClsiManager.deleteAuxFiles + .calledWith(@project_id) + .should.equal true describe "when the project has been recently compiled", -> beforeEach ->