changeset 2606:f2b9d3256060 jobs

SystemCallOperation
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 19 May 2018 15:10:26 +0200
parents 1e11b0229e04
children 44e268605478
files CMakeLists.txt OrthancServer/ServerJobs/LuaJobManager.cpp OrthancServer/ServerJobs/LuaJobManager.h OrthancServer/ServerJobs/StorePeerOperation.cpp OrthancServer/ServerJobs/StoreScuOperation.cpp OrthancServer/ServerJobs/SystemCallOperation.cpp OrthancServer/ServerJobs/SystemCallOperation.h UnitTestsSources/MultiThreadingTests.cpp
diffstat 8 files changed, 226 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri May 18 18:06:53 2018 +0200
+++ b/CMakeLists.txt	Sat May 19 15:10:26 2018 +0200
@@ -96,6 +96,7 @@
   OrthancServer/ServerJobs/OrthancPeerStoreJob.cpp
   OrthancServer/ServerJobs/StorePeerOperation.cpp
   OrthancServer/ServerJobs/StoreScuOperation.cpp
+  OrthancServer/ServerJobs/SystemCallOperation.cpp
   OrthancServer/ServerToolbox.cpp
   OrthancServer/SliceOrdering.cpp
   )
--- a/OrthancServer/ServerJobs/LuaJobManager.cpp	Fri May 18 18:06:53 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.cpp	Sat May 19 15:10:26 2018 +0200
@@ -37,6 +37,7 @@
 #include "DeleteResourceOperation.h"
 #include "StorePeerOperation.h"
 #include "StoreScuOperation.h"
+#include "SystemCallOperation.h"
 #include "../../Core/JobsEngine/Operations/LogJobOperation.h"
 
 #include "DicomInstanceOperationValue.h"
@@ -178,7 +179,8 @@
                                                    const RemoteModalityParameters& modality)
   {
     assert(jobLock_.get() != NULL);
-    return jobLock_->AddOperation(new StoreScuOperation(localAet, modality, that_.connectionManager_));    
+    return jobLock_->AddOperation
+      (new StoreScuOperation(localAet, modality, that_.connectionManager_));    
   }
 
 
@@ -189,6 +191,24 @@
   }
 
 
+  size_t LuaJobManager::Lock::AddSystemCallOperation(const std::string& command)
+  {
+    assert(jobLock_.get() != NULL);
+    return jobLock_->AddOperation(new SystemCallOperation(command));    
+  }
+    
+
+  size_t LuaJobManager::Lock::AddSystemCallOperation
+  (const std::string& command,
+   const std::vector<std::string>& preArguments,
+   const std::vector<std::string>& postArguments)
+  {
+    assert(jobLock_.get() != NULL);
+    return jobLock_->AddOperation
+      (new SystemCallOperation(command, preArguments, postArguments));
+  }
+
+
   void LuaJobManager::Lock::AddNullInput(size_t operation)
   {
     assert(jobLock_.get() != NULL);
--- a/OrthancServer/ServerJobs/LuaJobManager.h	Fri May 18 18:06:53 2018 +0200
+++ b/OrthancServer/ServerJobs/LuaJobManager.h	Sat May 19 15:10:26 2018 +0200
@@ -93,6 +93,12 @@
 
       size_t AddStorePeerOperation(const WebServiceParameters& peer);
 
+      size_t AddSystemCallOperation(const std::string& command);
+
+      size_t AddSystemCallOperation(const std::string& command,
+                                    const std::vector<std::string>& preArguments,
+                                    const std::vector<std::string>& postArguments);
+
       void AddNullInput(size_t operation); 
 
       void AddStringInput(size_t operation,
--- a/OrthancServer/ServerJobs/StorePeerOperation.cpp	Fri May 18 18:06:53 2018 +0200
+++ b/OrthancServer/ServerJobs/StorePeerOperation.cpp	Sat May 19 15:10:26 2018 +0200
@@ -70,7 +70,7 @@
                    << peer_.GetUrl();
       }
 
-      outputs.Append(instance.Clone());
+      outputs.Append(input.Clone());
     }
     catch (OrthancException& e)
     {
--- a/OrthancServer/ServerJobs/StoreScuOperation.cpp	Fri May 18 18:06:53 2018 +0200
+++ b/OrthancServer/ServerJobs/StoreScuOperation.cpp	Sat May 19 15:10:26 2018 +0200
@@ -58,7 +58,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 modality \"" 
               << modality_.GetApplicationEntityTitle() << "\"";
@@ -68,7 +69,7 @@
       std::string dicom;
       instance.ReadContent(dicom);
       resource->GetConnection().Store(dicom);
-      outputs.Append(instance.Clone());
+      outputs.Append(input.Clone());
     }
     catch (OrthancException& e)
     {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/ServerJobs/SystemCallOperation.cpp	Sat May 19 15:10:26 2018 +0200
@@ -0,0 +1,114 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#include "../PrecompiledHeadersServer.h"
+#include "SystemCallOperation.h"
+
+#include "DicomInstanceOperationValue.h"
+
+#include "../../Core/JobsEngine/Operations/StringOperationValue.h"
+#include "../../Core/Logging.h"
+#include "../../Core/TemporaryFile.h"
+#include "../../Core/Toolbox.h"
+
+namespace Orthanc
+{
+  void SystemCallOperation::Apply(JobOperationValues& outputs,
+                                  const JobOperationValue& input)
+  {
+    std::vector<std::string> arguments = preArguments_;
+
+    arguments.reserve(arguments.size() + postArguments_.size() + 1);
+
+    std::auto_ptr<TemporaryFile> tmp;
+    
+    switch (input.GetType())
+    {
+      case JobOperationValue::Type_DicomInstance:
+      {
+        const DicomInstanceOperationValue& instance =
+          dynamic_cast<const DicomInstanceOperationValue&>(input);
+
+        std::string dicom;
+        instance.ReadContent(dicom);
+
+        tmp.reset(new TemporaryFile);
+        tmp->Write(dicom);
+        
+        arguments.push_back(tmp->GetPath());
+        break;
+      }
+
+      case JobOperationValue::Type_String:
+      {
+        const StringOperationValue& value =
+          dynamic_cast<const StringOperationValue&>(input);
+
+        arguments.push_back(value.GetContent());
+        break;
+      }
+
+      case JobOperationValue::Type_Null:
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_BadParameterType);
+    }
+
+    for (size_t i = 0; i < postArguments_.size(); i++)
+    {
+      arguments.push_back(postArguments_[i]);
+    }
+
+    std::string info = command_;
+    for (size_t i = 0; i < arguments.size(); i++)
+    {
+      info += " " + arguments[i];
+    }
+    
+    LOG(INFO) << "Lua: System call: " << info;
+
+    try
+    {
+      SystemToolbox::ExecuteSystemCommand(command_, arguments);
+
+      // Only chain with other commands if this operation succeeds
+      outputs.Append(input.Clone());
+    }
+    catch (OrthancException& e)
+    {
+      LOG(ERROR) << "Lua: Failed system call - " << info << ": " << e.What();
+    }
+  }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/OrthancServer/ServerJobs/SystemCallOperation.h	Sat May 19 15:10:26 2018 +0200
@@ -0,0 +1,78 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2018 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "../../Core/JobsEngine/Operations/IJobOperation.h"
+
+#include <string>
+
+namespace Orthanc
+{
+  class SystemCallOperation : public IJobOperation
+  {
+  private:
+    std::string               command_;
+    std::vector<std::string>  preArguments_;
+    std::vector<std::string>  postArguments_;
+    
+  public:
+    SystemCallOperation(const std::string& command) :
+      command_(command)
+    {
+    }
+
+    SystemCallOperation(const std::string& command,
+                        const std::vector<std::string>& preArguments,
+                        const std::vector<std::string>& postArguments) :
+      command_(command),
+      preArguments_(preArguments),
+      postArguments_(postArguments)
+    {
+    }
+
+    void AddPreArgument(const std::string& argument)
+    {
+      preArguments_.push_back(argument);
+    }
+
+    void AddPostArgument(const std::string& argument)
+    {
+      postArguments_.push_back(argument);
+    }
+
+    virtual void Apply(JobOperationValues& outputs,
+                       const JobOperationValue& input);
+  };
+}
+
--- a/UnitTestsSources/MultiThreadingTests.cpp	Fri May 18 18:06:53 2018 +0200
+++ b/UnitTestsSources/MultiThreadingTests.cpp	Sat May 19 15:10:26 2018 +0200
@@ -799,9 +799,11 @@
     LuaJobManager::Lock lock(lua, engine);
     size_t a = lock.AddLogOperation();
     size_t b = lock.AddLogOperation();
+    size_t c = lock.AddSystemCallOperation("echo");
     lock.AddStringInput(a, boost::lexical_cast<std::string>(i));
     lock.AddNullInput(a);
     lock.Connect(a, b);
+    lock.Connect(a, c);
   }
 
   boost::this_thread::sleep(boost::posix_time::milliseconds(2000));