<commit_before>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")

checkCanStoreFiles = (callback)->
	req = {params:{}, query:{}, headers:{}}
	res = {}
	req.params.project_id = settings.health_check.project_id
	req.params.file_id = settings.health_check.file_id
	myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer(initialSize: 100)
	keyBuilder.userFileKey req, res, ->
		fileController.getFile req, myWritableStreamBuffer
		myWritableStreamBuffer.on "close", ->
			if myWritableStreamBuffer.size() > 0
				callback()
			else
				logger.err "no data in write stream buffer for health check"
				callback()

checkFileConvert = (callback)->
	imgPath = path.join(settings.path.uploadFolder, "/tiny.pdf")
	console.log imgPath, settings.path.uploadFolder
	async.waterfall [
		(cb)->
			fs.copy("./tiny.pdf", imgPath, cb)
		(cb)-> fileConverter.thumbnail imgPath, cb
		(resultPath, cb)-> fs.unlink resultPath, cb
		(cb)-> fs.unlink imgPath, cb
	], callback


isOk = true


runCheckInBackground = ->
	async.parallel [checkFileConvert, checkCanStoreFiles], (err)->
		if err? 
			logger.err err:err, "Health check: error running"
			isOk = false
		else
			isOk = true

module.exports =

	check: (req, res)->
		if isOk
			res.send 200
		else
			res.send 500
		runCheckInBackground()
<commit_msg>run health check max 1 a at a time<commit_after>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")

checkCanStoreFiles = (callback)->
	req = {params:{}, query:{}, headers:{}}
	res = {}
	req.params.project_id = settings.health_check.project_id
	req.params.file_id = settings.health_check.file_id
	myWritableStreamBuffer = new streamBuffers.WritableStreamBuffer(initialSize: 100)
	keyBuilder.userFileKey req, res, ->
		fileController.getFile req, myWritableStreamBuffer
		myWritableStreamBuffer.on "close", ->
			if myWritableStreamBuffer.size() > 0
				callback()
			else
				logger.err "no data in write stream buffer for health check"
				callback()

checkFileConvert = (callback)->
	imgPath = path.join(settings.path.uploadFolder, "/tiny.pdf")
	async.waterfall [
		(cb)->
			fs.copy("./tiny.pdf", imgPath, cb)
		(cb)-> fileConverter.thumbnail imgPath, cb
		(resultPath, cb)-> fs.unlink resultPath, cb
		(cb)-> fs.unlink imgPath, cb
	], callback


isOk = true

q = async.queue (task, callback)->
	task(callback)


runChecks = (callback)->
	async.parallel [checkFileConvert, checkCanStoreFiles], (err)->
		if err? 
			logger.err err:err, "Health check: error running"
			isOk = false
		else
			isOk = true
		callback()

module.exports =

	check: (req, res)->
		if isOk
			res.send 200
		else
			res.send 500
		q.push runChecks # run in background 1 at a time

{'fraction_non_alphanumeric': 0.1343143393863494, 'fraction_numerical': 0.0068879148403256105, 'mean_word_length': 4.461538461538462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1369533', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1016, 'n_words': 252}