HealthCheckController.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint-disable
  2. camelcase,
  3. no-dupe-keys,
  4. standard/no-callback-literal,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const { ObjectId } = require("mongojs");
  15. const request = require("request");
  16. const async = require("async");
  17. const _ = require("underscore");
  18. const settings = require("settings-sharelatex");
  19. const { port } = settings.internal.notifications;
  20. const logger = require("logger-sharelatex");
  21. const mongojs = require('mongojs');
  22. const Settings = require('settings-sharelatex');
  23. const db = mongojs(Settings.mongo != null ? Settings.mongo.url : undefined, ['notifications']);
  24. module.exports = {
  25. check(callback){
  26. const user_id = ObjectId();
  27. const cleanupNotifications = callback=> db.notifications.remove({user_id}, callback);
  28. let notification_key = `smoke-test-notification-${ObjectId()}`;
  29. const getOpts = endPath=> ({url:`http://localhost:${port}/user/${user_id}${endPath}`, timeout:5000});
  30. logger.log({user_id, opts:getOpts(), key:notification_key, user_id}, "Health Check: running");
  31. const jobs = [
  32. function(cb){
  33. const opts = getOpts("/");
  34. opts.json = {key: notification_key, messageOpts:'', templateKey:'f4g5', user_id};
  35. return request.post(opts, cb);
  36. },
  37. function(cb){
  38. const opts = getOpts("/");
  39. opts.json = true;
  40. return request.get(opts, function(err, res, body){
  41. if (err != null) {
  42. logger.err({err}, "Health Check: error getting notification");
  43. return callback(err);
  44. } else if (res.statusCode !== 200) {
  45. const e = `status code not 200 ${res.statusCode}`;
  46. logger.err({err}, e);
  47. return cb(e);
  48. }
  49. const hasNotification = _.some(body, notification=> (notification.key === notification_key) && (notification.user_id === user_id.toString()));
  50. if (hasNotification) {
  51. return cb(null, body);
  52. } else {
  53. logger.err({body, notification_key}, "Health Check: notification not in response");
  54. return cb("notification not found in response");
  55. }
  56. });
  57. }
  58. ];
  59. return async.series(jobs, function(err, body){
  60. if (err != null) {
  61. logger.err({err}, "Health Check: error running health check");
  62. return cleanupNotifications(() => callback(err));
  63. } else {
  64. const notification_id = body[1][0]._id;
  65. notification_key = body[1][0].key;
  66. let opts = getOpts(`/notification/${notification_id}`);
  67. logger.log({notification_id, notification_key}, "Health Check: doing cleanup");
  68. return request.del(opts, function(err, res, body){
  69. if (err != null) {
  70. logger.err(err, opts, "Health Check: error cleaning up notification");
  71. return callback(err);
  72. }
  73. opts = getOpts("");
  74. opts.json = {key: notification_key};
  75. return request.del(opts, function(err, res, body){
  76. if (err != null) {
  77. logger.err(err, opts, "Health Check: error cleaning up notification");
  78. return callback(err);
  79. }
  80. return cleanupNotifications(callback);
  81. });
  82. });
  83. }
  84. });
  85. }
  86. };