Skip to content
Snippets Groups Projects
Commit fbe2605b authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

TMSS-156: improved logging. Do not throw, but log errors and continue.

parent 13ad1eb8
No related branches found
No related tags found
1 merge request!116TMSS-156: Resolve TMSS-156
...@@ -913,7 +913,7 @@ void MACScheduler::_updatePlannedList() ...@@ -913,7 +913,7 @@ void MACScheduler::_updatePlannedList()
string parsetText = itsTMSSconnection->getParsetAsText(subtask_id); string parsetText = itsTMSSconnection->getParsetAsText(subtask_id);
if(prepIter == itsPreparedObs.end()) { if(prepIter == itsPreparedObs.end()) {
itsTMSSconnection->getSubTask(subtask_id); itsTMSSconnection->getSubTask(subtask_id);
cout << std::endl << " *** PARSET for " << subtask_id << " ***" << std::endl << parsetText.c_str() << std::endl << std::endl; LOG_INFO_STR(" *** PARSET for " << subtask_id << " ***" << std::endl << parsetText);
} }
string filename(observationParset(subtask_id)); string filename(observationParset(subtask_id));
......
...@@ -21,6 +21,9 @@ ...@@ -21,6 +21,9 @@
// $Id$ // $Id$
//#include <Common/LofarLogger.h> //#include <Common/LofarLogger.h>
#include <lofar_config.h>
#include <Common/LofarLogger.h>
#include "TMSSBridge.h" #include "TMSSBridge.h"
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
...@@ -60,8 +63,11 @@ TMSSBridge::~TMSSBridge() ...@@ -60,8 +63,11 @@ TMSSBridge::~TMSSBridge()
Json::Value TMSSBridge::getSubTask(int subtask_id) Json::Value TMSSBridge::getSubTask(int subtask_id)
{ {
string queryStr = "/api/subtask/" + to_string(subtask_id) + "/"; string queryStr = "/api/subtask/" + to_string(subtask_id) + "/";
Json::Value result = httpGETAsJson(queryStr);
return result; Json::Value result;
if(httpGETAsJson(queryStr, result))
return result;
return Json::Value("");
} }
// //
...@@ -76,9 +82,11 @@ Json::Value TMSSBridge::getSubTasksStartingInThreeMinutes() ...@@ -76,9 +82,11 @@ Json::Value TMSSBridge::getSubTasksStartingInThreeMinutes()
//TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow //TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow
string queryStr = "/api/subtask/?state__value=scheduled&start_time__gt=" + to_iso_extended_string(lower_limit) + "&start_time__lt=" + to_iso_extended_string(upper_limit) + "&ordering=start_time"; string queryStr = "/api/subtask/?state__value=scheduled&start_time__gt=" + to_iso_extended_string(lower_limit) + "&start_time__lt=" + to_iso_extended_string(upper_limit) + "&ordering=start_time";
Json::Value result = httpGETAsJson(queryStr);
return result["results"]; Json::Value result;
if(httpGETAsJson(queryStr, result))
return result["results"];
return Json::Value("");
} }
Json::Value TMSSBridge::getActiveSubTasks() Json::Value TMSSBridge::getActiveSubTasks()
...@@ -87,9 +95,10 @@ Json::Value TMSSBridge::getActiveSubTasks() ...@@ -87,9 +95,10 @@ Json::Value TMSSBridge::getActiveSubTasks()
//TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow //TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow
string queryStr = "/api/subtask/?state__value=started&start_time__lt=" + to_iso_extended_string(now) + "&stop_time__gt=" + to_iso_extended_string(now) + "&ordering=start_time"; string queryStr = "/api/subtask/?state__value=started&start_time__lt=" + to_iso_extended_string(now) + "&stop_time__gt=" + to_iso_extended_string(now) + "&ordering=start_time";
Json::Value result = httpGETAsJson(queryStr); Json::Value result;
if(httpGETAsJson(queryStr, result))
return result["results"]; return result["results"];
return Json::Value("");
} }
Json::Value TMSSBridge::getFinishingSubTasks() Json::Value TMSSBridge::getFinishingSubTasks()
...@@ -98,21 +107,32 @@ Json::Value TMSSBridge::getFinishingSubTasks() ...@@ -98,21 +107,32 @@ Json::Value TMSSBridge::getFinishingSubTasks()
//TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow //TODO: make exact query as in SAS/OTDB/sql/getTreeGroup_func.sql with OR'd states and exact timewindow
string queryStr = "/api/subtask/?state__value=finishing&stop_time__gt=" + to_iso_extended_string(justnow) + "&ordering=start_time"; string queryStr = "/api/subtask/?state__value=finishing&stop_time__gt=" + to_iso_extended_string(justnow) + "&ordering=start_time";
Json::Value result = httpGETAsJson(queryStr); Json::Value result;
if(httpGETAsJson(queryStr, result))
return result["results"]; return result["results"];
return Json::Value("");
} }
std::string TMSSBridge::getParsetAsText(int subtask_id) std::string TMSSBridge::getParsetAsText(int subtask_id)
{ {
string queryStr = "/api/subtask/" + to_string(subtask_id) + "/parset"; string queryStr = "/api/subtask/" + to_string(subtask_id) + "/parset";
return httpQuery(queryStr); string result;
if(httpQuery(queryStr, result, "GET"))
return result;
return "";
} }
std::string TMSSBridge::setSubtaskState(int subtask_id, const string& state) bool TMSSBridge::setSubtaskState(int subtask_id, const string& state)
{ {
string queryStr = "/api/subtask/" + to_string(subtask_id) + "/"; string queryStr = "/api/subtask/" + to_string(subtask_id) + "/";
return httpQuery(queryStr, "PATCH", "{ \"state\": \"/api/subtask_state/" + state +"/\" }"); string result;
if(httpQuery(queryStr, result, "PATCH", "{ \"state\": \"/api/subtask_state/" + state +"/\" }")) {
LOG_INFO_STR("Updated subtask id=" << subtask_id << " to status=" << state);
return true;
}
LOG_ERROR_STR("Could not update subtask id=" << subtask_id << " to status=" << state << " response=" << result);
return false;
} }
std::size_t callback(const char* in, std::size_t callback(const char* in,
...@@ -134,7 +154,7 @@ std::size_t callback(const char* in, ...@@ -134,7 +154,7 @@ std::size_t callback(const char* in,
// httpQuery("/api/subtask/?start_time__lt=2020-03-04T12:03:00") // httpQuery("/api/subtask/?start_time__lt=2020-03-04T12:03:00")
// results in a json string output // results in a json string output
// //
string TMSSBridge::httpQuery(const string& target, const string& query_method, const string& data) bool TMSSBridge::httpQuery(const string& target, string &result, const string& query_method, const string& data)
{ {
const std::string url(std::string("http://") + itsHost + std::string(":") + std::to_string(itsPort) + target); const std::string url(std::string("http://") + itsHost + std::string(":") + std::to_string(itsPort) + target);
...@@ -200,29 +220,34 @@ string TMSSBridge::httpQuery(const string& target, const string& query_method, c ...@@ -200,29 +220,34 @@ string TMSSBridge::httpQuery(const string& target, const string& query_method, c
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
curl_global_cleanup(); curl_global_cleanup();
cout << "[" << query_method << "] code=" << httpCode << " " << url << std::endl; LOG_INFO_STR("[" << query_method << "] code=" << httpCode << " " << url);
if (httpCode == 200) if (httpCode == 200)
return string(*httpData.get()); result = string(*httpData.get());
return true;
THROW(TMSSBridgeException, "Couldn't " + query_method + " from " + url + " exiting with http code " + to_string(httpCode)); LOG_ERROR_STR("Couldn't " + query_method + " from " + url + " exiting with http code " + to_string(httpCode));
result = "";
return false;
} }
Json::Value TMSSBridge::httpGETAsJson(const string& target) bool TMSSBridge::httpGETAsJson(const string& target, Json::Value &result)
{ {
std::string httpResponse = this->httpQuery(target); result = Json::Value("");
Json::Value jsonData(""); std::string text_result;
Json::Reader jsonReader; if(this->httpQuery(target, text_result)) {
if (jsonReader.parse(httpResponse, jsonData)) Json::Reader jsonReader;
{ if (jsonReader.parse(text_result, result))
// if(jsonData["count"] != 0) { {
// cout << "JSON data for " << target << std::endl << jsonData.toStyledString() << std::endl; if(result["count"] != 0) {
// } LOG_DEBUG_STR(string("JSON data for ") << target << std::endl << result.toStyledString());
return jsonData; }
return true;
}
} }
LOG_ERROR_STR("Could not parse HTTP data as JSON. HTTP data was:\n" + text_result);
THROW(TMSSBridgeException, "Could not parse HTTP data as JSON. HTTP data was:\n" + httpResponse); return false;
} }
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#define TMSSBRIDGE_H #define TMSSBRIDGE_H
#include <Common/LofarTypes.h> #include <Common/LofarTypes.h>
#include <Common/Exception.h>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <jsoncpp/json/json.h> #include <jsoncpp/json/json.h>
...@@ -50,14 +49,15 @@ public: ...@@ -50,14 +49,15 @@ public:
Json::Value getActiveSubTasks(); Json::Value getActiveSubTasks();
Json::Value getFinishingSubTasks(); Json::Value getFinishingSubTasks();
string getParsetAsText(int subtask_id); string getParsetAsText(int subtask_id);
string setSubtaskState(int subtask_id, const std::string& state); bool setSubtaskState(int subtask_id, const std::string& state);
// Actually the next method are private, make it public to be able to use in UnitTest++ // Actually the next method are private, make it public to be able to use in UnitTest++
vector<string> translateHttpResultToSortedUrlList(Json::Value result); vector<string> translateHttpResultToSortedUrlList(Json::Value result);
protected:
// http request to TMSS // http request to TMSS
string httpQuery(const string& target, const string& query_method="GET", const string& data=""); bool httpQuery(const string& target, string &result, const string& query_method="GET", const string& data="");
Json::Value httpGETAsJson(const string& target); bool httpGETAsJson(const string& target, Json::Value &result);
private: private:
// Copying is not allowed // Copying is not allowed
...@@ -70,8 +70,6 @@ private: ...@@ -70,8 +70,6 @@ private:
int itsPort; int itsPort;
}; };
EXCEPTION_CLASS(TMSSBridgeException, LOFAR::Exception);
};//MainCU };//MainCU
};//LOFAR };//LOFAR
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment