From ac4d56403abed7398c353cae96c38ccda291ea2b Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Wed, 20 Jul 2022 09:33:34 +0100 Subject: [PATCH] Merge pull request #8933 from overleaf/jpa-pdf-caching-dark [misc] enable pdf caching in dark mode for 1MB chunks GitOrigin-RevId: 1d0f7eadeff14a047548b4778d4789829bad5d48 --- services/clsi/app/js/ContentCacheManager.js | 4 -- services/clsi/app/js/OutputCacheManager.js | 45 ++++++++++++++------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/services/clsi/app/js/ContentCacheManager.js b/services/clsi/app/js/ContentCacheManager.js index f12441ffe2..758848d710 100644 --- a/services/clsi/app/js/ContentCacheManager.js +++ b/services/clsi/app/js/ContentCacheManager.js @@ -311,10 +311,6 @@ function pdfStreamHash(buffer) { async function writePdfStream(dir, hash, buffer) { const filename = Path.join(dir, hash) const atomicWriteFilename = filename + '~' - if (Settings.enablePdfCachingDark) { - // Write an empty file in dark mode. - buffer = Buffer.alloc(0) - } try { await fs.promises.writeFile(atomicWriteFilename, buffer) await fs.promises.rename(atomicWriteFilename, filename) diff --git a/services/clsi/app/js/OutputCacheManager.js b/services/clsi/app/js/OutputCacheManager.js index f18f542963..5654ef5893 100644 --- a/services/clsi/app/js/OutputCacheManager.js +++ b/services/clsi/app/js/OutputCacheManager.js @@ -178,19 +178,34 @@ module.exports = OutputCacheManager = { result, outputDir, stats, - (err, result) => { - if (err) return callback(err, result) + (err, outputFiles) => { + if (err) return callback(err, outputFiles) - if (!Settings.enablePdfCaching || !request.enablePdfCaching) { - return callback(null, result) + const enablePdfCaching = request.enablePdfCaching + const enablePdfCachingDark = + Settings.enablePdfCachingDark && !request.enablePdfCaching + if ( + !Settings.enablePdfCaching || + (!enablePdfCaching && !enablePdfCachingDark) + ) { + return callback(null, outputFiles) } OutputCacheManager.saveStreamsInContentDir( - { request, stats, timings }, - result, + { request, stats, timings, enablePdfCachingDark }, + outputFiles, compileDir, outputDir, - callback + err => { + if (err) { + logger.warn( + { err, outputDir, stats, timings }, + 'pdf caching failed' + ) + return callback(null, outputFiles) + } + callback(err, outputFiles) + } ) } ) @@ -348,7 +363,7 @@ module.exports = OutputCacheManager = { }, saveStreamsInContentDir( - { request, stats, timings }, + { request, stats, timings, enablePdfCachingDark }, outputFiles, compileDir, outputDir, @@ -357,7 +372,7 @@ module.exports = OutputCacheManager = { const cacheRoot = Path.join(outputDir, OutputCacheManager.CONTENT_SUBDIR) // check if content dir exists OutputCacheManager.ensureContentDir(cacheRoot, function (err, contentDir) { - if (err) return callback(err, outputFiles) + if (err) return callback(err) const outputFile = outputFiles.find(x => x.path === 'output.pdf') if (outputFile) { @@ -381,7 +396,7 @@ module.exports = OutputCacheManager = { if (err && err instanceof QueueLimitReachedError) { logger.warn({ err, outputDir }, 'pdf caching queue limit reached') stats['pdf-caching-queue-limit-reached'] = 1 - return callback(null, outputFiles) + return callback(null) } if (err && err instanceof TimedOutError) { logger.warn( @@ -389,9 +404,9 @@ module.exports = OutputCacheManager = { 'pdf caching timed out' ) stats['pdf-caching-timed-out'] = 1 - return callback(null, outputFiles) + return callback(null) } - if (err) return callback(err, outputFiles) + if (err) return callback(err) const [ contentRanges, newContentRanges, @@ -399,7 +414,7 @@ module.exports = OutputCacheManager = { startXRefTable, ] = result - if (Settings.enablePdfCachingDark) { + if (enablePdfCachingDark) { // In dark mode we are doing the computation only and do not emit // any ranges to the frontend. } else { @@ -420,11 +435,11 @@ module.exports = OutputCacheManager = { 0 ) stats['pdf-caching-reclaimed-space'] = reclaimedSpace - callback(null, outputFiles) + callback(null) } ) } else { - callback(null, outputFiles) + callback(null) } }) },