changeset 2609:f7a84b551ee4 jobs

switch Lua to new jobs engine
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 19 May 2018 16:29:00 +0200
parents 25225f0b4f33
children 3ff4c50647ea
files Core/DicomNetworking/TimeoutDicomConnectionManager.cpp OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h OrthancServer/ServerJobs/LuaJobManager.cpp OrthancServer/ServerJobs/LuaJobManager.h OrthancServer/ServerJobs/StorePeerOperation.cpp OrthancServer/ServerJobs/StoreScuOperation.cpp
diffstat 7 files changed, 64 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomNetworking/TimeoutDicomConnectionManager.cpp	Sat May 19 15:50:09 2018 +0200
+++ b/Core/DicomNetworking/TimeoutDicomConnectionManager.cpp	Sat May 19 16:29:00 2018 +0200
@@ -34,6 +34,7 @@
 #include "../PrecompiledHeaders.h"
 #include "TimeoutDicomConnectionManager.h"
 
+#include "../Logging.h"
 #include "../OrthancException.h"
 
 namespace Orthanc
@@ -82,7 +83,7 @@
     if (connection_.get() != NULL &&
         (GetNow() - lastUse_) >= timeout_)
     {
-      connection_.reset(NULL);
+      Close();
     }
   }
 
@@ -102,7 +103,13 @@
 
   void TimeoutDicomConnectionManager::Close()
   {
-    connection_.reset(NULL);
+    if (connection_.get() != NULL)
+    {
+      LOG(INFO) << "Closing inactive DICOM association with modality: "
+                << connection_->GetRemoteApplicationEntityTitle();
+
+      connection_.reset(NULL);
+    }
   }
 
 
--- a/OrthancServer/LuaScripting.cpp	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/LuaScripting.cpp	Sat May 19 16:29:00 2018 +0200
@@ -34,18 +34,13 @@
 #include "PrecompiledHeadersServer.h"
 #include "LuaScripting.h"
 
+#include "OrthancInitialization.h"
+#include "OrthancRestApi/OrthancRestApi.h"
 #include "ServerContext.h"
-#include "OrthancInitialization.h"
-#include "../Core/Lua/LuaFunctionCall.h"
+
 #include "../Core/HttpServer/StringHttpOutput.h"
 #include "../Core/Logging.h"
-
-#include "Scheduler/DeleteInstanceCommand.h"
-#include "Scheduler/StoreScuCommand.h"
-#include "Scheduler/StorePeerCommand.h"
-#include "Scheduler/ModifyInstanceCommand.h"
-#include "Scheduler/CallSystemCommand.h"
-#include "OrthancRestApi/OrthancRestApi.h"
+#include "../Core/Lua/LuaFunctionCall.h"
 
 #include <EmbeddedResources.h>
 
@@ -229,13 +224,14 @@
   }
 
 
-  IServerCommand* LuaScripting::ParseOperation(const std::string& operation,
-                                               const Json::Value& parameters)
+  size_t LuaScripting::ParseOperation(LuaJobManager::Lock& lock,
+                                      const std::string& operation,
+                                      const Json::Value& parameters)
   {
     if (operation == "delete")
     {
       LOG(INFO) << "Lua script to delete resource " << parameters["Resource"].asString();
-      return new DeleteInstanceCommand(context_);
+      return lock.AddDeleteResourceOperation(context_);
     }
 
     if (operation == "store-scu")
@@ -250,36 +246,29 @@
         localAet = context_.GetDefaultLocalApplicationEntityTitle();
       }
 
-      std::string modality = parameters["Modality"].asString();
-      LOG(INFO) << "Lua script to send resource " << parameters["Resource"].asString()
-                << " to modality " << modality << " using Store-SCU";
+      std::string name = parameters["Modality"].asString();
+      RemoteModalityParameters modality = Configuration::GetModalityUsingSymbolicName(name);
 
       // This is not a C-MOVE: No need to call "StoreScuCommand::SetMoveOriginator()"
-      return new StoreScuCommand(context_, localAet,
-                                 Configuration::GetModalityUsingSymbolicName(modality), true);
+      return lock.AddStoreScuOperation(localAet, modality);
     }
 
     if (operation == "store-peer")
     {
-      std::string peer = parameters["Peer"].asString();
-      LOG(INFO) << "Lua script to send resource " << parameters["Resource"].asString()
-                << " to peer " << peer << " using HTTP";
+      std::string name = parameters["Peer"].asString();
 
-      WebServiceParameters parameters;
-      Configuration::GetOrthancPeer(parameters, peer);
-      return new StorePeerCommand(context_, parameters, true);
+      WebServiceParameters peer;
+      Configuration::GetOrthancPeer(peer, name);
+
+      return lock.AddStorePeerOperation(peer);
     }
 
     if (operation == "modify")
     {
-      LOG(INFO) << "Lua script to modify resource " << parameters["Resource"].asString();
       std::auto_ptr<DicomModification> modification(new DicomModification);
       modification->ParseModifyRequest(parameters);
 
-      std::auto_ptr<ModifyInstanceCommand> command
-        (new ModifyInstanceCommand(context_, RequestOrigin_Lua, modification.release()));
-
-      return command.release();
+      return lock.AddModifyInstanceOperation(context_, modification.release());
     }
 
     if (operation == "call-system")
@@ -320,7 +309,10 @@
         }
       }
 
-      return new CallSystemCommand(context_, parameters["Command"].asString(), args);
+      std::string command = parameters["Command"].asString();
+      std::vector<std::string> postArgs;
+
+      return lock.AddSystemCallOperation(command, args, postArgs);
     }
 
     throw OrthancException(ErrorCode_ParameterOutOfRange);
@@ -344,8 +336,10 @@
       throw OrthancException(ErrorCode_InternalError);
     }
 
-    ServerJob job;
-    ServerCommandInstance* previousCommand = NULL;
+    LuaJobManager::Lock lock(jobManager_, context_.GetJobsEngine());
+
+    bool isFirst = true;
+    size_t previous;
 
     for (Json::Value::ArrayIndex i = 0; i < operations.size(); ++i)
     {
@@ -356,30 +350,27 @@
       }
 
       const Json::Value& parameters = operations[i];
-      std::string operation = parameters["Operation"].asString();
-
-      ServerCommandInstance& command = job.AddCommand(ParseOperation(operation, operations[i]));
-        
       if (!parameters.isMember("Resource"))
       {
         throw OrthancException(ErrorCode_InternalError);
       }
 
+      std::string operation = parameters["Operation"].asString();
+      size_t index = ParseOperation(lock, operation, operations[i]);
+        
       std::string resource = parameters["Resource"].asString();
-      if (resource.empty())
+      if (!resource.empty())
       {
-        previousCommand->ConnectOutput(command);
+        lock.AddDicomInstanceInput(index, context_, resource);
       }
-      else 
+      else if (!isFirst)
       {
-        command.AddInput(resource);
+        lock.Connect(previous, index);
       }
 
-      previousCommand = &command;
+      isFirst = false;
+      previous = index;
     }
-
-    job.SetDescription(description);
-    context_.GetScheduler().Submit(job);
   }
 
 
--- a/OrthancServer/LuaScripting.h	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/LuaScripting.h	Sat May 19 16:29:00 2018 +0200
@@ -34,8 +34,10 @@
 #pragma once
 
 #include "IServerListener.h"
+
+#include "ServerJobs/LuaJobManager.h"
+
 #include "../Core/Lua/LuaContext.h"
-#include "Scheduler/IServerCommand.h"
 
 namespace Orthanc
 {
@@ -59,8 +61,9 @@
                                const Json::Value& metadata,
                                const DicomInstanceToStore& instance);
 
-    IServerCommand* ParseOperation(const std::string& operation,
-                                   const Json::Value& parameters);
+    size_t ParseOperation(LuaJobManager::Lock& lock,
+                          const std::string& operation,
+                          const Json::Value& parameters);
 
     void InitializeJob();
 
@@ -68,9 +71,10 @@
 
     void OnStableResource(const ServerIndexChange& change);
 
-    boost::recursive_mutex    mutex_;
-    LuaContext      lua_;
-    ServerContext&  context_;
+    boost::recursive_mutex   mutex_;
+    LuaContext               lua_;
+    ServerContext&           context_;
+    LuaJobManager            jobManager_;
 
   public:
     class Locker : public boost::noncopyable
--- a/OrthancServer/ServerJobs/LuaJobManager.cpp	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.cpp	Sat May 19 16:29:00 2018 +0200
@@ -62,7 +62,8 @@
   LuaJobManager::LuaJobManager() :
     currentJob_(NULL),
     maxOperations_(1000),
-    priority_(0)
+    priority_(0),
+    trailingTimeout_(5000)
   {
   }
 
@@ -118,7 +119,7 @@
     {
       // Need to create a new job, as the previous one is either
       // finished, or is getting too long
-      that_.currentJob_ = new SequenceOfOperationsJob;
+      that_.currentJob_ = new SequenceOfOperationsJob("Lua");
       jobLock_.reset(new SequenceOfOperationsJob::Lock(*that_.currentJob_));
       jobLock_->SetTrailingOperationTimeout(that_.trailingTimeout_);
     }
--- a/OrthancServer/ServerJobs/LuaJobManager.h	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.h	Sat May 19 16:29:00 2018 +0200
@@ -33,15 +33,16 @@
 
 #pragma once
 
+#include "../../Core/DicomNetworking/TimeoutDicomConnectionManager.h"
 #include "../../Core/DicomParsing/DicomModification.h"
-#include "../../Core/DicomNetworking/TimeoutDicomConnectionManager.h"
 #include "../../Core/JobsEngine/JobsEngine.h"
 #include "../../Core/JobsEngine/Operations/SequenceOfOperationsJob.h"
-
-#include "../ServerContext.h"
+#include "../../Core/WebServiceParameters.h"
 
 namespace Orthanc
 {
+  class ServerContext;
+  
   class LuaJobManager : private SequenceOfOperationsJob::IObserver
   {
   private:
--- a/OrthancServer/ServerJobs/StorePeerOperation.cpp	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/ServerJobs/StorePeerOperation.cpp	Sat May 19 16:29:00 2018 +0200
@@ -71,13 +71,13 @@
         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();
     }
+
+    outputs.Append(input.Clone());
   }
 }
--- a/OrthancServer/ServerJobs/StoreScuOperation.cpp	Sat May 19 15:50:09 2018 +0200
+++ b/OrthancServer/ServerJobs/StoreScuOperation.cpp	Sat May 19 16:29:00 2018 +0200
@@ -70,12 +70,13 @@
       std::string dicom;
       instance.ReadContent(dicom);
       resource->GetConnection().Store(dicom);
-      outputs.Append(input.Clone());
     }
     catch (OrthancException& e)
     {
       LOG(ERROR) << "Lua: Unable to send instance " << instance.GetId() << " to modality \"" 
                  << modality_.GetApplicationEntityTitle() << "\": " << e.What();
     }
+
+    outputs.Append(input.Clone());
   }
 }