overleaf/services/filestore/app/coffee/HealthCheckController.coffee

56 lines
1.6 KiB
CoffeeScript
Raw Normal View History

2016-12-14 17:14:15 +00:00
fs = require("fs-extra")
path = require("path")
async = require("async")
fileConverter = require("./FileConverter")
keyBuilder = require("./KeyBuilder")
fileController = require("./FileController")
logger = require('logger-sharelatex')
settings = require("settings-sharelatex")
streamBuffers = require("stream-buffers")
2017-03-08 14:59:34 +00:00
_ = require('underscore')
2016-12-14 17:14:15 +00:00
checkCanStoreFiles = (callback)->
2017-03-08 14:59:34 +00:00
callback = _.once(callback)
2016-12-14 17:14:15 +00:00
req = {params:{}, query:{}, headers:{}}
req.params.project_id = settings.health_check.project_id
req.params.file_id = settings.health_check.file_id
myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer(initialSize: 100)
2017-03-08 14:59:34 +00:00
res = {
send: (code) ->
if code != 200
callback(new Error("non-200 code from getFile: #{code}"))
}
myWritableStreamBuffer.send = res.send
2016-12-14 17:14:15 +00:00
keyBuilder.userFileKey req, res, ->
fileController.getFile req, myWritableStreamBuffer
myWritableStreamBuffer.on "close", ->
if myWritableStreamBuffer.size() > 0
callback()
else
2017-03-08 14:59:34 +00:00
err = "no data in write stream buffer for health check"
logger.err {err,}, "error performing health check"
callback(err)
2016-12-14 17:14:15 +00:00
checkFileConvert = (callback)->
imgPath = path.join(settings.path.uploadFolder, "/tiny.pdf")
async.waterfall [
(cb)->
fs.copy("./tiny.pdf", imgPath, cb)
2016-12-14 18:11:28 +00:00
(cb)-> fileConverter.thumbnail imgPath, cb
2016-12-14 17:14:15 +00:00
(resultPath, cb)-> fs.unlink resultPath, cb
2016-12-14 18:11:28 +00:00
(cb)-> fs.unlink imgPath, cb
2016-12-14 17:14:15 +00:00
], callback
module.exports =
2017-03-08 14:59:34 +00:00
check: (req, res) ->
logger.log {}, "performing health check"
async.parallel [checkFileConvert, checkCanStoreFiles], (err)->
if err?
logger.err err:err, "Health check: error running"
res.send 500
else
res.send 200