changeset 2657:5eea2f11e8df jobs

JobsSerialization.GenericJobs
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 06 Jun 2018 16:52:42 +0200
parents a6d3e45eeff5
children ce770f095092
files Core/JobsEngine/SetOfInstancesJob.cpp Core/JobsEngine/SetOfInstancesJob.h Core/SerializationToolbox.cpp OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp Resources/CMake/OrthancFrameworkConfiguration.cmake UnitTestsSources/MultiThreadingTests.cpp
diffstat 6 files changed, 88 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/Core/JobsEngine/SetOfInstancesJob.cpp	Tue Jun 05 18:25:23 2018 +0200
+++ b/Core/JobsEngine/SetOfInstancesJob.cpp	Wed Jun 06 16:52:42 2018 +0200
@@ -114,6 +114,19 @@
   }
 
 
+  const std::string& SetOfInstancesJob::GetInstance(size_t index) const
+  {
+    if (index > instances_.size())
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+    else
+    {
+      return instances_[index];
+    }
+  }
+      
+
   JobStepResult SetOfInstancesJob::ExecuteStep()
   {
     if (!started_)
@@ -182,8 +195,8 @@
   void SetOfInstancesJob::GetPublicContent(Json::Value& value)
   {
     value["Description"] = GetDescription();
-    value["InstancesCount"] = static_cast<uint32_t>(GetInstances().size());
-    value["FailedInstancesCount"] = static_cast<uint32_t>(GetFailedInstances().size());
+    value["InstancesCount"] = static_cast<uint32_t>(instances_.size());
+    value["FailedInstancesCount"] = static_cast<uint32_t>(failedInstances_.size());
   }    
 
 
--- a/Core/JobsEngine/SetOfInstancesJob.h	Tue Jun 05 18:25:23 2018 +0200
+++ b/Core/JobsEngine/SetOfInstancesJob.h	Wed Jun 06 16:52:42 2018 +0200
@@ -57,6 +57,11 @@
 
     SetOfInstancesJob(const Json::Value& s);  // Unserialization
 
+    size_t GetPosition() const
+    {
+      return position_;
+    }
+
     void SetDescription(const std::string& description)
     {
       description_ = description;
@@ -97,16 +102,18 @@
       return started_;
     }
 
-    const std::vector<std::string>& GetInstances() const
-    {
-      return instances_;
-    }
-  
+    const std::string& GetInstance(size_t index) const;
+      
     const std::set<std::string>& GetFailedInstances() const
     {
       return failedInstances_;
     }
-  
+
+    bool IsFailedInstance(const std::string& instance) const
+    {
+      return failedInstances_.find(instance) != failedInstances_.end();
+    }
+    
     virtual JobStepResult ExecuteStep();
     
     virtual void GetPublicContent(Json::Value& value);
--- a/Core/SerializationToolbox.cpp	Tue Jun 05 18:25:23 2018 +0200
+++ b/Core/SerializationToolbox.cpp	Wed Jun 06 16:52:42 2018 +0200
@@ -116,11 +116,10 @@
         throw OrthancException(ErrorCode_BadFileFormat);
       }
 
-      target.clear();
-      target.resize(value.size());
+      const Json::Value& arr = value[field.c_str()];
 
-      const Json::Value arr = value[field.c_str()];
-    
+      target.resize(arr.size());
+
       for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++)
       {
         if (arr[i].type() != Json::stringValue)
--- a/OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp	Tue Jun 05 18:25:23 2018 +0200
+++ b/OrthancServer/ServerJobs/Operations/StorePeerOperation.cpp	Wed Jun 06 16:52:42 2018 +0200
@@ -44,7 +44,7 @@
 namespace Orthanc
 {
   void StorePeerOperation::Apply(JobOperationValues& outputs,
-                                const JobOperationValue& input,
+                                 const JobOperationValue& input,
                                  IDicomConnectionManager& connectionManager)
   {
     // Configure the HTTP client
--- a/Resources/CMake/OrthancFrameworkConfiguration.cmake	Tue Jun 05 18:25:23 2018 +0200
+++ b/Resources/CMake/OrthancFrameworkConfiguration.cmake	Wed Jun 06 16:52:42 2018 +0200
@@ -138,6 +138,14 @@
   ${ORTHANC_ROOT}/Core/Images/ImageAccessor.cpp
   ${ORTHANC_ROOT}/Core/Images/ImageBuffer.cpp
   ${ORTHANC_ROOT}/Core/Images/ImageProcessing.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/GenericJobUnserializer.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/JobInfo.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/JobStatus.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/JobStepResult.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/Operations/JobOperationValues.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/Operations/LogJobOperation.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp
+  ${ORTHANC_ROOT}/Core/JobsEngine/SetOfInstancesJob.cpp
   ${ORTHANC_ROOT}/Core/Logging.cpp
   ${ORTHANC_ROOT}/Core/SerializationToolbox.cpp
   ${ORTHANC_ROOT}/Core/Toolbox.cpp
@@ -499,16 +507,8 @@
   list(APPEND ORTHANC_CORE_SOURCES_INTERNAL
     ${ORTHANC_ROOT}/Core/Cache/SharedArchive.cpp
     ${ORTHANC_ROOT}/Core/FileStorage/FilesystemStorage.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/GenericJobUnserializer.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/JobInfo.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/JobStatus.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/JobStepResult.cpp
     ${ORTHANC_ROOT}/Core/JobsEngine/JobsEngine.cpp
     ${ORTHANC_ROOT}/Core/JobsEngine/JobsRegistry.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/Operations/JobOperationValues.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/Operations/LogJobOperation.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp
-    ${ORTHANC_ROOT}/Core/JobsEngine/SetOfInstancesJob.cpp
     ${ORTHANC_ROOT}/Core/MultiThreading/RunnableWorkersPool.cpp
     ${ORTHANC_ROOT}/Core/MultiThreading/SharedMessageQueue.cpp
     ${ORTHANC_ROOT}/Core/SharedLibrary.cpp
--- a/UnitTestsSources/MultiThreadingTests.cpp	Tue Jun 05 18:25:23 2018 +0200
+++ b/UnitTestsSources/MultiThreadingTests.cpp	Wed Jun 06 16:52:42 2018 +0200
@@ -141,7 +141,7 @@
   protected:
     virtual bool HandleInstance(const std::string& instance)
     {
-      return true;
+      return (instance != "nope");
     }
 
   public:
@@ -764,6 +764,32 @@
 }
 
 
+TEST(JobsSerialization, JobOperationValues)
+{
+  Json::Value s;
+
+  {
+    JobOperationValues values;
+    values.Append(new NullOperationValue);
+    values.Append(new StringOperationValue("hello"));
+    values.Append(new StringOperationValue("world"));
+    values.Serialize(s);
+  }
+
+  {
+    GenericJobUnserializer unserializer;
+    std::auto_ptr<JobOperationValues> values(JobOperationValues::Unserialize(unserializer, s));
+    ASSERT_EQ(3u, values->GetSize());
+    ASSERT_EQ(JobOperationValue::Type_Null, values->GetValue(0).GetType());
+    ASSERT_EQ(JobOperationValue::Type_String, values->GetValue(1).GetType());
+    ASSERT_EQ(JobOperationValue::Type_String, values->GetValue(2).GetType());
+
+    ASSERT_EQ("hello", dynamic_cast<const StringOperationValue&>(values->GetValue(1)).GetContent());
+    ASSERT_EQ("world", dynamic_cast<const StringOperationValue&>(values->GetValue(2)).GetContent());
+  }
+}
+
+
 TEST(JobsSerialization, GenericValues)
 {
   Json::Value s;
@@ -796,32 +822,6 @@
 }
 
 
-TEST(JobsSerialization, JobOperationValues)
-{
-  Json::Value s;
-
-  {
-    JobOperationValues values;
-    values.Append(new NullOperationValue);
-    values.Append(new StringOperationValue("hello"));
-    values.Append(new StringOperationValue("world"));
-    values.Serialize(s);
-  }
-
-  {
-    GenericJobUnserializer unserializer;
-    std::auto_ptr<JobOperationValues> values(JobOperationValues::Unserialize(unserializer, s));
-    ASSERT_EQ(3u, values->GetSize());
-    ASSERT_EQ(JobOperationValue::Type_Null, values->GetValue(0).GetType());
-    ASSERT_EQ(JobOperationValue::Type_String, values->GetValue(1).GetType());
-    ASSERT_EQ(JobOperationValue::Type_String, values->GetValue(2).GetType());
-
-    ASSERT_EQ("hello", dynamic_cast<const StringOperationValue&>(values->GetValue(1)).GetContent());
-    ASSERT_EQ("world", dynamic_cast<const StringOperationValue&>(values->GetValue(2)).GetContent());
-  }
-}
-
-
 TEST(JobsSerialization, GenericOperations)
 {   
   Json::Value s;
@@ -838,16 +838,14 @@
   {
     std::auto_ptr<IJobOperation> operation;
     operation.reset(unserializer.UnserializeOperation(s));
-    
-  }
 
-  {
-    
+    // Make sure that we have indeed unserialized a log operation
+    ASSERT_THROW(dynamic_cast<DeleteResourceOperation&>(*operation), std::bad_cast);
+    dynamic_cast<LogJobOperation&>(*operation);
   }
 }
 
 
-
 TEST(JobsSerialization, GenericJobs)
 {   
   Json::Value s;
@@ -855,9 +853,14 @@
   {
     DummyInstancesJob job;
     job.SetDescription("description");
+    job.AddInstance("hello");
     job.AddInstance("nope");
-    job.AddInstance("ok");
+    job.AddInstance("world");
     job.SetPermissive(true);
+    ASSERT_THROW(job.ExecuteStep(), OrthancException);  // Not started yet
+    job.Start();
+    job.ExecuteStep();
+    job.ExecuteStep();
     job.Serialize(s);
   }
 
@@ -868,8 +871,18 @@
 
     std::auto_ptr<IJob> job;
     job.reset(unserializer.UnserializeJob(s));
-    ASSERT_EQ("description", dynamic_cast<DummyInstancesJob&>(*job).GetDescription());
-    //ASSERT_EQ("nope", dynamic_cast<DummyInstancesJob&>(*job).GetInstance(0));
+
+    const DummyInstancesJob& tmp = dynamic_cast<const DummyInstancesJob&>(*job);
+    ASSERT_FALSE(tmp.IsStarted());
+    ASSERT_TRUE(tmp.IsPermissive());
+    ASSERT_EQ("description", tmp.GetDescription());
+    ASSERT_EQ(3u, tmp.GetInstancesCount());
+    ASSERT_EQ(2u, tmp.GetPosition());
+    ASSERT_EQ(1u, tmp.GetFailedInstances().size());
+    ASSERT_EQ("hello", tmp.GetInstance(0));
+    ASSERT_EQ("nope", tmp.GetInstance(1));
+    ASSERT_EQ("world", tmp.GetInstance(2));
+    ASSERT_TRUE(tmp.IsFailedInstance("nope"));
   }
 }