2020-02-19 06:14:37 -05:00
|
|
|
let OutputCacheManager
|
2024-11-08 05:21:56 -05:00
|
|
|
const { callbackify, promisify } = require('node:util')
|
2020-02-19 06:14:37 -05:00
|
|
|
const async = require('async')
|
2024-11-08 05:21:56 -05:00
|
|
|
const fs = require('node:fs')
|
|
|
|
const Path = require('node:path')
|
2022-03-01 10:09:36 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-06-15 04:52:21 -04:00
|
|
|
const _ = require('lodash')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2024-11-08 05:21:56 -05:00
|
|
|
const crypto = require('node:crypto')
|
2021-05-13 09:07:54 -04:00
|
|
|
const Metrics = require('./Metrics')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const OutputFileOptimiser = require('./OutputFileOptimiser')
|
2021-05-13 09:07:54 -04:00
|
|
|
const ContentCacheManager = require('./ContentCacheManager')
|
2022-08-02 05:09:22 -04:00
|
|
|
const {
|
|
|
|
QueueLimitReachedError,
|
|
|
|
TimedOutError,
|
|
|
|
NoXrefTableError,
|
|
|
|
} = require('./Errors')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-11-15 07:07:17 -05:00
|
|
|
const OLDEST_BUILD_DIR = new Map()
|
|
|
|
const PENDING_PROJECT_ACTIONS = new Map()
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
doInit().catch(err => {
|
|
|
|
logger.fatal({ err }, 'low level error setting up cleanup of output dir')
|
|
|
|
// consider shutting down?
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doInit() {
|
|
|
|
await fillCache()
|
|
|
|
const oldestTimestamp = await runBulkCleanup()
|
|
|
|
scheduleBulkCleanup(oldestTimestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
function scheduleBulkCleanup(oldestTimestamp) {
|
|
|
|
const delay =
|
|
|
|
Math.max(OutputCacheManager.CACHE_AGE + oldestTimestamp - Date.now(), 0) +
|
|
|
|
60 * 1000
|
|
|
|
setTimeout(async function () {
|
|
|
|
const oldestTimestamp = await runBulkCleanup()
|
|
|
|
scheduleBulkCleanup(oldestTimestamp)
|
|
|
|
}, delay)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fillCache() {
|
|
|
|
const handle = await fs.promises.opendir(Settings.path.outputDir)
|
|
|
|
try {
|
|
|
|
for await (const { name: projectIdAndUserId } of handle) {
|
|
|
|
OLDEST_BUILD_DIR.set(
|
|
|
|
Path.join(Settings.path.outputDir, projectIdAndUserId),
|
|
|
|
// Queue them for cleanup in the next hour.
|
|
|
|
Date.now() - Math.random() * OutputCacheManager.CACHE_AGE
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
await handle.close()
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runBulkCleanup() {
|
|
|
|
const cleanupThreshold = Date.now() - OutputCacheManager.CACHE_AGE
|
|
|
|
let oldestTimestamp = Date.now()
|
|
|
|
for (const [dir, timeStamp] of OLDEST_BUILD_DIR.entries()) {
|
|
|
|
if (timeStamp < cleanupThreshold) {
|
|
|
|
await cleanupDirectory(dir, { limit: OutputCacheManager.CACHE_LIMIT })
|
|
|
|
} else if (timeStamp < oldestTimestamp) {
|
|
|
|
oldestTimestamp = timeStamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldestTimestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanupDirectory(dir, options) {
|
2024-05-22 05:37:08 -04:00
|
|
|
return await queueDirOperation(dir, async () => {
|
2021-11-15 07:07:17 -05:00
|
|
|
try {
|
|
|
|
await OutputCacheManager.promises.expireOutputFiles(dir, options)
|
|
|
|
} catch (err) {
|
|
|
|
logger.err({ dir, err }, 'cleanup of output directory failed')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function queueDirOperation(dir, fn) {
|
|
|
|
const pending = PENDING_PROJECT_ACTIONS.get(dir) || Promise.resolve()
|
|
|
|
const p = pending.then(fn, fn).finally(() => {
|
|
|
|
if (PENDING_PROJECT_ACTIONS.get(dir) === p) {
|
|
|
|
PENDING_PROJECT_ACTIONS.delete(dir)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
PENDING_PROJECT_ACTIONS.set(dir, p)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
module.exports = OutputCacheManager = {
|
2021-05-13 09:07:54 -04:00
|
|
|
CONTENT_SUBDIR: 'content',
|
2020-12-15 09:59:05 -05:00
|
|
|
CACHE_SUBDIR: 'generated-files',
|
|
|
|
ARCHIVE_SUBDIR: 'archived-logs',
|
2020-02-19 06:14:37 -05:00
|
|
|
// build id is HEXDATE-HEXRANDOM from Date.now()and RandomBytes
|
|
|
|
// for backwards compatibility, make the randombytes part optional
|
|
|
|
BUILD_REGEX: /^[0-9a-f]+(-[0-9a-f]+)?$/,
|
2021-05-13 09:56:15 -04:00
|
|
|
CONTENT_REGEX: /^[0-9a-f]+(-[0-9a-f]+)?$/,
|
2020-02-19 06:14:37 -05:00
|
|
|
CACHE_LIMIT: 2, // maximum number of cache directories
|
|
|
|
CACHE_AGE: 60 * 60 * 1000, // up to one hour old
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-11-15 07:07:17 -05:00
|
|
|
init,
|
|
|
|
queueDirOperation: callbackify(queueDirOperation),
|
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
path(buildId, file) {
|
|
|
|
// used by static server, given build id return '.cache/clsi/buildId'
|
|
|
|
if (buildId.match(OutputCacheManager.BUILD_REGEX)) {
|
|
|
|
return Path.join(OutputCacheManager.CACHE_SUBDIR, buildId, file)
|
|
|
|
} else {
|
|
|
|
// for invalid build id, return top level
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
generateBuildId(callback) {
|
|
|
|
// generate a secure build id from Date.now() and 8 random bytes in hex
|
2024-01-08 03:10:58 -05:00
|
|
|
crypto.randomBytes(8, function (err, buf) {
|
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const random = buf.toString('hex')
|
|
|
|
const date = Date.now().toString(16)
|
2024-01-08 03:10:58 -05:00
|
|
|
callback(err, `${date}-${random}`)
|
2020-02-19 06:14:37 -05:00
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-05-13 09:07:54 -04:00
|
|
|
saveOutputFiles(
|
|
|
|
{ request, stats, timings },
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
callback
|
|
|
|
) {
|
2024-01-08 03:10:58 -05:00
|
|
|
OutputCacheManager.generateBuildId(function (err, buildId) {
|
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2021-11-15 07:07:17 -05:00
|
|
|
if (!OLDEST_BUILD_DIR.has(outputDir)) {
|
|
|
|
// Register for cleanup
|
|
|
|
OLDEST_BUILD_DIR.set(outputDir, Date.now())
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputCacheManager.queueDirOperation(
|
2020-12-15 09:59:05 -05:00
|
|
|
outputDir,
|
2021-11-15 07:07:17 -05:00
|
|
|
() =>
|
|
|
|
OutputCacheManager.promises.saveOutputFilesInBuildDir(
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
buildId
|
|
|
|
),
|
2021-05-13 09:07:54 -04:00
|
|
|
function (err, result) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2021-05-13 09:07:54 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
OutputCacheManager.collectOutputPdfSize(
|
|
|
|
result,
|
|
|
|
outputDir,
|
|
|
|
stats,
|
2022-07-20 04:33:34 -04:00
|
|
|
(err, outputFiles) => {
|
2024-05-24 05:38:12 -04:00
|
|
|
if (err) return callback(err, { outputFiles, buildId })
|
2021-05-13 09:07:54 -04:00
|
|
|
|
2022-07-20 04:33:34 -04:00
|
|
|
const enablePdfCaching = request.enablePdfCaching
|
|
|
|
const enablePdfCachingDark =
|
|
|
|
Settings.enablePdfCachingDark && !request.enablePdfCaching
|
|
|
|
if (
|
|
|
|
!Settings.enablePdfCaching ||
|
|
|
|
(!enablePdfCaching && !enablePdfCachingDark)
|
|
|
|
) {
|
2024-05-24 05:38:12 -04:00
|
|
|
return callback(null, { outputFiles, buildId })
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
OutputCacheManager.saveStreamsInContentDir(
|
2022-07-20 04:33:34 -04:00
|
|
|
{ request, stats, timings, enablePdfCachingDark },
|
|
|
|
outputFiles,
|
2021-05-13 09:07:54 -04:00
|
|
|
compileDir,
|
|
|
|
outputDir,
|
2022-07-20 04:33:44 -04:00
|
|
|
(err, status) => {
|
|
|
|
Metrics.inc('pdf-caching-status', 1, {
|
|
|
|
status,
|
|
|
|
...request.metricsOpts,
|
|
|
|
})
|
2022-07-20 04:33:34 -04:00
|
|
|
if (err) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, outputDir, stats, timings },
|
|
|
|
'pdf caching failed'
|
|
|
|
)
|
2024-05-24 05:38:12 -04:00
|
|
|
return callback(null, { outputFiles, buildId })
|
2022-07-20 04:33:34 -04:00
|
|
|
}
|
2024-05-24 05:38:12 -04:00
|
|
|
callback(err, { outputFiles, buildId })
|
2022-07-20 04:33:34 -04:00
|
|
|
}
|
2021-05-13 09:07:54 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-12-15 09:59:05 -05:00
|
|
|
saveOutputFilesInBuildDir(
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
buildId,
|
|
|
|
callback
|
|
|
|
) {
|
2020-02-19 06:14:37 -05:00
|
|
|
// make a compileDir/CACHE_SUBDIR/build_id directory and
|
|
|
|
// copy all the output files into it
|
|
|
|
// Put the files into a new cache subdirectory
|
|
|
|
const cacheDir = Path.join(
|
2020-12-15 09:59:05 -05:00
|
|
|
outputDir,
|
2020-02-19 06:14:37 -05:00
|
|
|
OutputCacheManager.CACHE_SUBDIR,
|
|
|
|
buildId
|
|
|
|
)
|
|
|
|
// Is it a per-user compile? check if compile directory is PROJECTID-USERID
|
|
|
|
const perUser = Path.basename(compileDir).match(
|
|
|
|
/^[0-9a-f]{24}-[0-9a-f]{24}$/
|
|
|
|
)
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
// Archive logs in background
|
2024-01-08 03:10:58 -05:00
|
|
|
if (Settings.clsi?.archive_logs || Settings.clsi?.strace) {
|
2020-08-10 12:01:11 -04:00
|
|
|
OutputCacheManager.archiveLogs(
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
2020-12-15 09:59:05 -05:00
|
|
|
outputDir,
|
2020-08-10 12:01:11 -04:00
|
|
|
buildId,
|
|
|
|
function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2020-08-10 12:01:11 -04:00
|
|
|
return logger.warn({ err }, 'erroring archiving log files')
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
// make the new cache directory
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.mkdir(cacheDir, { recursive: true }, function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.error(
|
|
|
|
{ err, directory: cacheDir },
|
|
|
|
'error creating cache directory'
|
|
|
|
)
|
2024-01-08 03:10:58 -05:00
|
|
|
callback(err, outputFiles)
|
2020-02-19 06:14:37 -05:00
|
|
|
} else {
|
|
|
|
// copy all the output files into the new cache directory
|
|
|
|
const results = []
|
2024-01-08 05:43:41 -05:00
|
|
|
const dirCache = new Set()
|
|
|
|
dirCache.add(cacheDir)
|
2024-01-08 03:10:58 -05:00
|
|
|
async.mapSeries(
|
2020-02-19 06:14:37 -05:00
|
|
|
outputFiles,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (file, cb) {
|
2020-02-19 06:14:37 -05:00
|
|
|
// don't send dot files as output, express doesn't serve them
|
|
|
|
if (OutputCacheManager._fileIsHidden(file.path)) {
|
|
|
|
logger.debug(
|
|
|
|
{ compileDir, path: file.path },
|
|
|
|
'ignoring dotfile in output'
|
|
|
|
)
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
// copy other files into cache directory if valid
|
|
|
|
const newFile = _.clone(file)
|
2024-01-08 03:10:58 -05:00
|
|
|
const src = Path.join(compileDir, file.path)
|
|
|
|
const dst = Path.join(cacheDir, file.path)
|
2024-01-11 03:13:02 -05:00
|
|
|
OutputCacheManager._checkIfShouldCopy(
|
|
|
|
src,
|
|
|
|
function (err, shouldCopy) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
if (!shouldCopy) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
OutputCacheManager._copyFile(src, dst, dirCache, err => {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
2024-01-11 03:13:02 -05:00
|
|
|
newFile.build = buildId // attach a build id if we cached the file
|
|
|
|
results.push(newFile)
|
|
|
|
cb()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2020-08-10 12:01:11 -04:00
|
|
|
function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
// pass back the original files if we encountered *any* error
|
|
|
|
callback(err, outputFiles)
|
|
|
|
// clean up the directory we just created
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.rm(cacheDir, { force: true, recursive: true }, function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return logger.error(
|
|
|
|
{ err, dir: cacheDir },
|
|
|
|
'error removing cache dir after failure'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// pass back the list of new files in the cache
|
|
|
|
callback(err, results)
|
|
|
|
// let file expiry run in the background, expire all previous files if per-user
|
2021-11-15 07:07:17 -05:00
|
|
|
cleanupDirectory(outputDir, {
|
2020-02-19 06:14:37 -05:00
|
|
|
keep: buildId,
|
2021-07-13 07:04:48 -04:00
|
|
|
limit: perUser ? 1 : null,
|
2021-11-15 07:07:17 -05:00
|
|
|
}).catch(() => {})
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-05-13 09:07:54 -04:00
|
|
|
collectOutputPdfSize(outputFiles, outputDir, stats, callback) {
|
2021-07-13 07:04:48 -04:00
|
|
|
const outputFile = outputFiles.find(x => x.path === 'output.pdf')
|
2021-05-13 09:07:54 -04:00
|
|
|
if (!outputFile) return callback(null, outputFiles)
|
|
|
|
const outputFilePath = Path.join(
|
|
|
|
outputDir,
|
|
|
|
OutputCacheManager.path(outputFile.build, outputFile.path)
|
|
|
|
)
|
|
|
|
fs.stat(outputFilePath, (err, stat) => {
|
|
|
|
if (err) return callback(err, outputFiles)
|
|
|
|
|
|
|
|
outputFile.size = stat.size
|
|
|
|
stats['pdf-size'] = outputFile.size
|
|
|
|
callback(null, outputFiles)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
saveStreamsInContentDir(
|
2022-07-20 04:33:34 -04:00
|
|
|
{ request, stats, timings, enablePdfCachingDark },
|
2021-05-13 09:07:54 -04:00
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
const cacheRoot = Path.join(outputDir, OutputCacheManager.CONTENT_SUBDIR)
|
|
|
|
// check if content dir exists
|
|
|
|
OutputCacheManager.ensureContentDir(cacheRoot, function (err, contentDir) {
|
2022-07-20 04:33:44 -04:00
|
|
|
if (err) return callback(err, 'content-dir-unavailable')
|
2021-05-13 09:07:54 -04:00
|
|
|
|
2021-07-13 07:04:48 -04:00
|
|
|
const outputFile = outputFiles.find(x => x.path === 'output.pdf')
|
2021-05-13 09:07:54 -04:00
|
|
|
if (outputFile) {
|
|
|
|
// possibly we should copy the file from the build dir here
|
|
|
|
const outputFilePath = Path.join(
|
|
|
|
outputDir,
|
|
|
|
OutputCacheManager.path(outputFile.build, outputFile.path)
|
|
|
|
)
|
2021-05-31 04:20:25 -04:00
|
|
|
const pdfSize = outputFile.size
|
2022-03-01 10:09:55 -05:00
|
|
|
const timer = new Metrics.Timer(
|
|
|
|
'compute-pdf-ranges',
|
|
|
|
1,
|
|
|
|
request.metricsOpts
|
|
|
|
)
|
2021-05-31 04:20:25 -04:00
|
|
|
ContentCacheManager.update(
|
2022-10-25 05:24:11 -04:00
|
|
|
{
|
|
|
|
contentDir,
|
|
|
|
filePath: outputFilePath,
|
|
|
|
pdfSize,
|
|
|
|
pdfCachingMinChunkSize: request.pdfCachingMinChunkSize,
|
|
|
|
compileTime: timings.compile,
|
|
|
|
},
|
2021-05-31 04:20:25 -04:00
|
|
|
function (err, result) {
|
2022-08-02 05:09:22 -04:00
|
|
|
if (err && err instanceof NoXrefTableError) {
|
|
|
|
return callback(null, err.message)
|
|
|
|
}
|
2021-08-24 05:22:30 -04:00
|
|
|
if (err && err instanceof QueueLimitReachedError) {
|
|
|
|
logger.warn({ err, outputDir }, 'pdf caching queue limit reached')
|
|
|
|
stats['pdf-caching-queue-limit-reached'] = 1
|
2022-07-20 04:33:44 -04:00
|
|
|
return callback(null, 'queue-limit')
|
2021-08-24 05:22:30 -04:00
|
|
|
}
|
2021-06-23 09:14:28 -04:00
|
|
|
if (err && err instanceof TimedOutError) {
|
|
|
|
logger.warn(
|
|
|
|
{ err, outputDir, stats, timings },
|
|
|
|
'pdf caching timed out'
|
|
|
|
)
|
|
|
|
stats['pdf-caching-timed-out'] = 1
|
2022-07-20 04:33:44 -04:00
|
|
|
return callback(null, 'timed-out')
|
2021-06-23 09:14:28 -04:00
|
|
|
}
|
2022-07-20 04:33:44 -04:00
|
|
|
if (err) return callback(err, 'failed')
|
2022-07-20 10:17:41 -04:00
|
|
|
const {
|
2022-07-15 04:03:40 -04:00
|
|
|
contentRanges,
|
|
|
|
newContentRanges,
|
|
|
|
reclaimedSpace,
|
2022-11-01 10:50:02 -04:00
|
|
|
overheadDeleteStaleHashes,
|
|
|
|
timedOutErr,
|
2022-07-15 04:03:40 -04:00
|
|
|
startXRefTable,
|
2022-07-20 10:17:41 -04:00
|
|
|
} = result
|
2021-05-13 09:07:54 -04:00
|
|
|
|
2022-11-01 10:50:02 -04:00
|
|
|
let status = 'success'
|
|
|
|
if (timedOutErr) {
|
|
|
|
// Soft failure: let the frontend use partial set of ranges.
|
|
|
|
logger.warn(
|
|
|
|
{
|
|
|
|
err: timedOutErr,
|
|
|
|
overheadDeleteStaleHashes,
|
|
|
|
outputDir,
|
|
|
|
stats,
|
|
|
|
timings,
|
|
|
|
},
|
|
|
|
'pdf caching timed out - soft failure'
|
|
|
|
)
|
|
|
|
stats['pdf-caching-timed-out'] = 1
|
|
|
|
status = 'timed-out-soft-failure'
|
|
|
|
}
|
|
|
|
|
2022-07-20 04:33:34 -04:00
|
|
|
if (enablePdfCachingDark) {
|
2021-05-31 04:20:25 -04:00
|
|
|
// In dark mode we are doing the computation only and do not emit
|
|
|
|
// any ranges to the frontend.
|
|
|
|
} else {
|
|
|
|
outputFile.contentId = Path.basename(contentDir)
|
|
|
|
outputFile.ranges = contentRanges
|
2022-07-15 04:03:40 -04:00
|
|
|
outputFile.startXRefTable = startXRefTable
|
2021-05-31 04:20:25 -04:00
|
|
|
}
|
2021-05-13 09:07:54 -04:00
|
|
|
|
2021-05-31 04:20:25 -04:00
|
|
|
timings['compute-pdf-caching'] = timer.done()
|
|
|
|
stats['pdf-caching-n-ranges'] = contentRanges.length
|
|
|
|
stats['pdf-caching-total-ranges-size'] = contentRanges.reduce(
|
|
|
|
(sum, next) => sum + (next.end - next.start),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
stats['pdf-caching-n-new-ranges'] = newContentRanges.length
|
|
|
|
stats['pdf-caching-new-ranges-size'] = newContentRanges.reduce(
|
|
|
|
(sum, next) => sum + (next.end - next.start),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
stats['pdf-caching-reclaimed-space'] = reclaimedSpace
|
2022-11-01 10:50:02 -04:00
|
|
|
timings['pdf-caching-overhead-delete-stale-hashes'] =
|
|
|
|
overheadDeleteStaleHashes
|
|
|
|
callback(null, status)
|
2021-05-31 04:20:25 -04:00
|
|
|
}
|
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
} else {
|
2022-07-20 04:33:44 -04:00
|
|
|
callback(null, 'missing-pdf')
|
2021-05-13 09:07:54 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureContentDir(contentRoot, callback) {
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.mkdir(contentRoot, { recursive: true }, function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2021-05-13 09:07:54 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
fs.readdir(contentRoot, function (err, results) {
|
2021-10-27 05:49:18 -04:00
|
|
|
if (err) return callback(err)
|
2021-05-13 09:07:54 -04:00
|
|
|
const dirs = results.sort()
|
2021-07-13 07:04:48 -04:00
|
|
|
const contentId = dirs.find(dir =>
|
2021-05-13 09:07:54 -04:00
|
|
|
OutputCacheManager.BUILD_REGEX.test(dir)
|
|
|
|
)
|
|
|
|
if (contentId) {
|
|
|
|
callback(null, Path.join(contentRoot, contentId))
|
|
|
|
} else {
|
|
|
|
// make a content directory
|
|
|
|
OutputCacheManager.generateBuildId(function (err, contentId) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const contentDir = Path.join(contentRoot, contentId)
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.mkdir(contentDir, { recursive: true }, function (err) {
|
2021-05-13 09:07:54 -04:00
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2024-01-08 03:10:58 -05:00
|
|
|
callback(null, contentDir)
|
2021-05-13 09:07:54 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-12-15 09:59:05 -05:00
|
|
|
archiveLogs(outputFiles, compileDir, outputDir, buildId, callback) {
|
2020-02-19 06:14:37 -05:00
|
|
|
const archiveDir = Path.join(
|
2020-12-15 09:59:05 -05:00
|
|
|
outputDir,
|
2020-02-19 06:14:37 -05:00
|
|
|
OutputCacheManager.ARCHIVE_SUBDIR,
|
|
|
|
buildId
|
|
|
|
)
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ dir: archiveDir }, 'archiving log files for project')
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.mkdir(archiveDir, { recursive: true }, function (err) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2024-01-08 05:43:41 -05:00
|
|
|
const dirCache = new Set()
|
|
|
|
dirCache.add(archiveDir)
|
2024-01-08 03:10:58 -05:00
|
|
|
async.mapSeries(
|
2020-02-19 06:14:37 -05:00
|
|
|
outputFiles,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (file, cb) {
|
2024-01-08 03:10:58 -05:00
|
|
|
const src = Path.join(compileDir, file.path)
|
|
|
|
const dst = Path.join(archiveDir, file.path)
|
2024-01-11 03:13:02 -05:00
|
|
|
OutputCacheManager._checkIfShouldArchive(
|
|
|
|
src,
|
|
|
|
function (err, shouldArchive) {
|
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
2024-01-08 03:10:58 -05:00
|
|
|
}
|
2024-01-11 03:13:02 -05:00
|
|
|
if (!shouldArchive) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
OutputCacheManager._copyFile(src, dst, dirCache, cb)
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-11-15 07:07:17 -05:00
|
|
|
expireOutputFiles(outputDir, options, callback) {
|
2020-02-19 06:14:37 -05:00
|
|
|
// look in compileDir for build dirs and delete if > N or age of mod time > T
|
2021-11-15 07:07:17 -05:00
|
|
|
const cleanupAll = cb => {
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.rm(outputDir, { force: true, recursive: true }, err => {
|
2021-11-15 07:07:17 -05:00
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
// Drop reference after successful cleanup of the output dir.
|
|
|
|
OLDEST_BUILD_DIR.delete(outputDir)
|
|
|
|
cb(null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const cacheRoot = Path.join(outputDir, OutputCacheManager.CACHE_SUBDIR)
|
2024-01-08 03:10:58 -05:00
|
|
|
fs.readdir(cacheRoot, function (err, results) {
|
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err.code === 'ENOENT') {
|
2021-11-15 07:07:17 -05:00
|
|
|
// cache directory is empty
|
|
|
|
return cleanupAll(callback)
|
|
|
|
}
|
2023-03-22 07:52:39 -04:00
|
|
|
logger.error({ err, projectId: cacheRoot }, 'error clearing cache')
|
2020-02-19 06:14:37 -05:00
|
|
|
return callback(err)
|
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const dirs = results.sort().reverse()
|
|
|
|
const currentTime = Date.now()
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-11-15 07:07:17 -05:00
|
|
|
let oldestDirTimeToKeep = 0
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const isExpired = function (dir, index) {
|
2024-01-08 03:10:58 -05:00
|
|
|
if (options?.keep === dir) {
|
2021-11-15 07:07:17 -05:00
|
|
|
// This is the directory we just created for the compile request.
|
|
|
|
oldestDirTimeToKeep = currentTime
|
2020-02-19 06:14:37 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
// remove any directories over the requested (non-null) limit
|
2024-01-08 03:10:58 -05:00
|
|
|
if (options?.limit != null && index > options.limit) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// remove any directories over the hard limit
|
|
|
|
if (index > OutputCacheManager.CACHE_LIMIT) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// we can get the build time from the first part of the directory name DDDD-RRRR
|
|
|
|
// DDDD is date and RRRR is random bytes
|
2024-01-08 03:10:58 -05:00
|
|
|
const dirTime = parseInt(dir.split('-')[0], 16)
|
2020-02-19 06:14:37 -05:00
|
|
|
const age = currentTime - dirTime
|
2021-11-15 07:07:17 -05:00
|
|
|
const expired = age > OutputCacheManager.CACHE_AGE
|
|
|
|
if (expired) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
oldestDirTimeToKeep = dirTime
|
|
|
|
return false
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const toRemove = _.filter(dirs, isExpired)
|
2021-11-15 07:07:17 -05:00
|
|
|
if (toRemove.length === dirs.length) {
|
|
|
|
// No builds left after cleanup.
|
|
|
|
return cleanupAll(callback)
|
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const removeDir = (dir, cb) =>
|
2024-01-08 05:43:41 -05:00
|
|
|
fs.rm(
|
|
|
|
Path.join(cacheRoot, dir),
|
|
|
|
{ force: true, recursive: true },
|
|
|
|
function (err, result) {
|
|
|
|
logger.debug({ cache: cacheRoot, dir }, 'removed expired cache dir')
|
|
|
|
if (err) {
|
|
|
|
logger.error({ err, dir }, 'cache remove error')
|
|
|
|
}
|
|
|
|
cb(err, result)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2024-01-08 05:43:41 -05:00
|
|
|
)
|
2024-01-08 03:10:58 -05:00
|
|
|
async.eachSeries(
|
2020-02-19 06:14:37 -05:00
|
|
|
toRemove,
|
|
|
|
(dir, cb) => removeDir(dir, cb),
|
2021-11-15 07:07:17 -05:00
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
// On error: keep the timestamp in the past.
|
|
|
|
// The next iteration of the cleanup loop will retry the deletion.
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
// On success: push the timestamp into the future.
|
|
|
|
OLDEST_BUILD_DIR.set(outputDir, oldestDirTimeToKeep)
|
|
|
|
callback(null)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_fileIsHidden(path) {
|
2024-01-08 03:10:58 -05:00
|
|
|
return path?.match(/^\.|\/\./) != null
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2024-01-08 05:43:41 -05:00
|
|
|
_ensureParentExists(dst, dirCache, callback) {
|
|
|
|
let parent = Path.dirname(dst)
|
|
|
|
if (dirCache.has(parent)) {
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
fs.mkdir(parent, { recursive: true }, err => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
while (!dirCache.has(parent)) {
|
|
|
|
dirCache.add(parent)
|
|
|
|
parent = Path.dirname(parent)
|
|
|
|
}
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_copyFile(src, dst, dirCache, callback) {
|
|
|
|
OutputCacheManager._ensureParentExists(dst, dirCache, err => {
|
|
|
|
if (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.warn(
|
2024-01-08 05:43:41 -05:00
|
|
|
{ err, dst },
|
|
|
|
'creating parent directory in output cache failed'
|
2020-02-19 06:14:37 -05:00
|
|
|
)
|
2024-01-08 05:43:41 -05:00
|
|
|
return callback(err, false)
|
|
|
|
}
|
|
|
|
// copy output file into the cache
|
|
|
|
fs.copyFile(src, dst, function (err) {
|
|
|
|
if (err?.code === 'ENOENT') {
|
|
|
|
logger.warn(
|
|
|
|
{ err, file: src },
|
|
|
|
'file has disappeared when copying to build cache'
|
|
|
|
)
|
|
|
|
callback(err, false)
|
|
|
|
} else if (err) {
|
|
|
|
logger.error({ err, src, dst }, 'copy error for file in cache')
|
|
|
|
callback(err)
|
2020-02-19 06:14:37 -05:00
|
|
|
} else {
|
2024-01-08 05:43:41 -05:00
|
|
|
if (Settings.clsi?.optimiseInDocker) {
|
|
|
|
// don't run any optimisations on the pdf when they are done
|
|
|
|
// in the docker container
|
|
|
|
callback()
|
|
|
|
} else {
|
|
|
|
// call the optimiser for the file too
|
|
|
|
OutputFileOptimiser.optimiseFile(src, dst, callback)
|
|
|
|
}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2024-01-08 05:43:41 -05:00
|
|
|
})
|
2020-02-19 06:14:37 -05:00
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_checkIfShouldCopy(src, callback) {
|
2024-01-08 03:10:58 -05:00
|
|
|
callback(null, !Path.basename(src).match(/^strace/))
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_checkIfShouldArchive(src, callback) {
|
|
|
|
if (Path.basename(src).match(/^strace/)) {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
2024-01-08 03:10:58 -05:00
|
|
|
const basename = Path.basename(src)
|
2020-02-19 06:14:37 -05:00
|
|
|
if (
|
2024-01-08 03:10:58 -05:00
|
|
|
Settings.clsi?.archive_logs &&
|
|
|
|
['output.log', 'output.blg'].includes(basename)
|
2020-02-19 06:14:37 -05:00
|
|
|
) {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
2024-01-08 03:10:58 -05:00
|
|
|
callback(null, false)
|
2021-07-13 07:04:48 -04:00
|
|
|
},
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-11-15 07:07:17 -05:00
|
|
|
OutputCacheManager.promises = {
|
|
|
|
expireOutputFiles: promisify(OutputCacheManager.expireOutputFiles),
|
2022-07-07 08:27:20 -04:00
|
|
|
saveOutputFiles: promisify(OutputCacheManager.saveOutputFiles),
|
2021-11-15 07:07:17 -05:00
|
|
|
saveOutputFilesInBuildDir: promisify(
|
|
|
|
OutputCacheManager.saveOutputFilesInBuildDir
|
|
|
|
),
|
|
|
|
}
|