# HG changeset patch # User Sebastien Jodogne # Date 1561048608 -7200 # Node ID dbaf439c8888619e587626ce00887c7c33a41baa # Parent 64eafed9418a516e089070d61486855b06b84e61 OrthancPlugins::OrthancJob::SubmitAndWait() diff -r 64eafed9418a -r dbaf439c8888 Plugins/Samples/Common/OrthancPluginCppWrapper.cpp --- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Wed Jun 19 18:33:49 2019 +0200 +++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Thu Jun 20 18:36:48 2019 +0200 @@ -33,6 +33,7 @@ #include "OrthancPluginCppWrapper.h" +#include #include #include #include @@ -2050,6 +2051,11 @@ std::string OrthancJob::Submit(OrthancJob* job, int priority) { + if (job == NULL) + { + ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_NullPointer); + } + OrthancPluginJob* orthanc = Create(job); char* id = OrthancPluginSubmitJob(GetGlobalContext(), orthanc, priority); @@ -2069,6 +2075,70 @@ return tmp; } } + + + void OrthancJob::SubmitAndWait(Json::Value& result, + OrthancJob* job /* takes ownership */, + int priority) + { + std::string id = Submit(job, priority); + + for (;;) + { + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); + + Json::Value status; + if (!RestApiGet(status, "/jobs/" + id, false) || + !status.isMember("State") || + status["State"].type() != Json::stringValue) + { + ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InexistentItem); + } + + const std::string state = status["State"].asString(); + if (state == "Success") + { + if (status.isMember("Content")) + { + result = status["Content"]; + } + else + { + result = Json::objectValue; + } + + return; + } + else if (state == "Running") + { + continue; + } + else if (!status.isMember("ErrorCode") || + status["ErrorCode"].type() != Json::intValue) + { + ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(OrthancPluginErrorCode_InternalError); + } + else + { + if (!status.isMember("ErrorDescription") || + status["ErrorDescription"].type() != Json::stringValue) + { + ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); + } + else + { +#if HAS_ORTHANC_EXCEPTION == 1 + throw Orthanc::OrthancException(static_cast(status["ErrorCode"].asInt()), + status["ErrorDescription"].asString()); +#else + LogError("Exception while executing the job: " + status["ErrorDescription"].asString()); + ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(status["ErrorCode"].asInt()); +#endif + } + } + } + } + #endif diff -r 64eafed9418a -r dbaf439c8888 Plugins/Samples/Common/OrthancPluginCppWrapper.h --- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h Wed Jun 19 18:33:49 2019 +0200 +++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h Thu Jun 20 18:36:48 2019 +0200 @@ -768,6 +768,10 @@ static std::string Submit(OrthancJob* job /* takes ownership */, int priority); + + static void SubmitAndWait(Json::Value& result, + OrthancJob* job /* takes ownership */, + int priority); }; #endif