comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3437:dbaf439c8888

OrthancPlugins::OrthancJob::SubmitAndWait()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Jun 2019 18:36:48 +0200
parents 6503ab9489ba
children d97ef941d521
comparison
equal deleted inserted replaced
3436:64eafed9418a 3437:dbaf439c8888
31 **/ 31 **/
32 32
33 33
34 #include "OrthancPluginCppWrapper.h" 34 #include "OrthancPluginCppWrapper.h"
35 35
36 #include <boost/thread.hpp>
36 #include <boost/algorithm/string/predicate.hpp> 37 #include <boost/algorithm/string/predicate.hpp>
37 #include <json/reader.h> 38 #include <json/reader.h>
38 #include <json/writer.h> 39 #include <json/writer.h>
39 40
40 41
2048 2049
2049 2050
2050 std::string OrthancJob::Submit(OrthancJob* job, 2051 std::string OrthancJob::Submit(OrthancJob* job,
2051 int priority) 2052 int priority)
2052 { 2053 {
2054 if (job == NULL)
2055 {
2056 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer);
2057 }
2058
2053 OrthancPluginJob* orthanc = Create(job); 2059 OrthancPluginJob* orthanc = Create(job);
2054 2060
2055 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); 2061 char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority);
2056 2062
2057 if (id == NULL) 2063 if (id == NULL)
2067 OrthancPluginFreeString(GetGlobalContext(), id); 2073 OrthancPluginFreeString(GetGlobalContext(), id);
2068 2074
2069 return tmp; 2075 return tmp;
2070 } 2076 }
2071 } 2077 }
2078
2079
2080 void OrthancJob::SubmitAndWait(Json::Value& result,
2081 OrthancJob* job /* takes ownership */,
2082 int priority)
2083 {
2084 std::string id = Submit(job, priority);
2085
2086 for (;;)
2087 {
2088 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
2089
2090 Json::Value status;
2091 if (!RestApiGet(status, "/jobs/" + id, false) ||
2092 !status.isMember("State") ||
2093 status["State"].type() != Json::stringValue)
2094 {
2095 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InexistentItem);
2096 }
2097
2098 const std::string state = status["State"].asString();
2099 if (state == "Success")
2100 {
2101 if (status.isMember("Content"))
2102 {
2103 result = status["Content"];
2104 }
2105 else
2106 {
2107 result = Json::objectValue;
2108 }
2109
2110 return;
2111 }
2112 else if (state == "Running")
2113 {
2114 continue;
2115 }
2116 else if (!status.isMember("ErrorCode") ||
2117 status["ErrorCode"].type() != Json::intValue)
2118 {
2119 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InternalError);
2120 }
2121 else
2122 {
2123 if (!status.isMember("ErrorDescription") ||
2124 status["ErrorDescription"].type() != Json::stringValue)
2125 {
2126 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt());
2127 }
2128 else
2129 {
2130 #if HAS_ORTHANC_EXCEPTION == 1
2131 throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(status["ErrorCode"].asInt()),
2132 status["ErrorDescription"].asString());
2133 #else
2134 LogError("Exception while executing the job: " + status["ErrorDescription"].asString());
2135 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt());
2136 #endif
2137 }
2138 }
2139 }
2140 }
2141
2072 #endif 2142 #endif
2073 2143
2074 2144
2075 2145
2076 2146