Salome HOME
Copyright update 2021
[tools/libbatch.git] / src / Core / BatchManager.cxx
index 874680727ff568d3f02aee45c49ccdd432b674ba..00ef07a0c3baf433a77ba5c5a15d4f11ef939aea 100644 (file)
@@ -1,23 +1,23 @@
-//  Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  This library 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
-//  Lesser General Public License for more details.
+// This library 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
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 /*
  * BatchManager.cxx :
@@ -78,52 +78,54 @@ namespace Batch {
   // Methode pour le controle des jobs : soumet un job au gestionnaire
   const JobId BatchManager::submitJob(const Job & job)
   {
-    throw NotYetImplementedException("Method submitJob not implemented by Batch Manager \"" + _type + "\"");
+    exportInputFiles(job);
+    preprocess(job);
+    return runJob(job);
   }
 
   // Methode pour le controle des jobs : retire un job du gestionnaire
-  void BatchManager::deleteJob(const JobId & jobid)
+  void BatchManager::deleteJob(const JobId & /*jobid*/)
   {
     throw NotYetImplementedException("Method deleteJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : suspend un job en file d'attente
-  void BatchManager::holdJob(const JobId & jobid)
+  void BatchManager::holdJob(const JobId & /*jobid*/)
   {
     throw NotYetImplementedException("Method holdJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : relache un job suspendu
-  void BatchManager::releaseJob(const JobId & jobid)
+  void BatchManager::releaseJob(const JobId & /*jobid*/)
   {
     throw NotYetImplementedException("Method releaseJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : modifie un job en file d'attente
-  void BatchManager::alterJob(const JobId & jobid, const Parametre & param, const Environnement & env)
+  void BatchManager::alterJob(const JobId & /*jobid*/, const Parametre & /*param*/, const Environnement & /*env*/)
   {
     throw NotYetImplementedException("Method alterJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : modifie un job en file d'attente
-  void BatchManager::alterJob(const JobId & jobid, const Parametre & param)
+  void BatchManager::alterJob(const JobId & /*jobid*/, const Parametre & /*param*/)
   {
     throw NotYetImplementedException("Method alterJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : modifie un job en file d'attente
-  void BatchManager::alterJob(const JobId & jobid, const Environnement & env)
+  void BatchManager::alterJob(const JobId & /*jobid*/, const Environnement & /*env*/)
   {
     throw NotYetImplementedException("Method alterJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
   // Methode pour le controle des jobs : renvoie l'etat du job
-  JobInfo BatchManager::queryJob(const JobId & jobid)
+  JobInfo BatchManager::queryJob(const JobId & /*jobid*/)
   {
     throw NotYetImplementedException("Method queryJob not implemented by Batch Manager \"" + _type + "\"");
   }
 
-  const JobId BatchManager::addJob(const Job & job, const string & reference)
+  const JobId BatchManager::addJob(const Job & /*job*/, const string & reference)
   {
     return JobId(this, reference);
   }
@@ -254,8 +256,9 @@ namespace Batch {
       CoupleType cpt  = *static_cast< CoupleType * >(*Vit);
       Couple outputFile = cpt;
       string remotePath = outputFile.getRemote();
-      if (!Utils::isAbsolutePath(remotePath)) {
-        remotePath = params[WORKDIR].str() + "/" + remotePath;
+      if (!Utils::isAbsolutePath(remotePath) && !Utils::isOption(remotePath)) {
+        // rsync creates the whole tree after /./ in the destination folder
+        remotePath = params[WORKDIR].str() + "/./" + remotePath;
       }
       string localPath = outputFile.getLocal();
       if (!Utils::isAbsolutePath(localPath)) {
@@ -293,6 +296,43 @@ namespace Batch {
     return ret;
   }
 
+  bool BatchManager::importWorkFile( const Job & job,
+                                     const std::string& work_file,
+                                     const std::string& directory )
+  {
+    Parametre params = job.getParametre();
+
+    // Create local result directory
+    int status = CommunicationProtocol::getInstance(SH).makeDirectory(directory, "", "");
+    if (status)
+      LOG("Directory creation failed. Status is: " << status);
+
+    bool ret = true;
+    status = _protocol.copyFile(string(params[WORKDIR]) + "/" + work_file,
+                                _hostname, _username,
+                                directory, "", "");
+    if (status) {
+      LOG("Copy command failed. Status is: " << status);
+      ret = false;
+    }
+    return ret;
+  }
+
+  void BatchManager::clearWorkingDir( const Job & job)
+  {
+    Parametre params = job.getParametre();
+
+    string wd = params[WORKDIR];
+    if(!wd.empty() && wd != "/")
+    {
+      int status = _protocol.removeDirectory(wd, _hostname, _username);
+      if (status)
+        LOG("removeDirectory command failed. Status is: " << status);
+    }
+    else
+      LOG("removeDirectory command failed. Invalid working directory: " << wd);
+  }
+
   MpiImpl *BatchManager::FactoryMpiImpl(string mpiImpl)
   {
     if(mpiImpl == "lam")
@@ -323,4 +363,35 @@ namespace Batch {
     return _protocol;
   }
 
+  void BatchManager::preprocess(const Batch::Job & job)
+  {
+    std::string preproCommand;
+    std::string workDir;
+    Parametre params = job.getParametre();
+    if (params.find(PREPROCESS) != params.end())
+      preproCommand = params[PREPROCESS].str();
+    if (params.find(WORKDIR) != params.end())
+      workDir = params[WORKDIR].str();
+
+    if(!preproCommand.empty() && !workDir.empty())
+    {
+      std::string subCommand = "cd " + workDir + "; " + preproCommand;
+      std::string command = _protocol.getExecCommand(subCommand, _hostname, _username);
+      command += " 2>&1";
+      LOG(command);
+
+      // submit job
+      std::string output;
+      int status = Utils::getCommandOutput(command, output);
+      LOG(output);
+      if (status != 0)
+        throw RunTimeException("Error when executing: " + command +
+                               "\nOutput:" + output);
+    }
+  }
+  
+  const Batch::JobId BatchManager::runJob(const Batch::Job & /*job*/)
+  {
+    throw NotYetImplementedException("Method runJob not implemented by Batch Manager \"" + _type + "\"");
+  }
 }