changeset 2608:25225f0b4f33 jobs

simplification wrt. dicom connection manager
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 19 May 2018 15:50:09 +0200
parents 44e268605478
children f7a84b551ee4
files Core/DicomNetworking/TimeoutDicomConnectionManager.cpp Core/DicomNetworking/TimeoutDicomConnectionManager.h Core/JobsEngine/Operations/IJobOperation.h Core/JobsEngine/Operations/LogJobOperation.cpp Core/JobsEngine/Operations/LogJobOperation.h Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp Core/JobsEngine/Operations/SequenceOfOperationsJob.h OrthancServer/ServerJobs/DeleteResourceOperation.cpp OrthancServer/ServerJobs/DeleteResourceOperation.h OrthancServer/ServerJobs/LuaJobManager.cpp OrthancServer/ServerJobs/LuaJobManager.h OrthancServer/ServerJobs/ModifyInstanceOperation.cpp OrthancServer/ServerJobs/ModifyInstanceOperation.h OrthancServer/ServerJobs/StorePeerOperation.cpp OrthancServer/ServerJobs/StorePeerOperation.h OrthancServer/ServerJobs/StoreScuOperation.cpp OrthancServer/ServerJobs/StoreScuOperation.h OrthancServer/ServerJobs/SystemCallOperation.cpp OrthancServer/ServerJobs/SystemCallOperation.h
diffstat 19 files changed, 79 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomNetworking/TimeoutDicomConnectionManager.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/Core/DicomNetworking/TimeoutDicomConnectionManager.cpp	Sat May 19 15:50:09 2018 +0200
@@ -47,13 +47,15 @@
   {
   private:
     TimeoutDicomConnectionManager&  that_;
-    boost::mutex::scoped_lock        lock_;
 
   public:
     Resource(TimeoutDicomConnectionManager& that) : 
-    that_(that),
-    lock_(that.mutex_)
+      that_(that)
     {
+      if (that_.connection_.get() == NULL)
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
 
     ~Resource()
@@ -63,11 +65,7 @@
 
     DicomUserConnection& GetConnection()
     {
-      if (that_.connection_.get() == NULL)
-      {
-        throw OrthancException(ErrorCode_InternalError);
-      }
-
+      assert(that_.connection_.get() != NULL);
       return *that_.connection_;
     }
   };
@@ -91,8 +89,6 @@
 
   void TimeoutDicomConnectionManager::SetTimeout(unsigned int timeout)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-
     timeout_ = boost::posix_time::milliseconds(timeout);
     CheckTimeoutInternal();
   }
@@ -100,21 +96,18 @@
 
   unsigned int TimeoutDicomConnectionManager::GetTimeout()
   {
-    boost::mutex::scoped_lock lock(mutex_);
     return timeout_.total_milliseconds();
   }
 
 
   void TimeoutDicomConnectionManager::Close()
   {
-    boost::mutex::scoped_lock lock(mutex_);
     connection_.reset(NULL);
   }
 
 
   void TimeoutDicomConnectionManager::CheckTimeout()
   {
-    boost::mutex::scoped_lock lock(mutex_);
     CheckTimeoutInternal();
   }
 
@@ -123,8 +116,6 @@
   TimeoutDicomConnectionManager::AcquireConnection(const std::string& localAet,
                                                    const RemoteModalityParameters& remote)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-
     if (connection_.get() == NULL ||
         !connection_->IsSameAssociation(localAet, remote))
     {
--- a/Core/DicomNetworking/TimeoutDicomConnectionManager.h	Sat May 19 15:28:41 2018 +0200
+++ b/Core/DicomNetworking/TimeoutDicomConnectionManager.h	Sat May 19 15:50:09 2018 +0200
@@ -35,7 +35,7 @@
 
 #include "IDicomConnectionManager.h"
 
-#include <boost/thread/mutex.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 namespace Orthanc
 {
@@ -44,7 +44,6 @@
   private:
     class Resource;
 
-    boost::mutex                         mutex_;
     std::auto_ptr<DicomUserConnection>   connection_;
     boost::posix_time::ptime             lastUse_;
     boost::posix_time::time_duration     timeout_;
--- a/Core/JobsEngine/Operations/IJobOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/Core/JobsEngine/Operations/IJobOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -34,6 +34,7 @@
 #pragma once
 
 #include "JobOperationValues.h"
+#include "../../DicomNetworking/IDicomConnectionManager.h"
 
 namespace Orthanc
 {
@@ -45,6 +46,7 @@
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input) = 0;
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& dicomConnection) = 0;
   };
 }
--- a/Core/JobsEngine/Operations/LogJobOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/Core/JobsEngine/Operations/LogJobOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -40,12 +40,14 @@
 namespace Orthanc
 {
   void LogJobOperation::Apply(JobOperationValues& outputs,
-                              const JobOperationValue& input)
+                              const JobOperationValue& input,
+                              IDicomConnectionManager& connectionManager)
   {
     switch (input.GetType())
     {
       case JobOperationValue::Type_String:
-        LOG(INFO) << "Job value: " << dynamic_cast<const StringOperationValue&>(input).GetContent();
+        LOG(INFO) << "Job value: "
+                  << dynamic_cast<const StringOperationValue&>(input).GetContent();
         break;
 
       case JobOperationValue::Type_Null:
--- a/Core/JobsEngine/Operations/LogJobOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/Core/JobsEngine/Operations/LogJobOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -41,6 +41,7 @@
   {
   public:
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& connectionManager);
   };
 }
--- a/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp	Sat May 19 15:50:09 2018 +0200
@@ -101,7 +101,7 @@
       return currentInput_ >= originalInputs_.GetSize() + workInputs_.GetSize();
     }
 
-    void Step()
+    void Step(IDicomConnectionManager& connectionManager)
     {
       if (IsDone())
       {
@@ -120,7 +120,7 @@
       }
 
       JobOperationValues outputs;
-      operation_->Apply(outputs, *input);
+      operation_->Apply(outputs, *input, connectionManager);
 
       if (!nextOperations_.empty())
       {
@@ -175,6 +175,12 @@
     that_.trailingTimeout_ = boost::posix_time::milliseconds(timeout);
   }
 
+  
+  void SequenceOfOperationsJob::Lock::SetDicomConnectionTimeout(unsigned int timeout)
+  {
+    that_.connectionManager_.SetTimeout(timeout);
+  }
+
 
   size_t SequenceOfOperationsJob::Lock::AddOperation(IJobOperation* operation)
   {
@@ -255,6 +261,7 @@
           (*it)->SignalDone(*this);
         }
 
+        connectionManager_.Close();
         return JobStepResult::Success();
       }
       else
@@ -273,9 +280,11 @@
 
     if (current_ < operations_.size())
     {
-      operations_[current_]->Step();
+      operations_[current_]->Step(connectionManager_);
     }
 
+    connectionManager_.CheckTimeout();
+
     return JobStepResult::Continue();
   }
 
@@ -294,6 +303,13 @@
   }
 
 
+  void SequenceOfOperationsJob::ReleaseResources()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    connectionManager_.Close();
+  }
+
+
   float SequenceOfOperationsJob::GetProgress()
   {
     boost::mutex::scoped_lock lock(mutex_);
--- a/Core/JobsEngine/Operations/SequenceOfOperationsJob.h	Sat May 19 15:28:41 2018 +0200
+++ b/Core/JobsEngine/Operations/SequenceOfOperationsJob.h	Sat May 19 15:50:09 2018 +0200
@@ -36,6 +36,8 @@
 #include "../IJob.h"
 #include "IJobOperation.h"
 
+#include "../../DicomNetworking/TimeoutDicomConnectionManager.h"
+
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition_variable.hpp>
 
@@ -67,6 +69,7 @@
     boost::condition_variable         operationAdded_;
     boost::posix_time::time_duration  trailingTimeout_;
     std::list<IObserver*>             observers_;
+    TimeoutDicomConnectionManager     connectionManager_;
 
     void Setup();
 
@@ -110,6 +113,8 @@
 
       void SetTrailingOperationTimeout(unsigned int timeout);
 
+      void SetDicomConnectionTimeout(unsigned int timeout);
+      
       size_t AddOperation(IJobOperation* operation);
 
       size_t GetOperationsCount() const
@@ -132,9 +137,7 @@
 
     virtual void SignalResubmit();
 
-    virtual void ReleaseResources()
-    {
-    }
+    virtual void ReleaseResources();
 
     virtual float GetProgress();
 
--- a/OrthancServer/ServerJobs/DeleteResourceOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/DeleteResourceOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -42,7 +42,8 @@
 namespace Orthanc
 {
   void DeleteResourceOperation::Apply(JobOperationValues& outputs,
-                                      const JobOperationValue& input)
+                                      const JobOperationValue& input,
+                                      IDicomConnectionManager& connectionManager)
   {
     switch (input.GetType())
     {
--- a/OrthancServer/ServerJobs/DeleteResourceOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/DeleteResourceOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -51,7 +51,8 @@
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& connectionManager);
   };
 }
 
--- a/OrthancServer/ServerJobs/LuaJobManager.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.cpp	Sat May 19 15:50:09 2018 +0200
@@ -47,16 +47,6 @@
 
 namespace Orthanc
 {
-  void LuaJobManager::ConnectionTimeoutThread(LuaJobManager* manager)
-  {
-    while (manager->continue_)
-    {
-      manager->connectionManager_.CheckTimeout();
-      boost::this_thread::sleep(boost::posix_time::milliseconds(100));
-    }
-  }
-
-    
   void LuaJobManager::SignalDone(const SequenceOfOperationsJob& job)
   {
     boost::mutex::scoped_lock lock(mutex_);
@@ -72,21 +62,8 @@
   LuaJobManager::LuaJobManager() :
     currentJob_(NULL),
     maxOperations_(1000),
-    priority_(0),
-    continue_(true)
+    priority_(0)
   {
-    connectionTimeoutThread_ = boost::thread(ConnectionTimeoutThread, this);
-  }
-
-
-  LuaJobManager::~LuaJobManager()
-  {
-    continue_ = false;
-
-    if (connectionTimeoutThread_.joinable())
-    {
-      connectionTimeoutThread_.join();
-    }
   }
 
 
@@ -180,8 +157,7 @@
                                                    const RemoteModalityParameters& modality)
   {
     assert(jobLock_.get() != NULL);
-    return jobLock_->AddOperation
-      (new StoreScuOperation(localAet, modality, that_.connectionManager_));    
+    return jobLock_->AddOperation(new StoreScuOperation(localAet, modality));    
   }
 
 
--- a/OrthancServer/ServerJobs/LuaJobManager.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.h	Sat May 19 15:50:09 2018 +0200
@@ -45,25 +45,18 @@
   class LuaJobManager : private SequenceOfOperationsJob::IObserver
   {
   private:
-    boost::mutex                   mutex_;
-    TimeoutDicomConnectionManager  connectionManager_;
-    std::string                    currentId_;
-    SequenceOfOperationsJob*       currentJob_;
-    size_t                         maxOperations_;
-    int                            priority_;
-    unsigned int                   trailingTimeout_;
-    bool                           continue_;
-    boost::thread                  connectionTimeoutThread_;
+    boost::mutex              mutex_;
+    std::string               currentId_;
+    SequenceOfOperationsJob*  currentJob_;
+    size_t                    maxOperations_;
+    int                       priority_;
+    unsigned int              trailingTimeout_;
 
-    static void ConnectionTimeoutThread(LuaJobManager* manager);
-    
     virtual void SignalDone(const SequenceOfOperationsJob& job);
 
   public:
     LuaJobManager();
 
-    ~LuaJobManager();
-
     void SetMaxOperationsPerJob(size_t count);
 
     void SetPriority(int priority);
--- a/OrthancServer/ServerJobs/ModifyInstanceOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/ModifyInstanceOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -79,7 +79,8 @@
   }
 
   void ModifyInstanceOperation::Apply(JobOperationValues& outputs,
-                                      const JobOperationValue& input)
+                                      const JobOperationValue& input,
+                                      IDicomConnectionManager& connectionManager)
   {
     if (input.GetType() != JobOperationValue::Type_DicomInstance)
     {
--- a/OrthancServer/ServerJobs/ModifyInstanceOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/ModifyInstanceOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -58,7 +58,8 @@
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& connectionManager);
   };
 }
 
--- a/OrthancServer/ServerJobs/StorePeerOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/StorePeerOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -43,7 +43,8 @@
 namespace Orthanc
 {
   void StorePeerOperation::Apply(JobOperationValues& outputs,
-                                const JobOperationValue& input)
+                                const JobOperationValue& input,
+                                 IDicomConnectionManager& connectionManager)
   {
     // Configure the HTTP client
     HttpClient client(peer_, "instances");
@@ -54,7 +55,8 @@
       throw OrthancException(ErrorCode_BadParameterType);
     }
 
-    const DicomInstanceOperationValue& instance = dynamic_cast<const DicomInstanceOperationValue&>(input);
+    const DicomInstanceOperationValue& instance =
+      dynamic_cast<const DicomInstanceOperationValue&>(input);
 
     LOG(INFO) << "Lua: Sending instance " << instance.GetId() << " to Orthanc peer \"" 
               << peer_.GetUrl() << "\"";
@@ -66,16 +68,16 @@
       std::string answer;
       if (!client.Apply(answer))
       {
-        LOG(ERROR) << "Lua: Unable to send instance " << instance.GetId() << " to Orthanc peer \"" 
-                   << peer_.GetUrl();
+        LOG(ERROR) << "Lua: Unable to send instance " << instance.GetId()
+                   << " to Orthanc peer \"" << peer_.GetUrl();
       }
 
       outputs.Append(input.Clone());
     }
     catch (OrthancException& e)
     {
-      LOG(ERROR) << "Lua: Unable to send instance " << instance.GetId() << " to Orthanc peer \"" 
-                 << peer_.GetUrl() << "\": " << e.What();
+      LOG(ERROR) << "Lua: Unable to send instance " << instance.GetId()
+                 << " to Orthanc peer \"" << peer_.GetUrl() << "\": " << e.What();
     }
   }
 }
--- a/OrthancServer/ServerJobs/StorePeerOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/StorePeerOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -50,7 +50,8 @@
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& connectionManager);
   };
 }
 
--- a/OrthancServer/ServerJobs/StoreScuOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/StoreScuOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -42,10 +42,11 @@
 namespace Orthanc
 {
   void StoreScuOperation::Apply(JobOperationValues& outputs,
-                                const JobOperationValue& input)
+                                const JobOperationValue& input,
+                                IDicomConnectionManager& connectionManager)
   {
     std::auto_ptr<IDicomConnectionManager::IResource> resource
-      (manager_.AcquireConnection(localAet_, modality_));
+      (connectionManager.AcquireConnection(localAet_, modality_));
 
     if (resource.get() == NULL)
     {
--- a/OrthancServer/ServerJobs/StoreScuOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/StoreScuOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -36,7 +36,6 @@
 #include "../../Core/JobsEngine/Operations/IJobOperation.h"
 
 #include "../../Core/DicomNetworking/RemoteModalityParameters.h"
-#include "../../Core/DicomNetworking/IDicomConnectionManager.h"
 
 namespace Orthanc
 {
@@ -45,19 +44,18 @@
   private:
     std::string               localAet_;
     RemoteModalityParameters  modality_;
-    IDicomConnectionManager&  manager_;
+    
   public:
     StoreScuOperation(const std::string& localAet,
-                      const RemoteModalityParameters& modality,
-                      IDicomConnectionManager& manager) :
+                      const RemoteModalityParameters& modality) :
       localAet_(localAet),
-      modality_(modality),
-      manager_(manager)
+      modality_(modality)
     {
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& manager);
   };
 }
 
--- a/OrthancServer/ServerJobs/SystemCallOperation.cpp	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/SystemCallOperation.cpp	Sat May 19 15:50:09 2018 +0200
@@ -44,7 +44,8 @@
 namespace Orthanc
 {
   void SystemCallOperation::Apply(JobOperationValues& outputs,
-                                  const JobOperationValue& input)
+                                  const JobOperationValue& input,
+                                  IDicomConnectionManager& connectionManager)
   {
     std::vector<std::string> arguments = preArguments_;
 
--- a/OrthancServer/ServerJobs/SystemCallOperation.h	Sat May 19 15:28:41 2018 +0200
+++ b/OrthancServer/ServerJobs/SystemCallOperation.h	Sat May 19 15:50:09 2018 +0200
@@ -72,7 +72,8 @@
     }
 
     virtual void Apply(JobOperationValues& outputs,
-                       const JobOperationValue& input);
+                       const JobOperationValue& input,
+                       IDicomConnectionManager& connectionManager);
   };
 }