HealthCheckController.js 3.0 KB

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