Skip to content

Commit

Permalink
email alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
manafasif committed Dec 5, 2023
1 parent 0bca754 commit e5d831a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import http from "http";
import { logger } from "./utils/logger";
import { initDB } from "./utils/db";
import { logHandler } from "./routes/log";
import { checkUptime } from "./routes/log";


const app = express();

Expand Down Expand Up @@ -39,6 +41,12 @@ app.get("/", (_req, res) => {
app.use(bodyParser.json());
app.use('/log', logHandler)


const UPTIME_CHECK_INTERVAL = process.env.UPTIME_CHECK_INTERVAL as unknown as number || 60000; // Default: 1 minute

setInterval(checkUptime, UPTIME_CHECK_INTERVAL);


initDB()
.then((_database) => {
logger.info("Database Initialized");
Expand Down
37 changes: 23 additions & 14 deletions src/routes/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ router.post("/", (req: Request, res: Response) => {



const UPTIME_CHECK_INTERVAL = process.env.UPTIME_CHECK_INTERVAL || 60000; // Default: 1 minute


const EMAIL_USERNAME = process.env.EMAIL_USERNAME;
Expand All @@ -72,31 +71,41 @@ const transporter = nodemailer.createTransport({

const EMAIL_LIST = ["[email protected]"];

var email_sent = false

export async function checkUptime() {

const subject = "API DOWN";

const mailOptions = {
from: EMAIL_USERNAME, // Replace with your Gmail email
to: EMAIL_LIST.join(", "),
subject,
text: subject,
};

async function checkUptime() {
try {
const response = await fetch('https://api.hanswehr.com/root?root=qtl');
if (response.status === 200) {
logger.info('Uptime check passed. Endpoint is up.');
email_sent = false
} else {
logger.error('Uptime check failed. Endpoint is down.');
// Trigger another workflow or take necessary actions on failure

const subject = "API DOWN";

const mailOptions = {
from: EMAIL_USERNAME, // Replace with your Gmail email
to: EMAIL_LIST.join(", "),
subject,
text: subject,
};

await transporter.sendMail(mailOptions);
if (!email_sent) {
await transporter.sendMail(mailOptions);
email_sent = true
}
}
} catch (error) {
} catch (error: any) {
logger.error(`Error checking uptime: ${error.message}`);
if (!email_sent) {
await transporter.sendMail(mailOptions);
email_sent = true
}
// Trigger another workflow or take necessary actions on failure
}
}

export const logHandler = router;
export const logHandler = router;

0 comments on commit e5d831a

Please sign in to comment.