<commit_before>/* eslint-disable
    camelcase,
    handle-callback-err,
    no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS101: Remove unnecessary use of Array.from
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
let CompileController
const RequestParser = require('./RequestParser')
const CompileManager = require('./CompileManager')
const Settings = require('settings-sharelatex')
const Metrics = require('./Metrics')
const ProjectPersistenceManager = require('./ProjectPersistenceManager')
const logger = require('logger-sharelatex')
const Errors = require('./Errors')

module.exports = CompileController = {
  compile(req, res, next) {
    if (next == null) {
      next = function(error) {}
    }
    const timer = new Metrics.Timer('compile-request')
    return RequestParser.parse(req.body, function(error, request) {
      if (error != null) {
        return next(error)
      }
      request.project_id = req.params.project_id
      if (req.params.user_id != null) {
        request.user_id = req.params.user_id
      }
      return ProjectPersistenceManager.markProjectAsJustAccessed(
        request.project_id,
        function(error) {
          if (error != null) {
            return next(error)
          }
          return CompileManager.doCompileWithLock(request, function(
            error,
            outputFiles
          ) {
            let code, status
            if (outputFiles == null) {
              outputFiles = []
            }
            if (error instanceof Errors.AlreadyCompilingError) {
              code = 423 // Http 423 Locked
              status = 'compile-in-progress'
            } else if (error instanceof Errors.FilesOutOfSyncError) {
              code = 409 // Http 409 Conflict
              status = 'retry'
            } else if (error != null ? error.terminated : undefined) {
              status = 'terminated'
            } else if (error != null ? error.validate : undefined) {
              status = `validation-${error.validate}`
            } else if (error != null ? error.timedout : undefined) {
              status = 'timedout'
              logger.log(
                { err: error, project_id: request.project_id },
                'timeout running compile'
              )
            } else if (error != null) {
              status = 'error'
              code = 500
              logger.warn(
                { err: error, project_id: request.project_id },
                'error running compile'
              )
            } else {
              let file
              status = 'failure'
              for (file of Array.from(outputFiles)) {
                if (
                  file.path != null
                    ? file.path.match(/output\.pdf$/)
                    : undefined
                ) {
                  status = 'success'<commit_msg>handle EPIPE errors in CompileController<commit_after>/* eslint-disable
    camelcase,
    handle-callback-err,
    no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
 * decaffeinate suggestions:
 * DS101: Remove unnecessary use of Array.from
 * DS102: Remove unnecessary code created because of implicit returns
 * DS207: Consider shorter variations of null checks
 * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
 */
let CompileController
const RequestParser = require('./RequestParser')
const CompileManager = require('./CompileManager')
const Settings = require('settings-sharelatex')
const Metrics = require('./Metrics')
const ProjectPersistenceManager = require('./ProjectPersistenceManager')
const logger = require('logger-sharelatex')
const Errors = require('./Errors')

module.exports = CompileController = {
  compile(req, res, next) {
    if (next == null) {
      next = function(error) {}
    }
    const timer = new Metrics.Timer('compile-request')
    return RequestParser.parse(req.body, function(error, request) {
      if (error != null) {
        return next(error)
      }
      request.project_id = req.params.project_id
      if (req.params.user_id != null) {
        request.user_id = req.params.user_id
      }
      return ProjectPersistenceManager.markProjectAsJustAccessed(
        request.project_id,
        function(error) {
          if (error != null) {
            return next(error)
          }
          return CompileManager.doCompileWithLock(request, function(
            error,
            outputFiles
          ) {
            let code, status
            if (outputFiles == null) {
              outputFiles = []
            }
            if (error instanceof Errors.AlreadyCompilingError) {
              code = 423 // Http 423 Locked
              status = 'compile-in-progress'
            } else if (error instanceof Errors.FilesOutOfSyncError) {
              code = 409 // Http 409 Conflict
              status = 'retry'
            } else if (error && error.code === 'EPIPE') {
              // docker returns EPIPE when shutting down
              code = 503 // send 503 Unavailable response
              status = 'unavailable'
            } else if (error != null ? error.terminated : undefined) {
              status = 'terminated'
            } else if (error != null ? error.validate : undefined) {
              status = `validation-${error.validate}`
            } else if (error != null ? error.timedout : undefined) {
              status = 'timedout'
              logger.log(
                { err: error, project_id: request.project_id },
                'timeout running compile'
              )
            } else if (error != null) {
              status = 'error'
              code = 500
              logger.warn(
                { err: error, project_id: request.project_id },
                'error running compile'
              )
            } else {
              let file
              status = 'failure'
              for (file of Array.from(outputFiles)) {
                if (
                  file.path != null
                    ? file.path.match(/output\.pdf$/)
                    : undefined
                ) {
                  status = 'success'
{'fraction_non_alphanumeric': 0.101411071483951, 'fraction_numerical': 0.008373391223445496, 'mean_word_length': 1.8153644696639022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '5551558', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1623, 'n_words': 436}