Salome HOME
Try to kill Salome application when a YACS Launcher job is killed
[modules/kernel.git] / src / Launcher / Launcher_Job.cxx
index d8538594a921fbc932512a7d827140088a6ce2b6..5728af45ee5a2a62bcca60bd9b12e4db4c18e7c7 100644 (file)
@@ -1,26 +1,31 @@
-//  Copyright (C) 2009 CEA/DEN, EDF R&D
+// Copyright (C) 2009-2011  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  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.
 //
-//  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
 //
-// Author: AndrĂ© RIBES - EDF R&D
 
+// Author: AndrĂ© RIBES - EDF R&D
+//
 #include "Launcher_Job.hxx"
 #include "Launcher.hxx"
 
+#ifdef WITH_LIBBATCH
+#include <Batch/Batch_Constants.hxx>
+#endif
+
 Launcher::Job::Job()
 {
   _number = -1;
@@ -28,6 +33,7 @@ Launcher::Job::Job()
   _launch_date = getLaunchDate();
 
   _env_file = "";
+  _job_name = "";
   _job_file = "";
   _job_file_name = "";
   _job_file_name_complete = "";
@@ -45,6 +51,7 @@ Launcher::Job::Job()
   _resource_required_params.cpu_clock = -1;
   _resource_required_params.mem_mb = -1;
   _queue = "";
+  _job_type = "";
 
 #ifdef WITH_LIBBATCH
   _batch_job = new Batch::Job();
@@ -54,6 +61,17 @@ Launcher::Job::Job()
 Launcher::Job::~Job() 
 {
   LAUNCHER_MESSAGE("Deleting job number: " << _number);
+#ifdef WITH_LIBBATCH
+  if (_batch_job)
+    delete _batch_job;
+#endif
+}
+
+void
+Launcher::Job::stopJob()
+{
+  LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
+  setState("FAILED");
 #ifdef WITH_LIBBATCH
   if (_batch_job_id.getReference() != "undefined")
   {
@@ -63,23 +81,62 @@ Launcher::Job::~Job()
     }
     catch (const Batch::EmulationException &ex)
     {
-      LAUNCHER_INFOS("WARNING: exception when deleting the job: " << ex.message);
+      LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
     }
   }
-  if (_batch_job)
-    delete _batch_job;
 #endif
 }
 
+
+void
+Launcher::Job::removeJob()
+{
+  LAUNCHER_MESSAGE("Removing job number: " << _number);
+#ifdef WITH_LIBBATCH
+  if (_batch_job_id.getReference() != "undefined")
+  {
+    try 
+    {
+      _batch_job_id.deleteJob();
+    }
+    catch (const Batch::EmulationException &ex)
+    {
+      LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
+    }
+  }
+#endif
+}
+
+std::string
+Launcher::Job::getJobType()
+{
+  return _job_type;
+}
+
+void
+Launcher::Job::setJobName(const std::string & job_name)
+{
+  _job_name = job_name;
+}
+
+std::string
+Launcher::Job::getJobName()
+{
+  return _job_name;
+}
+
 void 
 Launcher::Job::setState(const std::string & state)
 {
   // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
-  if (state != "CREATED" and
-      state != "QUEUED" and
-      state != "RUNNING" and
-      state != "FINISHED" and
-      state != "FAILED")
+  if (state != "CREATED" &&
+      state != "IN_PROCESS" &&
+      state != "QUEUED" &&
+      state != "RUNNING" &&
+      state != "PAUSED" &&
+      state != "FINISHED" &&
+      state != "FAILED" &&
+      state != "ERROR")
   {
     throw LauncherException("Bad state, this state does not exist: " + state);
   }
@@ -284,10 +341,12 @@ Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
 {
   std::string result("");
   std::string edt_value = maximum_duration;
+  std::size_t pos = edt_value.find(":");
+
   if (edt_value != "") {
-    std::string begin_edt_value = edt_value.substr(0, 2);
-    std::string mid_edt_value = edt_value.substr(2, 1);
-    std::string end_edt_value = edt_value.substr(3);
+    std::string begin_edt_value = edt_value.substr(0, pos);
+    std::string mid_edt_value = edt_value.substr(pos, 1);
+    std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
   
     long value;
     std::istringstream iss(begin_edt_value);
@@ -352,9 +411,9 @@ Launcher::Job::getLaunchDate()
   std::string launch_date = ctime(&rawtime);
   int i = 0 ;
   for (;i < launch_date.size(); i++) 
-    if (launch_date[i] == '/' or 
-        launch_date[i] == '-' or 
-        launch_date[i] == ':' or
+    if (launch_date[i] == '/' ||
+        launch_date[i] == '-' ||
+        launch_date[i] == ':' ||
         launch_date[i] == ' ') 
       launch_date[i] = '_';
   launch_date.erase(--launch_date.end()); // Last caracter is a \n
@@ -365,31 +424,22 @@ Launcher::Job::getLaunchDate()
 std::string
 Launcher::Job::updateJobState()
 {
-#ifdef WITH_LIBBATCH
-  if (_batch_job_id.getReference() != "undefined")
+
+  if (_state != "FINISHED" &&
+      _state != "ERROR"    &&
+      _state != "FAILED")
   {
-    // A batch manager has been affected to the job
-    Batch::JobInfo job_info = _batch_job_id.queryJob();
-    Batch::Parametre par = job_info.getParametre();
-
-    LAUNCHER_MESSAGE("State received is: " << par[STATE].str());
-
-    // TODO: Remove this if all tests pass with the new libBatch, otherwise fix the codes in libBatch
-    // Patch until new LIBBATCH version
-    // eSSH Client and ePBS Client and eSGE
-/*    if (par[STATE].str() == "Running" or par[STATE].str() == "E" or par[STATE].str() == "R" or par[STATE].str() == "r" or par[STATE].str() == "RUN")
-      _state = "RUNNING";
-    else if (par[STATE].str() == "Stopped")
-      _state = "PAUSED";
-    else if (par[STATE].str() == "Done" or par[STATE].str() == "U" or par[STATE].str() == "e" or par[STATE].str() == "DONE" or par[STATE].str() == "EXIT")
-      _state = "FINISHED";
-    else if (par[STATE].str() == "Dead" or par[STATE].str() == "Eqw")
-      _state = "ERROR";
-    else if (par[STATE].str() == "Q" or par[STATE].str() == "qw" or par[STATE].str() == "PEN")
-      _state = "QUEUED";*/
-    _state = par[STATE].str();
-  }
+#ifdef WITH_LIBBATCH
+    if (_batch_job_id.getReference() != "undefined")
+    {
+      // A batch manager has been affected to the job
+      Batch::JobInfo job_info = _batch_job_id.queryJob();
+      Batch::Parametre par = job_info.getParametre();
+      _state = par[Batch::STATE].str();
+      LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
+    }
 #endif
+  }
   return _state;
 }
 
@@ -406,14 +456,14 @@ Launcher::Job::common_job_params()
 {
   Batch::Parametre params;
 
-  params[USER] = _resource_definition.UserName;
-  params[NBPROC] = _resource_required_params.nb_proc;
+  params[Batch::NAME] = getJobName();
+  params[Batch::USER] = _resource_definition.UserName;
+  params[Batch::NBPROC] = _resource_required_params.nb_proc;
 
-  // Memory
+  // Memory in megabytes
   if (_resource_required_params.mem_mb > 0)
   {
-    // Memory is in kilobytes
-    params[MAXRAMSIZE] = _resource_required_params.mem_mb * 1024;
+    params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
   }
 
   // We define a default directory based on user time
@@ -433,8 +483,8 @@ Launcher::Job::common_job_params()
     _work_directory = std::string("$HOME/Batch/");
     _work_directory += thedate;
   }
-  params[WORKDIR] = _work_directory;
-  params[TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
+  params[Batch::WORKDIR] = _work_directory;
+  params[Batch::TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
 
   // If result_directory is not defined, we use HOME environnement
   if (_result_directory == "")
@@ -456,7 +506,7 @@ Launcher::Job::common_job_params()
     size_t found = file.find_last_of("/");
     std::string remote_file = _work_directory + "/" + file.substr(found+1);
 
-    params[INFILE] += Batch::Couple(local_file, remote_file);
+    params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
   }
    
   // _out_files
@@ -475,17 +525,21 @@ Launcher::Job::common_job_params()
     else
       remote_file = _work_directory + "/" + file;
 
-    params[OUTFILE] += Batch::Couple(local_file, remote_file);
+    params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
   }
 
   // Time
   if (_maximum_duration_in_second != -1)
-    params[MAXWALLTIME] = _maximum_duration_in_second;
+    params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
 
   // Queue
   if (_queue != "")
-    params[QUEUE] = _queue;
+    params[Batch::QUEUE] = _queue;
 
+  // Specific parameters
+  std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
+  if (it != _specific_parameters.end())
+    params["LL_JOBTYPE"] = it->second;
   return params;
 }
 
@@ -501,3 +555,94 @@ Launcher::Job::getBatchManagerJobId()
   return _batch_job_id;
 }
 #endif
+
+void
+Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
+{
+  // Begin job
+  xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
+  xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
+  xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
+
+  // Add user part
+  xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
+
+  xmlNewChild(node, NULL, xmlCharStrdup("job_file"),         xmlCharStrdup(getJobFile().c_str()));
+  xmlNewChild(node, NULL, xmlCharStrdup("env_file"),         xmlCharStrdup(getEnvFile().c_str()));
+  xmlNewChild(node, NULL, xmlCharStrdup("work_directory"),   xmlCharStrdup(getWorkDirectory().c_str()));
+  xmlNewChild(node, NULL, xmlCharStrdup("local_directory"),  xmlCharStrdup(getLocalDirectory().c_str()));
+  xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
+
+  // Files
+  xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
+  std::list<std::string> in_files  = get_in_files();
+  std::list<std::string> out_files = get_out_files();
+  for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
+    xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
+  for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
+    xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
+
+  // Resource part
+  resourceParams resource_params = getResourceRequiredParams();
+  xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
+  xmlNewChild(res_node, NULL, xmlCharStrdup("name"),   xmlCharStrdup(resource_params.name.c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"),   xmlCharStrdup(resource_params.hostname.c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("OS"),   xmlCharStrdup(resource_params.OS.c_str()));
+  std::ostringstream nb_proc_stream;
+  std::ostringstream nb_node_stream;
+  std::ostringstream nb_proc_per_node_stream;
+  std::ostringstream cpu_clock_stream;
+  std::ostringstream mem_mb_stream;
+  nb_proc_stream << resource_params.nb_proc;
+  nb_node_stream << resource_params.nb_node;
+  nb_proc_per_node_stream << resource_params.nb_proc_per_node;
+  cpu_clock_stream << resource_params.cpu_clock;
+  mem_mb_stream << resource_params.mem_mb;
+  xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"),            xmlCharStrdup(nb_proc_stream.str().c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"),            xmlCharStrdup(nb_node_stream.str().c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"),   xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"),          xmlCharStrdup(cpu_clock_stream.str().c_str()));
+  xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"),             xmlCharStrdup(mem_mb_stream.str().c_str()));
+
+  xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
+  xmlNewChild(node, NULL, xmlCharStrdup("queue"),            xmlCharStrdup(getQueue().c_str()));
+
+  // Specific parameters part
+  xmlNodePtr specific_parameters_node = xmlNewChild(node, NULL, xmlCharStrdup("specific_parameters"), NULL);
+  std::map<std::string, std::string> specific_parameters = getSpecificParameters();
+  for(std::map<std::string, std::string>::iterator it = specific_parameters.begin(); it != specific_parameters.end(); it++)
+  {
+    xmlNodePtr specific_parameter_node = xmlNewChild(specific_parameters_node, NULL, xmlCharStrdup("specific_parameter"), NULL);
+    xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("name"), xmlCharStrdup((it->first).c_str()));
+    xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("value"), xmlCharStrdup((it->second).c_str()));
+  }
+
+  // Run part
+  xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
+  xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
+  ParserResourcesType resource_definition = getResourceDefinition();
+  xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
+
+#ifdef WITH_LIBBATCH
+  Batch::JobId job_id = getBatchManagerJobId();
+  xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));
+#endif
+}
+
+void 
+Launcher::Job::addSpecificParameter(const std::string & name,
+                                      const std::string & value)
+{
+  _specific_parameters[name] = value;
+}
+
+const std::map<std::string, std::string> &
+Launcher::Job::getSpecificParameters()
+{
+  return _specific_parameters;
+}
+
+void
+Launcher::Job::checkSpecificParameters()
+{
+}