overleaf/services/clsi/app/coffee/OutputCacheManager.coffee

158 lines
5.7 KiB
CoffeeScript
Raw Normal View History

async = require "async"
fs = require "fs"
fse = require "fs-extra"
Path = require "path"
logger = require "logger-sharelatex"
_ = require "underscore"
Settings = require "settings-sharelatex"
OutputFileOptimiser = require "./OutputFileOptimiser"
module.exports = OutputCacheManager =
2015-02-27 08:15:28 -05:00
CACHE_SUBDIR: '.cache/clsi'
ARCHIVE_SUBDIR: '.archive/clsi'
2015-02-27 08:15:28 -05:00
BUILD_REGEX: /^[0-9a-f]+$/ # build id is Date.now() converted to hex
2015-03-02 06:31:48 -05:00
CACHE_LIMIT: 2 # maximum number of cache directories
2015-02-27 08:15:28 -05:00
CACHE_AGE: 60*60*1000 # up to one hour old
path: (buildId, file) ->
2015-02-27 08:15:28 -05:00
# 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
2015-02-27 08:15:28 -05:00
saveOutputFiles: (outputFiles, compileDir, callback = (error) ->) ->
# make a compileDir/CACHE_SUBDIR/build_id directory and
# copy all the output files into it
2015-02-27 08:15:28 -05:00
cacheRoot = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR)
# Put the files into a new cache subdirectory
buildId = Date.now().toString(16)
cacheDir = Path.join(compileDir, OutputCacheManager.CACHE_SUBDIR, buildId)
2015-02-27 08:15:28 -05:00
# let file expiry run in the background
OutputCacheManager.expireOutputFiles cacheRoot, {keep: buildId}
# Archive logs in background
2016-03-30 09:10:07 -04:00
if Settings.clsi?.archive_logs or Settings.clsi?.strace
OutputCacheManager.archiveLogs outputFiles, compileDir, (err) ->
2015-02-27 08:15:28 -05:00
if err?
logger.warn err:err, "erroring archiving log files"
2015-02-27 08:15:28 -05:00
# make the new cache directory
fse.ensureDir cacheDir, (err) ->
if err?
2015-02-27 08:15:28 -05:00
logger.error err: err, directory: cacheDir, "error creating cache directory"
callback(err, outputFiles)
else
2015-02-27 08:15:28 -05:00
# copy all the output files into the new cache directory
results = []
async.mapSeries outputFiles, (file, cb) ->
newFile = _.clone(file)
2015-02-27 08:15:28 -05:00
[src, dst] = [Path.join(compileDir, file.path), Path.join(cacheDir, file.path)]
OutputCacheManager._checkFileIsSafe src, (err, isSafe) ->
return cb(err) if err?
if !isSafe
return cb()
OutputCacheManager._checkIfShouldCopy src, (err, shouldCopy) ->
return cb(err) if err?
if !shouldCopy
return cb()
OutputCacheManager._copyFile src, dst, (err) ->
return cb(err) if err?
2015-02-27 08:15:28 -05:00
newFile.build = buildId # attach a build id if we cached the file
results.push newFile
cb()
, (err) ->
if err?
2015-02-27 08:15:28 -05:00
# pass back the original files if we encountered *any* error
callback(err, outputFiles)
else
2015-02-27 08:15:28 -05:00
# pass back the list of new files in the cache
callback(err, results)
archiveLogs: (outputFiles, compileDir, callback = (error) ->) ->
buildId = Date.now().toString(16)
archiveDir = Path.join(compileDir, OutputCacheManager.ARCHIVE_SUBDIR, buildId)
logger.log {dir: archiveDir}, "archiving log files for project"
fse.ensureDir archiveDir, (err) ->
return callback(err) if err?
async.mapSeries outputFiles, (file, cb) ->
[src, dst] = [Path.join(compileDir, file.path), Path.join(archiveDir, file.path)]
OutputCacheManager._checkFileIsSafe src, (err, isSafe) ->
return cb(err) if err?
return cb() if !isSafe
OutputCacheManager._checkIfShouldArchive src, (err, shouldArchive) ->
return cb(err) if err?
return cb() if !shouldArchive
OutputCacheManager._copyFile src, dst, cb
, callback
2015-02-27 08:15:28 -05:00
expireOutputFiles: (cacheRoot, options, callback = (error) ->) ->
# look in compileDir for build dirs and delete if > N or age of mod time > T
fs.readdir cacheRoot, (err, results) ->
if err?
2015-03-02 04:58:20 -05:00
return callback(null) if err.code == 'ENOENT' # cache directory is empty
2015-02-27 08:15:28 -05:00
logger.error err: err, project_id: cacheRoot, "error clearing cache"
return callback(err)
dirs = results.sort().reverse()
currentTime = Date.now()
isExpired = (dir, index) ->
return false if options?.keep == dir
# remove any directories over the hard limit
return true if index > OutputCacheManager.CACHE_LIMIT
# we can get the build time from the directory name
dirTime = parseInt(dir, 16)
age = currentTime - dirTime
return age > OutputCacheManager.CACHE_AGE
toRemove = _.filter(dirs, isExpired)
removeDir = (dir, cb) ->
fse.remove Path.join(cacheRoot, dir), (err, result) ->
logger.log cache: cacheRoot, dir: dir, "removed expired cache dir"
if err?
logger.error err: err, dir: dir, "cache remove error"
cb(err, result)
async.eachSeries toRemove, (dir, cb) ->
removeDir dir, cb
, callback
_checkFileIsSafe: (src, callback = (error, isSafe) ->) ->
# check if we have a valid file to copy into the cache
fs.stat src, (err, stats) ->
if err?
# some problem reading the file
logger.error err: err, file: src, "stat error for file in cache"
callback(err, false)
else if not stats.isFile()
# other filetype - reject it
logger.warn src: src, stat: stats, "nonfile output - refusing to copy to cache"
callback(null, false)
else
# it's a plain file, ok to copy
callback(null, true)
_copyFile: (src, dst, callback) ->
# copy output file into the cache
fse.copy src, dst, (err) ->
if err?
logger.error err: err, src: src, dst: dst, "copy error for file in cache"
callback(err)
else
# call the optimiser for the file too
OutputFileOptimiser.optimiseFile src, dst, callback
_checkIfShouldCopy: (src, callback = (err, shouldCopy) ->) ->
return callback(null, !Path.basename(src).match(/^strace/))
_checkIfShouldArchive: (src, callback = (err, shouldCopy) ->) ->
if Path.basename(src).match(/^strace/)
return callback(null, true)
2016-03-30 09:10:07 -04:00
if Settings.clsi?.archive_logs and Path.basename(src) in ["output.log", "output.blg"]
return callback(null, true)
return callback(null, false)