oneuptime/probe/index.js

135 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-06-30 21:18:42 +00:00
// if new relic license key exists. Then load the key.
2021-07-06 11:34:39 +00:00
if (process.env.NEW_RELIC_LICENSE_KEY) {
2021-06-30 21:18:42 +00:00
require('newrelic');
}
2020-03-27 11:30:27 +00:00
const { NODE_ENV } = process.env;
if (!NODE_ENV || NODE_ENV === 'development') {
// Load env vars from /backend/.env
require('custom-env').env();
}
2021-01-26 13:48:33 +00:00
process.on('exit', () => {
2021-07-08 19:33:17 +00:00
// eslint-disable-next-line no-console
2021-01-26 13:48:33 +00:00
console.log('Probe Shutting Shutdown');
});
process.on('unhandledRejection', err => {
2021-07-08 19:33:17 +00:00
// eslint-disable-next-line no-console
2021-01-26 13:48:33 +00:00
console.error('Unhandled rejection in probe process occurred');
2021-07-08 19:33:17 +00:00
// eslint-disable-next-line no-console
2021-01-26 13:48:33 +00:00
console.error(err);
});
process.on('uncaughtException', err => {
2021-07-08 19:33:17 +00:00
// eslint-disable-next-line no-console
2021-01-26 13:48:33 +00:00
console.error('Uncaught exception in probe process occurred');
2021-07-08 19:33:17 +00:00
// eslint-disable-next-line no-console
2021-01-26 13:48:33 +00:00
console.error(err);
});
2020-02-16 23:23:27 +00:00
const express = require('express');
const Sentry = require('@sentry/node');
2020-02-16 23:23:27 +00:00
const app = express();
const http = require('http').createServer(app);
const cors = require('cors');
const Main = require('./workers/main');
const cron = require('node-cron');
const config = require('./utils/config');
Sentry.init({
dsn: process.env.SENTRY_DSN,
release: `probe@${process.env.npm_package_version}`,
environment: process.env.NODE_ENV,
tracesSampleRate: 0.0,
2021-09-13 13:50:03 +00:00
integrations: [
new Sentry.Integrations.OnUncaughtException({
onFatalError() {
// override default behaviour
return;
},
}),
],
});
// Sentry: The request handler must be the first middleware on the app
app.use(Sentry.Handlers.requestHandler());
2020-02-24 13:07:26 +00:00
const cronMinuteStartTime = Math.floor(Math.random() * 50);
app.use(cors());
app.set('port', process.env.PORT || 3008);
2021-08-12 12:43:05 +00:00
const monitorStore = {};
2021-12-14 20:40:38 +00:00
// handle probe1 status
app.get(['/probe1/status', '/status'], function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(
JSON.stringify({
status: 200,
message: 'Service Status - OK',
serviceType: 'oneuptime-probe',
})
);
});
// handle probe2 status
app.get(['/probe2/status', '/status'], function(req, res) {
res.setHeader('Content-Type', 'application/json');
2020-02-27 11:15:46 +00:00
res.send(
JSON.stringify({
status: 200,
message: 'Service Status - OK',
serviceType: 'oneuptime-probe',
2020-02-27 11:15:46 +00:00
})
);
});
2021-12-16 07:49:29 +00:00
app.get(['/probe1/monitorCount', '/monitorCount'], function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(
JSON.stringify({
monitorCount: Object.keys(monitorStore).length,
monitors: monitorStore,
})
);
});
app.get(['/probe2/monitorCount', '/monitorCount'], function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send(
JSON.stringify({
monitorCount: Object.keys(monitorStore).length,
monitors: monitorStore,
})
);
});
2021-01-25 16:31:08 +00:00
//App Version
app.get(['/probe/version', '/version'], function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.send({ probeVersion: process.env.npm_package_version });
});
app.use(Sentry.Handlers.errorHandler());
global.Sentry = Sentry;
2022-01-18 16:38:06 +00:00
setTimeout(() => {
Main.runJob(monitorStore);
}, cronMinuteStartTime * 1000);
2021-12-14 17:31:23 +00:00
http.listen(app.get('port'), function() {
// eslint-disable-next-line
console.log(
`Probe with Probe Name ${config.probeName} and Probe Key ${
config.probeKey
} Started on port ${app.get('port')}. OneUptime API URL: ${
config.serverUrl
}`
);
});
module.exports = app;