Salome HOME
in session_attached mode synchronize local NS with content of salome.[cm,rm,esm,dsm]
[modules/kernel.git] / src / Launcher / Launcher.cxx
index 96a1fafd0a87ce5abcfd6a59f466fb543b182944..f673496f8de112525501b4829d4d390dc360027a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  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
@@ -25,6 +25,7 @@
 #include <sstream>
 #include <sys/stat.h>
 #include <time.h>
+#include <memory>
 
 #ifdef WITH_LIBBATCH
 #include <libbatch/BatchManagerCatalog.hxx>
 #include "SALOME_Launcher_Handler.hxx"
 #include "Launcher.hxx"
 #include "Launcher_Job_Command.hxx"
+#include "Launcher_Job_YACSFile.hxx"
+#include "Launcher_Job_PythonSALOME.hxx"
+#include "Launcher_Job_CommandSALOME.hxx"
 #include "Launcher_XML_Persistence.hxx"
 
 using namespace std;
 
 //=============================================================================
-/*! 
+/*!
  *  Constructor
  *  \param orb
  *  Define a CORBA single thread policy for the server, which avoid to deal
@@ -56,7 +60,7 @@ Launcher_cpp::Launcher_cpp()
 }
 
 //=============================================================================
-/*! 
+/*!
  * destructor
  */
 //=============================================================================
@@ -77,10 +81,10 @@ Launcher_cpp::~Launcher_cpp()
 
 //=============================================================================
 /*!
- * Add a job into the launcher - check resource and choose one 
- */ 
+ * Add a job into the launcher - check resource and choose one
+ */
 //=============================================================================
-void 
+void
 Launcher_cpp::createJob(Launcher::Job * new_job)
 {
   LAUNCHER_MESSAGE("Creating a new job");
@@ -95,7 +99,6 @@ Launcher_cpp::createJob(Launcher::Job * new_job)
   else
   {
     LAUNCHER_INFOS("A job has already the same id: " << new_job->getNumber());
-    delete new_job;
     throw LauncherException("A job has already the same id - job is not created !");
   }
   LAUNCHER_MESSAGE("New Job created");
@@ -103,10 +106,81 @@ Launcher_cpp::createJob(Launcher::Job * new_job)
 
 //=============================================================================
 /*!
- * Launch a job 
- */ 
+ * Add a job into the launcher - check resource and choose one
+ */
 //=============================================================================
-void 
+int
+Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
+{
+  std::string job_type = job_parameters.job_type;
+  Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
+
+  if (job_type == Launcher::Job_Command::TYPE_NAME)
+    new_job = new Launcher::Job_Command();
+  else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME)
+    new_job = new Launcher::Job_CommandSALOME();
+  else if (job_type == Launcher::Job_YACSFile::TYPE_NAME)
+    new_job = new Launcher::Job_YACSFile();
+  else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME)
+    new_job = new Launcher::Job_PythonSALOME();
+  else
+  {
+    std::string message("Launcher_cpp::createJob: bad job type: ");
+    message += job_type;
+    throw LauncherException(message.c_str());
+  }
+
+  // Name
+  new_job->setJobName(job_parameters.job_name);
+
+  // Directories
+  new_job->setWorkDirectory(job_parameters.work_directory);
+  new_job->setLocalDirectory(job_parameters.local_directory);
+  new_job->setResultDirectory(job_parameters.result_directory);
+
+  // Parameters for COORM
+  new_job->setLauncherFile(job_parameters.launcher_file);
+  new_job->setLauncherArgs(job_parameters.launcher_args);
+
+  // Job File
+  new_job->setJobFile(job_parameters.job_file);
+  new_job->setPreCommand(job_parameters.pre_command);
+
+  // Files
+  new_job->setEnvFile(job_parameters.env_file);
+  for(const std::string param : job_parameters.in_files)
+    new_job->add_in_file(param);
+  for(const std::string param : job_parameters.out_files)
+    new_job->add_out_file(param);
+
+  new_job->setMaximumDuration(job_parameters.maximum_duration);
+  new_job->setQueue(job_parameters.queue);
+  new_job->setPartition(job_parameters.partition);
+  new_job->setExclusive(job_parameters.exclusive);
+  new_job->setMemPerCpu(job_parameters.mem_per_cpu);
+  new_job->setWCKey(job_parameters.wckey);
+  new_job->setExtraParams(job_parameters.extra_params);
+
+  // Resources requirements
+  new_job->setResourceRequiredParams(job_parameters.resource_required);
+
+  // Adding specific parameters to the job
+  for (const auto& it_specific : job_parameters.specific_parameters)
+    new_job->addSpecificParameter(it_specific.first, it_specific.second);
+
+  new_job->checkSpecificParameters();
+
+  createJob(new_job);
+
+  return new_job->getNumber();
+}
+
+//=============================================================================
+/*!
+ * Launch a job
+ */
+//=============================================================================
+void
 Launcher_cpp::launchJob(int job_id)
 {
   LAUNCHER_MESSAGE("Launch a job");
@@ -140,9 +214,9 @@ Launcher_cpp::launchJob(int job_id)
 //=============================================================================
 /*!
  * Get job state
- */ 
+ */
 //=============================================================================
-const char *
+std::string
 Launcher_cpp::getJobState(int job_id)
 {
   LAUNCHER_MESSAGE("Get job state");
@@ -161,7 +235,7 @@ Launcher_cpp::getJobState(int job_id)
     throw LauncherException(ex.message.c_str());
   }
 
-  return state.c_str();
+  return state;
 }
 
 //=============================================================================
@@ -169,7 +243,7 @@ Launcher_cpp::getJobState(int job_id)
  * Get job assigned hostnames
  */
 //=============================================================================
-const char *
+std::string
 Launcher_cpp::getAssignedHostnames(int job_id)
 {
   LAUNCHER_MESSAGE("Get job assigned hostnames");
@@ -178,13 +252,40 @@ Launcher_cpp::getAssignedHostnames(int job_id)
   Launcher::Job * job = findJob(job_id);
   std::string assigned_hostnames = job->getAssignedHostnames();
 
-  return assigned_hostnames.c_str();
+  return assigned_hostnames;
+}
+
+void
+Launcher_cpp::exportInputFiles(int job_id)
+{
+  LAUNCHER_MESSAGE("Copy in files");
+  // Check if job exists
+  Launcher::Job * job = findJob(job_id);
+
+  // Check job state (cannot launch a job already launched...)
+  if (job->getState() != "CREATED")
+  {
+    LAUNCHER_INFOS("Bad state of the job: " << job->getState());
+    throw LauncherException("Bad state of the job: " + job->getState());
+  }
+
+  Batch::BatchManager * bm = getBatchManager(job);
+
+  try {
+    bm->exportInputFiles(*(job->getBatchJob()));
+  }
+  catch(const Batch::GenericException &ex)
+  {
+    LAUNCHER_INFOS("Failed to copy in files: " << ex.message);
+    throw LauncherException(ex.message.c_str());
+  }
+  LAUNCHER_MESSAGE("Files copied");
 }
 
 //=============================================================================
 /*!
  * Get Job result - the result directory could be changed
- */ 
+ */
 //=============================================================================
 void
 Launcher_cpp::getJobResults(int job_id, std::string directory)
@@ -193,7 +294,7 @@ Launcher_cpp::getJobResults(int job_id, std::string directory)
 
   Launcher::Job * job = findJob(job_id);
   std::string resource_name = job->getResourceDefinition().Name;
-  try 
+  try
   {
     if (directory != "")
       _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
@@ -234,7 +335,7 @@ Launcher_cpp::clearJobWorkingDir(int job_id)
 //=============================================================================
 /*!
  * Get Job dump state - the result directory could be changed
- */ 
+ */
 //=============================================================================
 bool
 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
@@ -244,7 +345,7 @@ Launcher_cpp::getJobDumpState(int job_id, std::string directory)
 
   Launcher::Job * job = findJob(job_id);
   std::string resource_name = job->getResourceDefinition().Name;
-  try 
+  try
   {
     if (directory != "")
       rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
@@ -294,7 +395,7 @@ Launcher_cpp::getJobWorkFile(int job_id,
 //=============================================================================
 /*!
  * Remove the job - into the Launcher and its batch manager
- */ 
+ */
 //=============================================================================
 void
 Launcher_cpp::removeJob(int job_id)
@@ -317,7 +418,7 @@ Launcher_cpp::removeJob(int job_id)
 //=============================================================================
 /*!
  * stop the job
- */ 
+ */
 //=============================================================================
 void
 Launcher_cpp::stopJob(int job_id)
@@ -341,7 +442,7 @@ int
 Launcher_cpp::restoreJob(const std::string& dumpedJob)
 {
   LAUNCHER_MESSAGE("restore Job");
-  Launcher::Job * new_job=NULL;
+  Launcher::Job* new_job(nullptr);
   int jobId = -1;
   try
   {
@@ -351,27 +452,69 @@ Launcher_cpp::restoreJob(const std::string& dumpedJob)
     if(new_job)
     {
       jobId = addJob(new_job);
-      if(jobId < 0)
-        delete new_job;
     }
+    else
+      LAUNCHER_INFOS("Failed to create a new job.");
   }
   catch(const LauncherException &ex)
   {
     LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
-    if(new_job)
-      delete new_job;
   }
   return jobId;
 }
 
+JobParameters_cpp
+Launcher_cpp::getJobParameters(int job_id)
+{
+  Launcher::Job * job = findJob(job_id);
+  JobParameters_cpp job_parameters;
+  job_parameters.job_name         = job->getJobName();
+  job_parameters.job_type         = job->getJobType();
+  job_parameters.job_file         = job->getJobFile();
+  job_parameters.env_file         = job->getEnvFile();
+  job_parameters.work_directory   = job->getWorkDirectory();
+  job_parameters.local_directory  = job->getLocalDirectory();
+  job_parameters.result_directory = job->getResultDirectory();
+  job_parameters.pre_command      = job->getPreCommand();
+
+  // Parameters for COORM
+  job_parameters.launcher_file = job->getLauncherFile();
+  job_parameters.launcher_args = job->getLauncherArgs();
+
+  job_parameters.in_files      = job->get_in_files();
+  job_parameters.out_files     = job->get_out_files();
+
+  job_parameters.maximum_duration = job->getMaximumDuration();
+  job_parameters.queue            = job->getQueue();
+  job_parameters.partition        = job->getPartition();
+  job_parameters.exclusive        = job->getExclusive();
+  job_parameters.mem_per_cpu      = job->getMemPerCpu();
+  job_parameters.wckey            = job->getWCKey();
+  job_parameters.extra_params     = job->getExtraParams();
+
+  resourceParams resource_params = job->getResourceRequiredParams();
+  job_parameters.resource_required.name             = resource_params.name;
+  job_parameters.resource_required.hostname         = resource_params.hostname;
+  job_parameters.resource_required.OS               = resource_params.OS;
+  job_parameters.resource_required.nb_proc          = resource_params.nb_proc;
+  job_parameters.resource_required.nb_node          = resource_params.nb_node;
+  job_parameters.resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
+  job_parameters.resource_required.cpu_clock        = resource_params.cpu_clock;
+  job_parameters.resource_required.mem_mb           = resource_params.mem_mb;
+
+  job_parameters.specific_parameters                = job->getSpecificParameters();
+
+  return job_parameters;
+}
+
 //=============================================================================
-/*! 
+/*!
  *  create a launcher job based on a file
  *  \param xmlExecuteFile     : to define the execution on the batch cluster
  */
 //=============================================================================
-long 
-Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile, 
+long
+Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
                                 const std::string clusterName)
 {
   LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
@@ -380,9 +523,9 @@ Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
   ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
 
   // Creating a new job
-  Launcher::Job_Command * new_job = new Launcher::Job_Command();
+  std::unique_ptr<Launcher::Job_Command> new_job(new Launcher::Job_Command);
 
-  std::string cmdFile = Kernel_Utils::GetTmpFileName();  
+  std::string cmdFile = Kernel_Utils::GetTmpFileName();
 #ifndef WIN32
   cmdFile += ".sh";
 #else
@@ -399,9 +542,9 @@ Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
   new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
   new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
 
-  for(int i=0; i < job_params.InputFile.size(); i++)
+  for(size_t i=0; i < job_params.InputFile.size(); i++)
     new_job->add_in_file(job_params.InputFile[i]);
-  for(int i=0; i < job_params.OutputFile.size();i++)
+  for(size_t i=0; i < job_params.OutputFile.size();i++)
     new_job->add_out_file(job_params.OutputFile[i]);
 
   resourceParams p;
@@ -415,14 +558,14 @@ Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
   p.mem_mb = 0;
   new_job->setResourceRequiredParams(p);
 
-  createJob(new_job);
+  createJob(new_job.get());
   return new_job->getNumber();
 }
 
 //=============================================================================
 /*!
  *  Factory to instantiate the good batch manager for chosen cluster.
- */ 
+ */
 //=============================================================================
 Batch::BatchManager *
 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
@@ -533,16 +676,23 @@ Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
 //----------------------------------------------------------
 #else
 
-void 
+void
 Launcher_cpp::createJob(Launcher::Job * new_job)
 {
   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
-  delete new_job;
   throw LauncherException("Method Launcher_cpp::createJob is not available "
                           "(libBatch was not present at compilation time)");
 }
 
-void 
+int
+Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
+{
+  LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
+  throw LauncherException("Method Launcher_cpp::createJob is not available "
+                          "(libBatch was not present at compilation time)");
+}
+
+void
 Launcher_cpp::launchJob(int job_id)
 {
   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
@@ -550,7 +700,7 @@ Launcher_cpp::launchJob(int job_id)
                           "(libBatch was not present at compilation time)");
 }
 
-const char *
+std::string
 Launcher_cpp::getJobState(int job_id)
 {
   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
@@ -558,7 +708,7 @@ Launcher_cpp::getJobState(int job_id)
                           "(libBatch was not present at compilation time)");
 }
 
-const char *
+std::string
 Launcher_cpp::getAssignedHostnames(int job_id)
 {
   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
@@ -566,6 +716,14 @@ Launcher_cpp::getAssignedHostnames(int job_id)
                           "(libBatch was not present at compilation time)");
 }
 
+void
+Launcher_cpp::exportInputFiles(int job_id)
+{
+  LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot copy the files!!!");
+  throw LauncherException("Method Launcher_cpp::exportInputFiles is not available "
+                          "(libBatch was not present at compilation time)");
+}
+
 void
 Launcher_cpp::getJobResults(int job_id, std::string directory)
 {
@@ -631,7 +789,7 @@ Launcher_cpp::restoreJob(const std::string& dumpedJob)
   return 0;
 }
 
-long 
+long
 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
 {
   throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
@@ -639,9 +797,16 @@ Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string c
   return 0;
 }
 
+JobParameters_cpp
+Launcher_cpp::getJobParameters(int job_id)
+{
+  throw LauncherException("Method Launcher_cpp::getJobParamaters is not available "
+                          "(libBatch was not present at compilation time)");
+}
+
 #endif
 
-ParserLauncherType 
+ParserLauncherType
 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
 {
   ParserLauncherType job_params;
@@ -688,6 +853,9 @@ Launcher_cpp::getJobs()
 Batch::BatchManager*
 Launcher_cpp::getBatchManager(Launcher::Job * job)
 {
+  if(!_ResManager)
+    throw LauncherException("Resource manager is not set.");
+
   Batch::BatchManager* result = nullptr;
   int job_id = job->getNumber();
 
@@ -756,7 +924,7 @@ Launcher_cpp::getBatchManager(Launcher::Job * job)
 }
 #endif
 
-void 
+void
 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
 {
   // Step 0: Calculated job_id
@@ -789,7 +957,6 @@ Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
   else
   {
     LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
-    delete new_job;
     throw LauncherException("A job as already the same id - job is not created !");
   }
   LAUNCHER_MESSAGE("New job added");
@@ -852,20 +1019,17 @@ Launcher_cpp::loadJobs(const char* jobs_file)
   list<Launcher::Job *>::const_iterator it_job;
   for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
   {
-    Launcher::Job * new_job = *it_job;
+    Launcher::Job* new_job(*it_job);
     int jobId = -1;
     try
     {
       jobId = addJob(new_job);
       if(jobId >= 0)
         new_jobs_id_list.push_back(jobId);
-      else
-        delete new_job;
     }
     catch(const LauncherException &ex)
     {
       LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
-      delete new_job;
     }
   }
 
@@ -900,4 +1064,4 @@ Launcher_cpp::findJob(int job_id)
   }
   Launcher::Job * job = it_job->second;
   return job;
-}
\ No newline at end of file
+}