Skip to content

Commit

Permalink
Adds heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
arunkrishnan committed May 15, 2016
1 parent 8a465e5 commit d041188
Show file tree
Hide file tree
Showing 15 changed files with 9,158 additions and 5 deletions.
1 change: 1 addition & 0 deletions merchant/merchant.info
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"merchant_id":"234","merchant_id":"524","customer_id":"123","time_created" : "2016-05-15 00:30:48"}
1 change: 1 addition & 0 deletions merchant/services/config.php
9 changes: 9 additions & 0 deletions merchant/services/config.php.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$config['megamind_http_url'] = '172.31.99.15/http';
$config['oblivion_http_url'] = '172.31.98.139/http';

$config['megamind_sms_address'] = '172.31.99.15/sms';
$config['oblivion_sms_address'] = '172.31.98.139/http';

$config['httpStat'] = true;
?>
8 changes: 8 additions & 0 deletions merchant/services/config.php.prod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
$config['megamind_http_url'] = '127.0.0.1/http/';
$config['oblivion_http_url'] = '127.0.0.1/http/';

$config['megamind_sms_address'] = '127.0.0.1/sms/';
$config['oblivion_sms_Address'] = '127.0.0.1/sms/';

?>
71 changes: 71 additions & 0 deletions merchant/services/heartbeat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
require_once '../../common/helpers/networkHelper.php';
require_once '../../common/helpers/fileHelper.php';
require_once '../../common/helpers/SmsHelper.php';
require_once './config.php';

$filePath = "../merchant.info";

$megamindUrl = $config['megamind_http_url'];
$oblivionUrl = $config['oblivion_http_url'];

$megamindSmsAddress = $config['megamind_sms_address'];
$oblivionSmsAddress = $config['oblivion_sms_address'];

$currentTime = date("Y-m-d H:i:s");


$fileData = FileHelper::readFile($filePath);

$httpAvail = $config['httpStat'];
if($httpAvail) {
// Making a HeartBeat Request to MegaMind
$megamindResponse = NetworkHelper::postJson($megamindUrl, $fileData , 1, 2);
} else {
$megamindResponse = "";
}

if (!empty($megamindResponse)) {
echo "megamindResponse :: $megamindResponse \n";
$shellResult = shell_exec("echo '$currentTime,success' >> megamindConnectionHttp.log");

} else {
echo "HeartBeat request to MegaMind Failed \n";
$shellResult1 = shell_exec("echo '$currentTime,failed' >> megamindConnectionHttp.log");

// if its the 5th consecutive failed request, notify megamind using SMS
$shellResult2 = shell_exec("tail -n5 megamindConnectionHttp.log | grep failed | wc -l");
$shellResult2 = str_replace(array("\n","\r"), '', $shellResult2);
if ($shellResult2 >= 5) {
$megamindResponseSms = SmsHelper::sendSms($megamindSmsAddress, $fileData);
$shellResult3 = shell_exec("echo '$currentTime,success' >> megamindConnectionSms.log");
}

}

if($httpAvail) {
// Making a HeartBeat Request to oblivion
$oblivionResponse = NetworkHelper::postJson($oblivionUrl, $fileData , 1, 2);
} else {
$oblivionResponse = "";
}

if (!empty($oblivionResponse)) {
echo "oblivionResponse :: $oblivionResponse \n";
$shellResult = shell_exec("echo '$currentTime,success' >> oblivionConnectionHttp.log");


} else {
echo "HeartBeat request to oblivion Failed \n";
$shellResult = shell_exec("echo '$currentTime,failed' >> oblivionConnectionHttp.log");

// if its the 5th consecutive failed request, notify megamind using SMS
$shellResult2 = shell_exec("tail -n5 oblivionConnectionHttp.log | grep failed | wc -l");
$shellResult2 = str_replace(array("\n","\r"), '', $shellResult2);
if ($shellResult2 >= 5) {
$oblivionResponseSms = SmsHelper::sendSms($megamindSmsAddress, $fileData);
$shellResult3 = shell_exec("echo '$currentTime,success' >> oblivionConnectionSms.log");
}

}
?>
Loading

0 comments on commit d041188

Please sign in to comment.