Skip to content

Commit

Permalink
Update fetching script
Browse files Browse the repository at this point in the history
  • Loading branch information
fahminlb33 committed Sep 13, 2020
1 parent 5ebde52 commit a376ca0
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions app/index.js
Expand Up @@ -8,29 +8,28 @@ const config = Object.freeze({
});

const main = async () => {
const checkResult = [];
const monitoredUrls = JSON.parse(fs.readFileSync('data/monitored.json', 'utf-8'));

const monitoredUrls = JSON.parse(fs.readFileSync('data/monitored.json', 'utf-8'));
const checkResult = [];
const reqs = [];

// run request
console.log("Checking services...");
for (let i = 0; i < monitoredUrls.length; i++)
{
const item = monitoredUrls[i];
const opt = {
url: item.uri,
responseType: 'text',
transformResponse: [],
timeout: 1000 * 5,
headers: {
'User-Agent': config.userAgent
}
};

try {
const opt = {
url: item.uri,
responseType: 'text',
transformResponse: [],
timeout: 1000 * 5,
headers: {
'User-Agent': config.userAgent
}
};

console.debug(`Checking ${item.desc}, env: ${item.env}`);
const response = await axios(opt);
if (!response.data || response.data.includes("Application is not available")) {
console.debug(`Checking ${item.desc}, env: ${item.env}`);
const req = axios(opt).then(response => {
if (response.status >= 400 || !response.data || response.data.includes("Application is not available")) {
checkResult.push({
...item,
status: "error",
Expand All @@ -43,15 +42,18 @@ const main = async () => {
date: new Date().toISOString()
});
}
} catch (error) {
}).catch(_ => {
checkResult.push({
...item,
status: "error",
date: new Date().toISOString()
});
}
});
reqs.push(req);
}

await Promise.all(reqs);

// load latest data
console.log("Fetching last data...");
const lastIncidentData = fs.readFileSync('data/last_incident.json', 'utf-8');
Expand Down Expand Up @@ -106,6 +108,7 @@ const main = async () => {
else
{
const currentServiceLastIncident = currentService.incidents[currentService.incidents.length - 1];
currentService.status = item.status;
if (currentServiceLastIncident.status !== item.status)
{
if (lastIncident.services[currentServiceIndex].incidents.length >= config.maxIncidents)
Expand Down

0 comments on commit a376ca0

Please sign in to comment.