Merge pull request #18474 from overleaf/ar-return-build-id-from-clsi-after-compile

[clsi] Return buildId after compiles

GitOrigin-RevId: 872048f4fea8f5a00b817e29bd26a444d179a45f
This commit is contained in:
Andrew Rumble 2024-05-24 10:38:12 +01:00 committed by Copybot
parent a0c8cf663a
commit 80ede301fa
5 changed files with 36 additions and 13 deletions

View file

@ -30,7 +30,7 @@ function compile(req, res, next) {
return next(error) return next(error)
} }
CompileManager.doCompileWithLock(request, (error, result) => { CompileManager.doCompileWithLock(request, (error, result) => {
let { outputFiles, stats, timings } = result || {} let { buildId, outputFiles, stats, timings } = result || {}
let code, status let code, status
if (outputFiles == null) { if (outputFiles == null) {
outputFiles = [] outputFiles = []
@ -98,6 +98,7 @@ function compile(req, res, next) {
if (error) { if (error) {
outputFiles = error.outputFiles || [] outputFiles = error.outputFiles || []
buildId = error.buildId
} }
timer.done() timer.done()
@ -107,6 +108,7 @@ function compile(req, res, next) {
error: error?.message || error, error: error?.message || error,
stats, stats,
timings, timings,
buildId,
outputUrlPrefix: Settings.apis.clsi.outputUrlPrefix, outputUrlPrefix: Settings.apis.clsi.outputUrlPrefix,
outputFiles: outputFiles.map(file => ({ outputFiles: outputFiles.map(file => ({
url: url:

View file

@ -208,7 +208,7 @@ async function doCompile(request) {
Metrics.inc('compiles-timeout', 1, request.metricsOpts) Metrics.inc('compiles-timeout', 1, request.metricsOpts)
} }
const { outputFiles, allEntries } = await _saveOutputFiles({ const { outputFiles, allEntries, buildId } = await _saveOutputFiles({
request, request,
compileDir, compileDir,
resourceList, resourceList,
@ -216,7 +216,7 @@ async function doCompile(request) {
timings, timings,
}) })
error.outputFiles = outputFiles // return output files so user can check logs error.outputFiles = outputFiles // return output files so user can check logs
error.buildId = buildId
// Clear project if this compile was abruptly terminated // Clear project if this compile was abruptly terminated
if (error.terminated || error.timedout) { if (error.terminated || error.timedout) {
await clearProjectWithListing( await clearProjectWithListing(
@ -280,7 +280,7 @@ async function doCompile(request) {
// Emit compile time. // Emit compile time.
timings.compile = ts timings.compile = ts
const { outputFiles } = await _saveOutputFiles({ const { outputFiles, buildId } = await _saveOutputFiles({
request, request,
compileDir, compileDir,
resourceList, resourceList,
@ -296,7 +296,7 @@ async function doCompile(request) {
emitPdfStats(stats, timings, request) emitPdfStats(stats, timings, request)
} }
return { outputFiles, stats, timings } return { outputFiles, stats, timings, buildId }
} }
async function _saveOutputFiles({ async function _saveOutputFiles({
@ -316,20 +316,24 @@ async function _saveOutputFiles({
let { outputFiles, allEntries } = let { outputFiles, allEntries } =
await OutputFileFinder.promises.findOutputFiles(resourceList, compileDir) await OutputFileFinder.promises.findOutputFiles(resourceList, compileDir)
let buildId
try { try {
outputFiles = await OutputCacheManager.promises.saveOutputFiles( const saveResult = await OutputCacheManager.promises.saveOutputFiles(
{ request, stats, timings }, { request, stats, timings },
outputFiles, outputFiles,
compileDir, compileDir,
outputDir outputDir
) )
buildId = saveResult.buildId
outputFiles = saveResult.outputFiles
} catch (err) { } catch (err) {
const { project_id: projectId, user_id: userId } = request const { project_id: projectId, user_id: userId } = request
logger.err({ projectId, userId, err }, 'failed to save output files') logger.err({ projectId, userId, err }, 'failed to save output files')
} }
timings.output = timer.done() timings.output = timer.done()
return { outputFiles, allEntries } return { outputFiles, allEntries, buildId }
} }
async function stopCompile(projectId, userId) { async function stopCompile(projectId, userId) {

View file

@ -164,7 +164,7 @@ module.exports = OutputCacheManager = {
outputDir, outputDir,
stats, stats,
(err, outputFiles) => { (err, outputFiles) => {
if (err) return callback(err, outputFiles) if (err) return callback(err, { outputFiles, buildId })
const enablePdfCaching = request.enablePdfCaching const enablePdfCaching = request.enablePdfCaching
const enablePdfCachingDark = const enablePdfCachingDark =
@ -173,7 +173,7 @@ module.exports = OutputCacheManager = {
!Settings.enablePdfCaching || !Settings.enablePdfCaching ||
(!enablePdfCaching && !enablePdfCachingDark) (!enablePdfCaching && !enablePdfCachingDark)
) { ) {
return callback(null, outputFiles) return callback(null, { outputFiles, buildId })
} }
OutputCacheManager.saveStreamsInContentDir( OutputCacheManager.saveStreamsInContentDir(
@ -191,9 +191,9 @@ module.exports = OutputCacheManager = {
{ err, outputDir, stats, timings }, { err, outputDir, stats, timings },
'pdf caching failed' 'pdf caching failed'
) )
return callback(null, outputFiles) return callback(null, { outputFiles, buildId })
} }
callback(err, outputFiles) callback(err, { outputFiles, buildId })
} }
) )
} }

View file

@ -50,6 +50,7 @@ function tryImageNameValidation(method, imageNameField) {
describe('CompileController', function () { describe('CompileController', function () {
beforeEach(function () { beforeEach(function () {
this.buildId = 'build-id-123'
this.CompileController = SandboxedModule.require(modulePath, { this.CompileController = SandboxedModule.require(modulePath, {
requires: { requires: {
'./CompileManager': (this.CompileManager = {}), './CompileManager': (this.CompileManager = {}),
@ -118,6 +119,7 @@ describe('CompileController', function () {
outputFiles: this.output_files, outputFiles: this.output_files,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
}) })
this.CompileController.compile(this.req, this.res) this.CompileController.compile(this.req, this.res)
}) })
@ -147,6 +149,7 @@ describe('CompileController', function () {
error: null, error: null,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
outputFiles: this.output_files.map(file => ({ outputFiles: this.output_files.map(file => ({
url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`, url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
@ -165,6 +168,7 @@ describe('CompileController', function () {
outputFiles: this.output_files, outputFiles: this.output_files,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
}) })
this.CompileController.compile(this.req, this.res) this.CompileController.compile(this.req, this.res)
}) })
@ -178,6 +182,7 @@ describe('CompileController', function () {
error: null, error: null,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
outputUrlPrefix: '', outputUrlPrefix: '',
outputFiles: this.output_files.map(file => ({ outputFiles: this.output_files.map(file => ({
url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`, url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
@ -207,6 +212,7 @@ describe('CompileController', function () {
outputFiles: this.output_files, outputFiles: this.output_files,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
}) })
this.CompileController.compile(this.req, this.res) this.CompileController.compile(this.req, this.res)
}) })
@ -221,6 +227,7 @@ describe('CompileController', function () {
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
buildId: this.buildId,
outputFiles: this.output_files.map(file => ({ outputFiles: this.output_files.map(file => ({
url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`, url: `${this.Settings.apis.clsi.url}/project/${this.project_id}/build/${file.build}/output/${file.path}`,
...file, ...file,
@ -250,6 +257,7 @@ describe('CompileController', function () {
outputFiles: this.output_files, outputFiles: this.output_files,
stats: this.stats, stats: this.stats,
timings: this.timings, timings: this.timings,
buildId: this.buildId,
}) })
this.CompileController.compile(this.req, this.res) this.CompileController.compile(this.req, this.res)
}) })
@ -262,6 +270,7 @@ describe('CompileController', function () {
status: 'failure', status: 'failure',
error: null, error: null,
stats: this.stats, stats: this.stats,
buildId: this.buildId,
timings: this.timings, timings: this.timings,
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
outputFiles: this.output_files.map(file => ({ outputFiles: this.output_files.map(file => ({
@ -276,9 +285,11 @@ describe('CompileController', function () {
describe('with an error', function () { describe('with an error', function () {
beforeEach(function () { beforeEach(function () {
const error = new Error((this.message = 'error message'))
error.buildId = this.buildId
this.CompileManager.doCompileWithLock = sinon this.CompileManager.doCompileWithLock = sinon
.stub() .stub()
.callsArgWith(1, new Error((this.message = 'error message')), null) .callsArgWith(1, error, null)
this.CompileController.compile(this.req, this.res) this.CompileController.compile(this.req, this.res)
}) })
@ -291,6 +302,7 @@ describe('CompileController', function () {
error: this.message, error: this.message,
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
outputFiles: [], outputFiles: [],
buildId: this.buildId,
// JSON.stringify will omit these // JSON.stringify will omit these
stats: undefined, stats: undefined,
timings: undefined, timings: undefined,
@ -320,6 +332,7 @@ describe('CompileController', function () {
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
outputFiles: [], outputFiles: [],
// JSON.stringify will omit these // JSON.stringify will omit these
buildId: undefined,
stats: undefined, stats: undefined,
timings: undefined, timings: undefined,
}, },
@ -346,6 +359,7 @@ describe('CompileController', function () {
outputUrlPrefix: '/zone/b', outputUrlPrefix: '/zone/b',
outputFiles: [], outputFiles: [],
// JSON.stringify will omit these // JSON.stringify will omit these
buildId: undefined,
stats: undefined, stats: undefined,
timings: undefined, timings: undefined,
}, },

View file

@ -35,6 +35,7 @@ describe('CompileManager', function () {
build: 1234, build: 1234,
}, },
] ]
this.buildId = 'build-id-123'
this.commandOutput = 'Dummy output' this.commandOutput = 'Dummy output'
this.compileBaseDir = '/compile/dir' this.compileBaseDir = '/compile/dir'
this.outputBaseDir = '/output/dir' this.outputBaseDir = '/output/dir'
@ -61,7 +62,9 @@ describe('CompileManager', function () {
} }
this.OutputCacheManager = { this.OutputCacheManager = {
promises: { promises: {
saveOutputFiles: sinon.stub().resolves(this.buildFiles), saveOutputFiles: sinon
.stub()
.resolves({ outputFiles: this.buildFiles, buildId: this.buildId }),
}, },
} }
this.Settings = { this.Settings = {