Skip to content

Commit

Permalink
switched to console log
Browse files Browse the repository at this point in the history
  • Loading branch information
manafasif committed Dec 5, 2023
1 parent e5d831a commit de6f360
Show file tree
Hide file tree
Showing 4 changed files with 6,421 additions and 5,950 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"jsonwebtoken": "^9.0.0",
"mongodb": "^5.3.0",
"mongoose": "^7.1.0",
"node-fetch": "^3.3.2",
"nodemailer": "^6.9.7",
"sqlite3": "^5.1.4",
"swagger-jsdoc": "^6.2.8",
Expand Down
9 changes: 8 additions & 1 deletion src/routes/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from "../utils/logger";
import { insertLogIntoDB } from "../utils/db";
import nodemailer from "nodemailer";
import dotenv from "dotenv";
import { triggerWorkflow } from "../utils/trigger_workflows";


const router = express.Router();
Expand Down Expand Up @@ -87,24 +88,30 @@ export 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.');
// logger.info('Uptime check passed. Endpoint is up.');
console.log('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

if (!email_sent) {
await transporter.sendMail(mailOptions);
await triggerWorkflow()
email_sent = true
}

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

}
}

Expand Down
44 changes: 44 additions & 0 deletions src/utils/trigger_workflows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import fetch from 'node-fetch';
import { logger } from './logger';

// Function to trigger GitHub Actions workflow
export async function triggerWorkflow() {
const repoOwner = 'manafasif';
const repoName = 'hans-wehr-cloud-logger';
const workflowName = 'build_push_docker.yml'; // Replace with the actual name of your workflow

const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/actions/workflows/${workflowName}/dispatches`;

const githubToken = 'your-github-token'; // Replace with a valid GitHub token with workflow scope

const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${githubToken}`,
};

const requestBody = {
ref: 'main', // Replace with the branch where your workflow is defined
inputs: {
// Add any additional inputs that your workflow might need
},
};

try {
const response = await fetch(apiUrl, {
method: 'POST',
headers,
body: JSON.stringify(requestBody),
});

if (response.ok) {
logger.info('Workflow triggered successfully.');
} else {
logger.error('Failed to trigger workflow:', await response.text());
}
} catch (error: any) {
logger.error('Error triggering workflow:', error.message);
}
}

// // Call the function to trigger the workflow
// triggerWorkflow();
Loading

0 comments on commit de6f360

Please sign in to comment.