![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* monitor_client.cc 00002 Wolfgang Sourdeau, January 2013 00003 Copyright (c) 2013 Datacratic. All rights reserved. */ 00004 00005 #include <jml/arch/exception_handler.h> 00006 00007 #include "monitor_client.h" 00008 00009 using namespace std; 00010 00011 namespace RTBKIT { 00012 00013 MonitorClient:: 00014 ~MonitorClient() 00015 { 00016 shutdown(); 00017 } 00018 00019 void 00020 MonitorClient:: 00021 init(std::shared_ptr<ConfigurationService> & config, 00022 const std::string & serviceName) 00023 { 00024 addPeriodic("MonitorClient::checkStatus", 1.0, 00025 std::bind(&MonitorClient::checkStatus, this), 00026 true); 00027 00028 RestProxy::init(config, serviceName); 00029 } 00030 00031 void 00032 MonitorClient:: 00033 shutdown() 00034 { 00035 sleepUntilIdle(); 00036 RestProxy::shutdown(); 00037 } 00038 00039 void 00040 MonitorClient:: 00041 checkStatus() 00042 { 00043 if (!testMode) { 00044 Guard(requestLock); 00045 00046 if (pendingRequest) { 00047 cerr << "MonitorClient::checkStatus: last request is still active\n"; 00048 } 00049 else { 00050 pendingRequest = true; 00051 push(onDone, "GET", "/v1/status"); 00052 } 00053 } 00054 } 00055 00056 void 00057 MonitorClient:: 00058 onResponseReceived(exception_ptr ext, int responseCode, const string & body) 00059 { 00060 bool newStatus(false); 00061 00062 if (responseCode == 200) { 00063 ML::Set_Trace_Exceptions notrace(false); 00064 try { 00065 Json::Value parsedBody = Json::parse(body); 00066 if (parsedBody.isMember("status") && parsedBody["status"] == "ok") { 00067 newStatus = true; 00068 } 00069 } 00070 catch (const Json::Exception & exc) { 00071 } 00072 } 00073 00074 lastStatus = newStatus; 00075 lastCheck = Date::now(); 00076 pendingRequest = false; 00077 } 00078 00079 bool 00080 MonitorClient:: 00081 getStatus() 00082 const 00083 { 00084 bool status; 00085 00086 if (testMode) 00087 status = testResponse; 00088 else { 00089 Date now = Date::now(); 00090 00091 status = (lastStatus 00092 && (lastCheck.plusSeconds(checkTimeout_) >= now)); 00093 } 00094 00095 return status; 00096 } 00097 00098 } // RTB