2020-02-19 06:14:28 -05:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:14:14 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS104: Avoid inline assignments
|
|
|
|
* DS204: Change includes calls to have a more natural evaluation order
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 06:14:37 -05:00
|
|
|
let OutputCacheManager
|
|
|
|
const async = require('async')
|
|
|
|
const fs = require('fs')
|
|
|
|
const fse = require('fs-extra')
|
|
|
|
const Path = require('path')
|
|
|
|
const logger = require('logger-sharelatex')
|
2020-06-15 04:52:21 -04:00
|
|
|
const _ = require('lodash')
|
2021-07-12 12:47:21 -04:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-19 06:14:37 -05:00
|
|
|
const crypto = require('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')
|
2021-06-23 09:14:28 -04:00
|
|
|
const { TimedOutError } = require('./Errors')
|
2020-02-19 06:14:14 -05:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, buildId) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return crypto.randomBytes(8, function (err, buf) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
const random = buf.toString('hex')
|
|
|
|
const date = Date.now().toString(16)
|
|
|
|
return callback(err, `${date}-${random}`)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2021-05-13 09:07:54 -04:00
|
|
|
saveOutputFiles(
|
|
|
|
{ request, stats, timings },
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
callback
|
|
|
|
) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return OutputCacheManager.generateBuildId(function (err, buildId) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return OutputCacheManager.saveOutputFilesInBuildDir(
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
2020-12-15 09:59:05 -05:00
|
|
|
outputDir,
|
2020-02-19 06:14:37 -05:00
|
|
|
buildId,
|
2021-05-13 09:07:54 -04:00
|
|
|
function (err, result) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
OutputCacheManager.collectOutputPdfSize(
|
|
|
|
result,
|
|
|
|
outputDir,
|
|
|
|
stats,
|
|
|
|
(err, result) => {
|
|
|
|
if (err) return callback(err, result)
|
|
|
|
|
|
|
|
if (!Settings.enablePdfCaching || !request.enablePdfCaching) {
|
|
|
|
return callback(null, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputCacheManager.saveStreamsInContentDir(
|
|
|
|
{ stats, timings },
|
|
|
|
result,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
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
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-12-15 09:59:05 -05:00
|
|
|
const cacheRoot = Path.join(outputDir, OutputCacheManager.CACHE_SUBDIR)
|
2020-02-19 06:14:37 -05:00
|
|
|
// 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
|
|
|
|
if (
|
|
|
|
(Settings.clsi != null ? Settings.clsi.archive_logs : undefined) ||
|
|
|
|
(Settings.clsi != null ? Settings.clsi.strace : undefined)
|
|
|
|
) {
|
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) {
|
|
|
|
if (err != null) {
|
|
|
|
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
|
2020-08-10 12:01:11 -04:00
|
|
|
return fse.ensureDir(cacheDir, function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
logger.error(
|
|
|
|
{ err, directory: cacheDir },
|
|
|
|
'error creating cache directory'
|
|
|
|
)
|
|
|
|
return callback(err, outputFiles)
|
|
|
|
} else {
|
|
|
|
// copy all the output files into the new cache directory
|
|
|
|
const results = []
|
|
|
|
return async.mapSeries(
|
|
|
|
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)
|
|
|
|
const [src, dst] = Array.from([
|
|
|
|
Path.join(compileDir, file.path),
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join(cacheDir, file.path),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
2021-07-13 07:04:48 -04:00
|
|
|
return OutputCacheManager._checkFileIsSafe(
|
|
|
|
src,
|
|
|
|
function (err, isSafe) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
if (!isSafe) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return cb()
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return OutputCacheManager._checkIfShouldCopy(
|
|
|
|
src,
|
|
|
|
function (err, shouldCopy) {
|
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
if (!shouldCopy) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
return OutputCacheManager._copyFile(
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
newFile.build = buildId // attach a build id if we cached the file
|
|
|
|
results.push(newFile)
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
2020-08-10 12:01:11 -04:00
|
|
|
function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
// pass back the original files if we encountered *any* error
|
|
|
|
callback(err, outputFiles)
|
|
|
|
// clean up the directory we just created
|
2020-08-10 12:01:11 -04:00
|
|
|
return fse.remove(cacheDir, function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
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
|
|
|
|
return OutputCacheManager.expireOutputFiles(cacheRoot, {
|
|
|
|
keep: buildId,
|
2021-07-13 07:04:48 -04:00
|
|
|
limit: perUser ? 1 : null,
|
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(
|
|
|
|
{ stats, timings },
|
|
|
|
outputFiles,
|
|
|
|
compileDir,
|
|
|
|
outputDir,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
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)
|
|
|
|
|
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
|
2021-05-13 09:07:54 -04:00
|
|
|
const timer = new Metrics.Timer('compute-pdf-ranges')
|
2021-05-31 04:20:25 -04:00
|
|
|
ContentCacheManager.update(
|
|
|
|
contentDir,
|
|
|
|
outputFilePath,
|
|
|
|
pdfSize,
|
2021-06-23 09:14:28 -04:00
|
|
|
timings.compile,
|
2021-05-31 04:20:25 -04:00
|
|
|
function (err, result) {
|
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
|
|
|
|
return callback(null, outputFiles)
|
|
|
|
}
|
2021-05-31 04:20:25 -04:00
|
|
|
if (err) return callback(err, outputFiles)
|
|
|
|
const [contentRanges, newContentRanges, reclaimedSpace] = result
|
2021-05-13 09:07:54 -04:00
|
|
|
|
2021-05-31 04:20:25 -04:00
|
|
|
if (Settings.enablePdfCachingDark) {
|
|
|
|
// 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
|
|
|
|
}
|
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
|
|
|
|
callback(null, outputFiles)
|
|
|
|
}
|
|
|
|
)
|
2021-05-13 09:07:54 -04:00
|
|
|
} else {
|
|
|
|
callback(null, outputFiles)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureContentDir(contentRoot, callback) {
|
|
|
|
fse.ensureDir(contentRoot, function (err) {
|
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
fs.readdir(contentRoot, function (err, results) {
|
|
|
|
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)
|
|
|
|
fse.ensureDir(contentDir, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return callback(null, contentDir)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2020-12-15 09:59:05 -05:00
|
|
|
archiveLogs(outputFiles, compileDir, outputDir, buildId, callback) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
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
|
|
|
|
)
|
|
|
|
logger.log({ dir: archiveDir }, 'archiving log files for project')
|
2020-08-10 12:01:11 -04:00
|
|
|
return fse.ensureDir(archiveDir, function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return async.mapSeries(
|
|
|
|
outputFiles,
|
2020-08-10 12:01:11 -04:00
|
|
|
function (file, cb) {
|
2020-02-19 06:14:37 -05:00
|
|
|
const [src, dst] = Array.from([
|
|
|
|
Path.join(compileDir, file.path),
|
2021-07-13 07:04:48 -04:00
|
|
|
Path.join(archiveDir, file.path),
|
2020-02-19 06:14:37 -05:00
|
|
|
])
|
2021-07-13 07:04:48 -04:00
|
|
|
return OutputCacheManager._checkFileIsSafe(
|
|
|
|
src,
|
|
|
|
function (err, isSafe) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
if (!isSafe) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return cb()
|
|
|
|
}
|
2021-07-13 07:04:48 -04:00
|
|
|
return OutputCacheManager._checkIfShouldArchive(
|
|
|
|
src,
|
|
|
|
function (err, shouldArchive) {
|
|
|
|
if (err != null) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
if (!shouldArchive) {
|
|
|
|
return cb()
|
|
|
|
}
|
|
|
|
return OutputCacheManager._copyFile(src, dst, cb)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2020-02-19 06:14:37 -05:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
expireOutputFiles(cacheRoot, options, callback) {
|
|
|
|
// look in compileDir for build dirs and delete if > N or age of mod time > T
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return fs.readdir(cacheRoot, function (err, results) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if (err != null) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return callback(null)
|
|
|
|
} // cache directory is empty
|
|
|
|
logger.error({ err, project_id: cacheRoot }, 'error clearing cache')
|
|
|
|
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
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
const isExpired = function (dir, index) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if ((options != null ? options.keep : undefined) === dir) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// remove any directories over the requested (non-null) limit
|
|
|
|
if (
|
|
|
|
(options != null ? options.limit : undefined) != null &&
|
|
|
|
index > options.limit
|
|
|
|
) {
|
|
|
|
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
|
|
|
|
const dirTime = parseInt(
|
2021-07-13 07:04:48 -04:00
|
|
|
__guard__(dir.split('-'), x => x[0]),
|
2020-02-19 06:14:37 -05:00
|
|
|
16
|
|
|
|
)
|
|
|
|
const age = currentTime - dirTime
|
|
|
|
return age > OutputCacheManager.CACHE_AGE
|
|
|
|
}
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const toRemove = _.filter(dirs, isExpired)
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
const removeDir = (dir, cb) =>
|
2020-08-10 12:01:11 -04:00
|
|
|
fse.remove(Path.join(cacheRoot, dir), function (err, result) {
|
2020-02-19 06:14:37 -05:00
|
|
|
logger.log({ cache: cacheRoot, dir }, 'removed expired cache dir')
|
|
|
|
if (err != null) {
|
|
|
|
logger.error({ err, dir }, 'cache remove error')
|
|
|
|
}
|
|
|
|
return cb(err, result)
|
|
|
|
})
|
|
|
|
return async.eachSeries(
|
|
|
|
toRemove,
|
|
|
|
(dir, cb) => removeDir(dir, cb),
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_fileIsHidden(path) {
|
|
|
|
return (path != null ? path.match(/^\.|\/\./) : undefined) != null
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_checkFileIsSafe(src, callback) {
|
|
|
|
// check if we have a valid file to copy into the cache
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (error, isSafe) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
2020-08-10 12:01:11 -04:00
|
|
|
return fs.stat(src, function (err, stats) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if ((err != null ? err.code : undefined) === 'ENOENT') {
|
|
|
|
logger.warn(
|
|
|
|
{ err, file: src },
|
|
|
|
'file has disappeared before copying to build cache'
|
|
|
|
)
|
|
|
|
return callback(err, false)
|
|
|
|
} else if (err != null) {
|
|
|
|
// some other problem reading the file
|
|
|
|
logger.error({ err, file: src }, 'stat error for file in cache')
|
|
|
|
return callback(err, false)
|
|
|
|
} else if (!stats.isFile()) {
|
|
|
|
// other filetype - reject it
|
|
|
|
logger.warn(
|
|
|
|
{ src, stat: stats },
|
|
|
|
'nonfile output - refusing to copy to cache'
|
|
|
|
)
|
|
|
|
return callback(null, false)
|
|
|
|
} else {
|
|
|
|
// it's a plain file, ok to copy
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_copyFile(src, dst, callback) {
|
|
|
|
// copy output file into the cache
|
2020-08-10 12:01:11 -04:00
|
|
|
return fse.copy(src, dst, function (err) {
|
2020-02-19 06:14:37 -05:00
|
|
|
if ((err != null ? err.code : undefined) === 'ENOENT') {
|
|
|
|
logger.warn(
|
|
|
|
{ err, file: src },
|
|
|
|
'file has disappeared when copying to build cache'
|
|
|
|
)
|
|
|
|
return callback(err, false)
|
|
|
|
} else if (err != null) {
|
|
|
|
logger.error({ err, src, dst }, 'copy error for file in cache')
|
|
|
|
return callback(err)
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
Settings.clsi != null ? Settings.clsi.optimiseInDocker : undefined
|
|
|
|
) {
|
|
|
|
// don't run any optimisations on the pdf when they are done
|
|
|
|
// in the docker container
|
|
|
|
return callback()
|
|
|
|
} else {
|
|
|
|
// call the optimiser for the file too
|
|
|
|
return OutputFileOptimiser.optimiseFile(src, dst, callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_checkIfShouldCopy(src, callback) {
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (err, shouldCopy) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
return callback(null, !Path.basename(src).match(/^strace/))
|
|
|
|
},
|
2020-02-19 06:14:14 -05:00
|
|
|
|
2020-02-19 06:14:37 -05:00
|
|
|
_checkIfShouldArchive(src, callback) {
|
|
|
|
let needle
|
|
|
|
if (callback == null) {
|
2020-08-10 12:01:11 -04:00
|
|
|
callback = function (err, shouldCopy) {}
|
2020-02-19 06:14:37 -05:00
|
|
|
}
|
|
|
|
if (Path.basename(src).match(/^strace/)) {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(Settings.clsi != null ? Settings.clsi.archive_logs : undefined) &&
|
|
|
|
((needle = Path.basename(src)),
|
|
|
|
['output.log', 'output.blg'].includes(needle))
|
|
|
|
) {
|
|
|
|
return callback(null, true)
|
|
|
|
}
|
|
|
|
return 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
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
2020-02-19 06:14:37 -05:00
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|