/* eslint-disable no-console */ import { services } from '../../../Config/urls' import { getServiceUrls, sendSnsMessage } from './helper' import { ping } from './ping' import { PingedServers } from './types' exports.handler = async () => { const pingedServers: PingedServers[] = [] try { const serverStatuses = await Promise.all( services.flatMap((service: string) => ['dev', 'stage', ''].map((env: string) => { const serviceName = service.replace(/_/g, '') const urls = getServiceUrls(serviceName, env) pingedServers.push({ url: urls.httpUrl, name: `${serviceName} - ${!env ? 'prod' : env}`, }) return ping(urls) })), ) console.log('Pinged servers', JSON.stringify(pingedServers)) const success: string[] = serverStatuses .filter((url) => url.success) .map((url) => url.urls.friendlyUrl) const fail = serverStatuses .filter((url) => !url.success) .map((url) => url.urls.friendlyUrl) if (fail.length || !success.length) { const sentSms = await sendSnsMessage(fail) return { statusCode: 400, body: JSON.stringify('Servers are DOWN'), success, fail, sentSms, } } return { statusCode: 200, body: JSON.stringify('Servers are OK'), success, } } catch (err) { console.log('Lambda Error: Something went wrong with pinging services! ', err) return { statusCode: 400, body: JSON.stringify('Servers are DOWN'), } } }