HealthCheckController.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // TODO: This file was created by bulk-decaffeinate.
  2. // Sanity-check the conversion and remove this comment.
  3. /*
  4. * decaffeinate suggestions:
  5. * DS102: Remove unnecessary code created because of implicit returns
  6. * DS103: Rewrite code to no longer use __guard__
  7. * DS207: Consider shorter variations of null checks
  8. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  9. */
  10. const request = require('request')
  11. const logger = require('logger-sharelatex')
  12. const settings = require('settings-sharelatex')
  13. module.exports = {
  14. healthCheck(req, res) {
  15. const opts = {
  16. url: `http://localhost:3005/user/${settings.healthCheckUserId}/check`,
  17. json: {
  18. words: ['helllo'],
  19. language: 'en'
  20. },
  21. timeout: 1000 * 20
  22. }
  23. return request.post(opts, function(err, response, body) {
  24. if (err != null) {
  25. return res.sendStatus(500)
  26. }
  27. const numberOfSuggestions = __guard__(
  28. __guard__(
  29. __guard__(body != null ? body.misspellings : undefined, x2 => x2[0]),
  30. x1 => x1.suggestions
  31. ),
  32. x => x.length
  33. )
  34. if (numberOfSuggestions > 10) {
  35. logger.log('health check passed')
  36. return res.sendStatus(200)
  37. } else {
  38. logger.err({ body, numberOfSuggestions }, 'health check failed')
  39. return res.sendStatus(500)
  40. }
  41. })
  42. }
  43. }
  44. function __guard__(value, transform) {
  45. return typeof value !== 'undefined' && value !== null
  46. ? transform(value)
  47. : undefined
  48. }