Salome HOME
Copyrights update 2015.
[modules/yacs.git] / src / Launcher / Launcher.cxx
index d42b3a3ba42e44fb457ef851d8a200fbabb4453e..404a2b8d0bc301d4bf2a53b5471b622cd458ab6d 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  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
@@ -6,7 +6,7 @@
 // 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.
+// 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
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
+#include <list>
+#include <iostream>
+#include <sstream>
+#include <sys/stat.h>
+#include <time.h>
+
 #ifdef WITH_LIBBATCH
 #include <libbatch/BatchManagerCatalog.hxx>
 #include <libbatch/FactBatchManager.hxx>
 #include "SALOME_Launcher_Handler.hxx"
 #include "Launcher.hxx"
 #include "Launcher_Job_Command.hxx"
-#include <iostream>
-#include <sstream>
-#include <sys/stat.h>
-#include <time.h>
+#include "Launcher_XML_Persistence.hxx"
+
+using namespace std;
 
 //=============================================================================
 /*! 
@@ -141,6 +146,7 @@ Launcher_cpp::launchJob(int job_id)
     Batch::JobId batch_manager_job_id = _batchmap[job_id]->submitJob(*(job->getBatchJob()));
     job->setBatchManagerJobId(batch_manager_job_id);
     job->setState("QUEUED");
+    job->setReference(batch_manager_job_id.getReference());
   }
   catch(const Batch::GenericException &ex)
   {
@@ -454,7 +460,7 @@ Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
     case slurm:
       bmType = "SLURM";
       break;
-    case ssh_batch:
+    case none:
       bmType = "LOCAL";
       break;
     case ll:
@@ -515,6 +521,14 @@ Launcher_cpp::getJobState(int job_id)
                           "(libBatch was not present at compilation time)");
 }
 
+const char *
+Launcher_cpp::getAssignedHostnames(int job_id)
+{
+  LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
+  throw LauncherException("Method Launcher_cpp::getAssignedHostnames is not available "
+                          "(libBatch was not present at compilation time)");
+}
+
 void
 Launcher_cpp::getJobResults(int job_id, std::string directory)
 {
@@ -668,7 +682,7 @@ Launcher_cpp::createBatchManagerForJob(Launcher::Job * job)
 }
 
 void 
-Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job_reference)
+Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
 {
   // Step 0: Calculated job_id
   pthread_mutex_lock(_job_cpt_mutex);
@@ -685,7 +699,7 @@ Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job
   try
   {
     Batch::JobId batch_manager_job_id = _batchmap[job_id]->addJob(*(new_job->getBatchJob()),
-                                                                  job_reference);
+                                                                  new_job->getReference());
     new_job->setBatchManagerJobId(batch_manager_job_id);
   }
   catch(const Batch::GenericException &ex)
@@ -707,3 +721,84 @@ Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job
   LAUNCHER_MESSAGE("New job added");
 #endif
 }
+
+list<int>
+Launcher_cpp::loadJobs(const char* jobs_file)
+{
+  list<int> new_jobs_id_list;
+
+  // Load the jobs from XML file
+  list<Launcher::Job *> jobs_list = Launcher::XML_Persistence::loadJobs(jobs_file);
+
+  // Create each job in the launcher
+  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;
+    string job_state = new_job->getState();
+
+    try
+    {
+      if (job_state == "CREATED")
+      {
+        // In this case, we ignore run_part informations
+        createJob(new_job);
+        new_jobs_id_list.push_back(new_job->getNumber());
+      }
+      else if (job_state == "QUEUED"     ||
+               job_state == "RUNNING"    ||
+               job_state == "IN_PROCESS" ||
+               job_state == "PAUSED")
+      {
+        addJobDirectlyToMap(new_job);
+        new_jobs_id_list.push_back(new_job->getNumber());
+
+        // Step 4: We check that the BatchManager could resume
+        // the job
+#ifdef WITH_LIBBATCH
+        if (new_job->getBatchManagerJobId().getReference() != new_job->getReference())
+        {
+          LAUNCHER_INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
+          new_job->setState("ERROR");
+        }
+#endif
+      }
+      else if (job_state == "FINISHED" ||
+               job_state == "FAILED"   ||
+               job_state == "ERROR")
+      {
+        // Step 2: We add run_part informations
+        addJobDirectlyToMap(new_job);
+        new_jobs_id_list.push_back(new_job->getNumber());
+      }
+      else
+      {
+        LAUNCHER_INFOS("A bad job is found, state unknown " << job_state);
+        delete new_job;
+      }
+    }
+    catch(const LauncherException &ex)
+    {
+      LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
+      delete new_job;
+    }
+  }
+
+  return new_jobs_id_list;
+}
+
+void
+Launcher_cpp::saveJobs(const char* jobs_file)
+{
+  // Create a sorted list from the internal job map
+  list<const Launcher::Job *> jobs_list;
+  for (int i=0; i<_job_cpt; i++)
+  {
+    map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(i);
+    if (it_job != _launcher_job_map.end())
+      jobs_list.push_back(it_job->second);
+  }
+
+  // Save the jobs in XML file
+  Launcher::XML_Persistence::saveJobs(jobs_file, jobs_list);
+}