Просмотр исходного кода

Merge pull request #19296 from overleaf/jpa-issue-19290-3

[clsi] atomic writing of LaTeXMk output

GitOrigin-RevId: d81c497370587b98fc7ad282035cd59b0ae09ec8
Jakob Ackermann 2 лет назад
Родитель
Сommit
9f68bc5660

+ 8 - 5
services/clsi/app/js/LatexRunner.js

@@ -110,11 +110,14 @@ function _writeLogOutput(projectId, directory, output, callback) {
   // internal method for writing non-empty log files
   function _writeFile(file, content, cb) {
     if (content && content.length > 0) {
-      fs.writeFile(file, content, err => {
-        if (err) {
-          logger.error({ err, projectId, file }, 'error writing log file') // don't fail on error
-        }
-        cb()
+      fs.unlink(file, () => {
+        fs.writeFile(file, content, { flag: 'wx' }, err => {
+          if (err) {
+            // don't fail on error
+            logger.error({ err, projectId, file }, 'error writing log file')
+          }
+          cb()
+        })
       })
     } else {
       cb()

+ 13 - 2
services/clsi/test/unit/js/LatexRunnerTests.js

@@ -23,6 +23,9 @@ describe('LatexRunner', function () {
     }
     this.fs = {
       writeFile: sinon.stub().yields(),
+      unlink: sinon
+        .stub()
+        .yields(new Error('ENOENT: no such file or directory, unlink ...')),
     }
     this.LatexRunner = SandboxedModule.require(MODULE_PATH, {
       requires: {
@@ -99,11 +102,19 @@ describe('LatexRunner', function () {
       it('should record the stdout and stderr', function () {
         this.fs.writeFile.should.have.been.calledWith(
           this.directory + '/' + 'output.stdout',
-          'this is stdout'
+          'this is stdout',
+          { flag: 'wx' }
         )
         this.fs.writeFile.should.have.been.calledWith(
           this.directory + '/' + 'output.stderr',
-          'this is stderr'
+          'this is stderr',
+          { flag: 'wx' }
+        )
+        this.fs.unlink.should.have.been.calledWith(
+          this.directory + '/' + 'output.stdout'
+        )
+        this.fs.unlink.should.have.been.calledWith(
+          this.directory + '/' + 'output.stderr'
         )
       })