2020-01-13 19:00:40 +00:00
|
|
|
/* eslint-disable
|
|
|
|
no-dupe-keys,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-01-13 19:00:33 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* 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
|
|
|
|
*/
|
2020-08-31 09:12:37 +00:00
|
|
|
const { db, ObjectId } = require('./mongodb')
|
2020-01-13 19:00:49 +00:00
|
|
|
const request = require('request')
|
|
|
|
const async = require('async')
|
2021-07-12 16:47:16 +00:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-01-13 19:00:49 +00:00
|
|
|
const { port } = settings.internal.notifications
|
2021-12-14 13:00:35 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2016-01-14 12:35:16 +00:00
|
|
|
|
2020-01-13 19:00:49 +00:00
|
|
|
module.exports = {
|
|
|
|
check(callback) {
|
2023-10-23 08:45:57 +00:00
|
|
|
const userId = new ObjectId()
|
2021-07-13 11:04:44 +00:00
|
|
|
const cleanupNotifications = callback =>
|
2023-10-23 08:45:57 +00:00
|
|
|
db.notifications.deleteOne({ user_id: userId }, callback)
|
2016-06-01 13:57:06 +00:00
|
|
|
|
2023-10-23 08:45:57 +00:00
|
|
|
let notificationKey = `smoke-test-notification-${new ObjectId()}`
|
2021-07-13 11:04:44 +00:00
|
|
|
const getOpts = endPath => ({
|
2023-03-16 12:38:09 +00:00
|
|
|
url: `http://localhost:${port}/user/${userId}${endPath}`,
|
2021-07-13 11:04:44 +00:00
|
|
|
timeout: 5000,
|
2020-01-13 19:00:49 +00:00
|
|
|
})
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2023-03-16 12:38:09 +00:00
|
|
|
{ userId, opts: getOpts(), key: notificationKey, userId },
|
2020-01-13 19:00:49 +00:00
|
|
|
'Health Check: running'
|
|
|
|
)
|
|
|
|
const jobs = [
|
2020-06-04 07:50:05 +00:00
|
|
|
function (cb) {
|
2020-01-13 19:00:49 +00:00
|
|
|
const opts = getOpts('/')
|
|
|
|
opts.json = {
|
2023-03-16 12:38:09 +00:00
|
|
|
key: notificationKey,
|
2020-01-13 19:00:49 +00:00
|
|
|
messageOpts: '',
|
|
|
|
templateKey: 'f4g5',
|
2023-03-16 12:38:09 +00:00
|
|
|
user_id: userId,
|
2020-01-13 19:00:49 +00:00
|
|
|
}
|
|
|
|
return request.post(opts, cb)
|
|
|
|
},
|
2020-06-04 07:50:05 +00:00
|
|
|
function (cb) {
|
2020-01-13 19:00:49 +00:00
|
|
|
const opts = getOpts('/')
|
|
|
|
opts.json = true
|
2020-06-04 07:50:05 +00:00
|
|
|
return request.get(opts, function (err, res, body) {
|
2020-01-13 19:00:49 +00:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'Health Check: error getting notification')
|
|
|
|
return callback(err)
|
|
|
|
} else if (res.statusCode !== 200) {
|
|
|
|
const e = `status code not 200 ${res.statusCode}`
|
|
|
|
logger.err({ err }, e)
|
|
|
|
return cb(e)
|
|
|
|
}
|
2022-12-20 14:55:25 +00:00
|
|
|
const hasNotification = body.some(
|
2021-07-13 11:04:44 +00:00
|
|
|
notification =>
|
2023-03-16 12:38:09 +00:00
|
|
|
notification.key === notificationKey &&
|
|
|
|
notification.user_id === userId.toString()
|
2020-01-13 19:00:49 +00:00
|
|
|
)
|
|
|
|
if (hasNotification) {
|
|
|
|
return cb(null, body)
|
|
|
|
} else {
|
|
|
|
logger.err(
|
2023-03-16 12:38:09 +00:00
|
|
|
{ body, notificationKey },
|
2020-01-13 19:00:49 +00:00
|
|
|
'Health Check: notification not in response'
|
|
|
|
)
|
2021-10-27 09:49:42 +00:00
|
|
|
return cb(new Error('notification not found in response'))
|
2020-01-13 19:00:49 +00:00
|
|
|
}
|
|
|
|
})
|
2021-07-13 11:04:44 +00:00
|
|
|
},
|
2020-01-13 19:00:49 +00:00
|
|
|
]
|
2020-06-04 07:50:05 +00:00
|
|
|
return async.series(jobs, function (err, body) {
|
2020-01-13 19:00:49 +00:00
|
|
|
if (err != null) {
|
|
|
|
logger.err({ err }, 'Health Check: error running health check')
|
|
|
|
return cleanupNotifications(() => callback(err))
|
|
|
|
} else {
|
2023-03-16 12:38:09 +00:00
|
|
|
const notificationId = body[1][0]._id
|
|
|
|
notificationKey = body[1][0].key
|
|
|
|
let opts = getOpts(`/notification/${notificationId}`)
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug(
|
2023-03-16 12:38:09 +00:00
|
|
|
{ notificationId, notificationKey },
|
2020-01-13 19:00:49 +00:00
|
|
|
'Health Check: doing cleanup'
|
|
|
|
)
|
2020-06-04 07:50:05 +00:00
|
|
|
return request.del(opts, function (err, res, body) {
|
2020-01-13 19:00:49 +00:00
|
|
|
if (err != null) {
|
|
|
|
logger.err(
|
|
|
|
err,
|
|
|
|
opts,
|
|
|
|
'Health Check: error cleaning up notification'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
opts = getOpts('')
|
2023-03-16 12:38:09 +00:00
|
|
|
opts.json = { key: notificationKey }
|
2020-06-04 07:50:05 +00:00
|
|
|
return request.del(opts, function (err, res, body) {
|
2020-01-13 19:00:49 +00:00
|
|
|
if (err != null) {
|
|
|
|
logger.err(
|
|
|
|
err,
|
|
|
|
opts,
|
|
|
|
'Health Check: error cleaning up notification'
|
|
|
|
)
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return cleanupNotifications(callback)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-07-13 11:04:44 +00:00
|
|
|
},
|
2020-01-13 19:00:49 +00:00
|
|
|
}
|