From: barate Date: Thu, 10 Oct 2013 15:58:04 +0000 (+0000) Subject: Move XML persistence functions from SALOME_Launcher class to a dedicated class Launch... X-Git-Tag: BR_hydro_v_0_3_1~88 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=53dbc52658003957cb4483086acac6af3906088a;p=modules%2Fkernel.git Move XML persistence functions from SALOME_Launcher class to a dedicated class Launcher::XML_Persistence --- diff --git a/src/Launcher/CMakeLists.txt b/src/Launcher/CMakeLists.txt index d458e8446..bf351e5d8 100755 --- a/src/Launcher/CMakeLists.txt +++ b/src/Launcher/CMakeLists.txt @@ -69,6 +69,7 @@ SET(Launcher_SOURCES Launcher_Job_YACSFile.cxx Launcher_Job_Writer.cxx Launcher.cxx + Launcher_XML_Persistence.cxx ) ADD_LIBRARY(Launcher ${Launcher_SOURCES}) diff --git a/src/Launcher/Launcher.cxx b/src/Launcher/Launcher.cxx index 9ec41817f..0d905d8d7 100644 --- a/src/Launcher/Launcher.cxx +++ b/src/Launcher/Launcher.cxx @@ -20,6 +20,12 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // +#include +#include +#include +#include +#include + #ifdef WITH_LIBBATCH #include #include @@ -31,10 +37,9 @@ #include "SALOME_Launcher_Handler.hxx" #include "Launcher.hxx" #include "Launcher_Job_Command.hxx" -#include -#include -#include -#include +#include "Launcher_XML_Persistence.hxx" + +using namespace std; //============================================================================= /*! @@ -676,7 +681,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); @@ -693,7 +698,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) @@ -715,3 +720,84 @@ Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job, const std::string job LAUNCHER_MESSAGE("New job added"); #endif } + +list +Launcher_cpp::loadJobs(const char* jobs_file) +{ + list new_jobs_id_list; + + // Load the jobs from XML file + list jobs_list = Launcher::XML_Persistence::loadJobs(jobs_file); + + // Create each job in the launcher + list::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 jobs_list; + for (int i=0; i<_job_cpt; i++) + { + map::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); +} diff --git a/src/Launcher/Launcher.hxx b/src/Launcher/Launcher.hxx index 228355c30..11cfab5fb 100644 --- a/src/Launcher/Launcher.hxx +++ b/src/Launcher/Launcher.hxx @@ -33,6 +33,7 @@ #include #include +#include #include @@ -71,11 +72,19 @@ public: void stopJob(int job_id); void removeJob(int job_id); + /*! Load the jobs from the file "jobs_file" and add them to the Launcher. + * Return a list with the IDs of the jobs that were successfully loaded. + */ + std::list loadJobs(const char* jobs_file); + + //! Save the jobs of the Launcher to the file "jobs_file". + void saveJobs(const char* jobs_file); + // Useful methods long createJobWithFile(std::string xmlExecuteFile, std::string clusterName); std::map getJobs(); void createBatchManagerForJob(Launcher::Job * job); - void addJobDirectlyToMap(Launcher::Job * new_job, const std::string job_reference); + void addJobDirectlyToMap(Launcher::Job * new_job); // Lib methods void SetResourcesManager( ResourcesManager_cpp* rm ) {_ResManager = rm;} diff --git a/src/Launcher/Launcher_Job.cxx b/src/Launcher/Launcher_Job.cxx index 9dd61df11..a930e4f7e 100644 --- a/src/Launcher/Launcher_Job.cxx +++ b/src/Launcher/Launcher_Job.cxx @@ -116,7 +116,7 @@ Launcher::Job::removeJob() } std::string -Launcher::Job::getJobType() +Launcher::Job::getJobType() const { return _job_type; } @@ -128,7 +128,7 @@ Launcher::Job::setJobName(const std::string & job_name) } std::string -Launcher::Job::getJobName() +Launcher::Job::getJobName() const { return _job_name; } @@ -152,7 +152,7 @@ Launcher::Job::setState(const std::string & state) } std::string -Launcher::Job::getState() +Launcher::Job::getState() const { return _state; } @@ -200,7 +200,7 @@ Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_defini } ParserResourcesType -Launcher::Job::getResourceDefinition() +Launcher::Job::getResourceDefinition() const { return _resource_definition; } @@ -223,7 +223,7 @@ Launcher::Job::setJobFile(const std::string & job_file) } std::string -Launcher::Job::getJobFile() +Launcher::Job::getJobFile() const { return _job_file; } @@ -234,7 +234,7 @@ Launcher::Job::setEnvFile(const std::string & env_file) } std::string -Launcher::Job::getEnvFile() +Launcher::Job::getEnvFile() const { return _env_file; } @@ -333,68 +333,74 @@ Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu) _mem_per_cpu = mem_per_cpu; } +void +Launcher::Job::setReference(const std::string & reference) +{ + _reference = reference; +} + std::string -Launcher::Job::getWorkDirectory() +Launcher::Job::getWorkDirectory() const { return _work_directory; } std::string -Launcher::Job::getLocalDirectory() +Launcher::Job::getLocalDirectory() const { return _local_directory; } std::string -Launcher::Job::getResultDirectory() +Launcher::Job::getResultDirectory() const { return _result_directory; } const std::list & -Launcher::Job::get_in_files() +Launcher::Job::get_in_files() const { return _in_files; } const std::list & -Launcher::Job::get_out_files() +Launcher::Job::get_out_files() const { return _out_files; } std::string -Launcher::Job::getMaximumDuration() +Launcher::Job::getMaximumDuration() const { return _maximum_duration; } // For COORM std::string -Launcher::Job::getLauncherFile() +Launcher::Job::getLauncherFile() const { return _launcher_file; } std::string -Launcher::Job::getLauncherArgs() +Launcher::Job::getLauncherArgs() const { return _launcher_args; } resourceParams -Launcher::Job::getResourceRequiredParams() +Launcher::Job::getResourceRequiredParams() const { return _resource_required_params; } std::string -Launcher::Job::getQueue() +Launcher::Job::getQueue() const { return _queue; } bool -Launcher::Job::getExclusive() +Launcher::Job::getExclusive() const { return _exclusive; } @@ -411,6 +417,12 @@ Launcher::Job::getMemPerCpu() const return _mem_per_cpu; } +std::string +Launcher::Job::getReference() const +{ + return _reference; +} + void Launcher::Job::checkMaximumDuration(const std::string & maximum_duration) { @@ -482,7 +494,7 @@ Launcher::Job::convertMaximumDuration(const std::string & edt) } std::string -Launcher::Job::getLaunchDate() +Launcher::Job::getLaunchDate() const { time_t rawtime; time(&rawtime); @@ -642,7 +654,7 @@ Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id) } Batch::JobId -Launcher::Job::getBatchManagerJobId() +Launcher::Job::getBatchManagerJobId() const { return _batch_job_id; } @@ -656,7 +668,7 @@ Launcher::Job::addSpecificParameter(const std::string & name, } const std::map & -Launcher::Job::getSpecificParameters() +Launcher::Job::getSpecificParameters() const { return _specific_parameters; } diff --git a/src/Launcher/Launcher_Job.hxx b/src/Launcher/Launcher_Job.hxx index 58f2cf739..e2674c6e5 100644 --- a/src/Launcher/Launcher_Job.hxx +++ b/src/Launcher/Launcher_Job.hxx @@ -54,7 +54,7 @@ namespace Launcher // Launcher managing parameters // State of a Job: CREATED, IN_PROCESS, QUEUED, RUNNING, PAUSED, FINISHED, ERROR void setState(const std::string & state); - std::string getState(); + std::string getState() const; // Get names or ids of hosts assigned to the job std::string getAssignedHostnames(); @@ -62,7 +62,7 @@ namespace Launcher int getNumber(); virtual void setResourceDefinition(const ParserResourcesType & resource_definition); - ParserResourcesType getResourceDefinition(); + ParserResourcesType getResourceDefinition() const; // Common parameters void setJobName(const std::string & job_name); @@ -79,35 +79,37 @@ namespace Launcher void setExclusive(bool exclusive); void setExclusiveStr(const std::string & exclusiveStr); void setMemPerCpu(unsigned long mem_per_cpu); + void setReference(const std::string & reference); // For COORM void setLauncherFile(const std::string & launcher_file); void setLauncherArgs(const std::string & launcher_args); - std::string getJobName(); - std::string getJobFile(); - std::string getWorkDirectory(); - std::string getLocalDirectory(); - std::string getResultDirectory(); - const std::list & get_in_files(); - const std::list & get_out_files(); - std::string getMaximumDuration(); - resourceParams getResourceRequiredParams(); - std::string getQueue(); - std::string getEnvFile(); - std::string getJobType(); - bool getExclusive(); + std::string getJobName() const; + std::string getJobFile() const; + std::string getWorkDirectory() const; + std::string getLocalDirectory() const; + std::string getResultDirectory() const; + const std::list & get_in_files() const; + const std::list & get_out_files() const; + std::string getMaximumDuration() const; + resourceParams getResourceRequiredParams() const; + std::string getQueue() const; + std::string getEnvFile() const; + std::string getJobType() const; + bool getExclusive() const; std::string getExclusiveStr() const; unsigned long getMemPerCpu() const; + std::string getReference() const; // For COORM - std::string getLauncherFile(); - std::string getLauncherArgs(); + std::string getLauncherFile() const; + std::string getLauncherArgs() const; std::string updateJobState(); void addSpecificParameter(const std::string & name, const std::string & value); - const std::map & getSpecificParameters(); + const std::map & getSpecificParameters() const; virtual void checkSpecificParameters(); // Checks @@ -116,7 +118,7 @@ namespace Launcher // Helps long convertMaximumDuration(const std::string & maximum_duration); - std::string getLaunchDate(); + std::string getLaunchDate() const; void stopJob(); void removeJob(); @@ -153,6 +155,7 @@ namespace Launcher std::string _queue; bool _exclusive; unsigned long _mem_per_cpu; + std::string _reference; // Parameters for COORM std::string _launcher_file; @@ -164,7 +167,7 @@ namespace Launcher Batch::Job * getBatchJob(); Batch::Parametre common_job_params(); void setBatchManagerJobId(Batch::JobId batch_manager_job_id); - Batch::JobId getBatchManagerJobId(); + Batch::JobId getBatchManagerJobId() const; protected: Batch::Job * _batch_job; @@ -174,4 +177,3 @@ namespace Launcher } #endif - diff --git a/src/Launcher/Launcher_Job_Writer.cxx b/src/Launcher/Launcher_Job_Writer.cxx index 449fa02f7..482fefd06 100644 --- a/src/Launcher/Launcher_Job_Writer.cxx +++ b/src/Launcher/Launcher_Job_Writer.cxx @@ -23,7 +23,7 @@ #include "Launcher_Job_Writer.hxx" #include "Launcher_Job.hxx" -void Launcher::addToXmlDocument(xmlNodePtr root_node, Launcher::Job* job) +void Launcher::addToXmlDocument(xmlNodePtr root_node, const Launcher::Job* job) { // Begin job xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL); diff --git a/src/Launcher/Launcher_Job_Writer.hxx b/src/Launcher/Launcher_Job_Writer.hxx index 814c41c17..fa9f21711 100644 --- a/src/Launcher/Launcher_Job_Writer.hxx +++ b/src/Launcher/Launcher_Job_Writer.hxx @@ -25,5 +25,5 @@ namespace Launcher { class Job; - void addToXmlDocument(xmlNodePtr root_node, Job* job); + void addToXmlDocument(xmlNodePtr root_node, const Job* job); } diff --git a/src/Launcher/Launcher_XML_Persistence.cxx b/src/Launcher/Launcher_XML_Persistence.cxx new file mode 100644 index 000000000..b60b14fd5 --- /dev/null +++ b/src/Launcher/Launcher_XML_Persistence.cxx @@ -0,0 +1,540 @@ +// Copyright (C) 2007-2013 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 +// +// 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. +// +// 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 +// + +#include + +#include "Launcher_XML_Persistence.hxx" +#include "Launcher_Job_Command.hxx" +#include "Launcher_Job_YACSFile.hxx" +#include "Launcher_Job_PythonSALOME.hxx" +#include "Launcher_Job_Writer.hxx" + +using namespace std; + +namespace Launcher +{ + +list +XML_Persistence::loadJobs(const char* jobs_file) +{ + // Step 1: check jobs_file read access + FILE* xml_file = fopen(jobs_file, "r"); + if (xml_file == NULL) + { + std::string error = "Error opening jobs_file in SALOME_Launcher::loadJobs: " + std::string(jobs_file); + LAUNCHER_INFOS(error); + throw LauncherException(error); + } + + // Step 2: read xml file + xmlDocPtr doc = xmlReadFile(jobs_file, NULL, 0); + if (doc == NULL) + { + std::string error = "Error in xmlReadFile in SALOME_Launcher::loadJobs, could not parse file: " + std::string(jobs_file); + LAUNCHER_INFOS(error); + fclose(xml_file); + throw LauncherException(error); + } + + // Step 3: Find jobs + list jobs_list; + xmlNodePtr root_node = xmlDocGetRootElement(doc); + xmlNodePtr xmlCurrentNode = root_node->xmlChildrenNode; + if (!xmlStrcmp(root_node->name, xmlCharStrdup("jobs"))) + { + while(xmlCurrentNode != NULL) + { + if (!xmlStrcmp(xmlCurrentNode->name, xmlCharStrdup("job"))) + { + LAUNCHER_INFOS("A job is found"); + Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it + xmlNodePtr job_node = xmlCurrentNode; + + // Begin Job + if (!xmlHasProp(job_node, xmlCharStrdup("type")) || + !xmlHasProp(job_node, xmlCharStrdup("name"))) + { + LAUNCHER_INFOS("A bad job is found, type or name not found"); + break; + } + xmlChar* type = xmlGetProp(job_node, xmlCharStrdup("type")); + xmlChar* name = xmlGetProp(job_node, xmlCharStrdup("name")); + std::string job_type((const char*) type); + if (job_type == "command") + new_job = new Launcher::Job_Command(); + else if (job_type == "yacs_file") + new_job = new Launcher::Job_YACSFile(); + else if (job_type == "python_salome") + new_job = new Launcher::Job_PythonSALOME(); + new_job->setJobName(std::string((const char *)name)); + xmlFree(type); + xmlFree(name); + + xmlNodePtr user_node = xmlFirstElementChild(job_node); + xmlNodePtr run_node = xmlNextElementSibling(user_node); + if (user_node == NULL || run_node == NULL) + { + LAUNCHER_INFOS("A bad job is found, user_part or run_part not found"); + delete new_job; + break; + } + if (xmlStrcmp(user_node->name, xmlCharStrdup("user_part")) || + xmlStrcmp(run_node->name, xmlCharStrdup("run_part"))) + { + LAUNCHER_INFOS("A bad job is found, cannot get a correct user_part or run_part node"); + delete new_job; + break; + } + + // Add user part + + // Get job_file env_file work_directory local_directory result_directory + xmlNodePtr job_file_node = xmlFirstElementChild(user_node); + xmlNodePtr env_file_node = xmlNextElementSibling(job_file_node); + xmlNodePtr work_directory_node = xmlNextElementSibling(env_file_node); + xmlNodePtr local_directory_node = xmlNextElementSibling(work_directory_node); + xmlNodePtr result_directory_node = xmlNextElementSibling(local_directory_node); + + // Parameters for COORM + xmlNodePtr launcher_file_node = xmlNextElementSibling(result_directory_node); + + if (job_file_node == NULL || + env_file_node == NULL || + work_directory_node == NULL || + local_directory_node == NULL || + result_directory_node == NULL || + // For COORM + launcher_file_node == NULL + ) + { + LAUNCHER_INFOS("A bad job is found, some user_part are not found"); + delete new_job; + break; + } + if (xmlStrcmp(job_file_node->name, xmlCharStrdup("job_file")) || + xmlStrcmp(env_file_node->name, xmlCharStrdup("env_file")) || + xmlStrcmp(work_directory_node->name, xmlCharStrdup("work_directory")) || + xmlStrcmp(local_directory_node->name, xmlCharStrdup("local_directory")) || + xmlStrcmp(result_directory_node->name, xmlCharStrdup("result_directory")) || + // For COORM + xmlStrcmp(launcher_file_node->name, xmlCharStrdup("launcher_file")) + ) + { + LAUNCHER_INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name"); + delete new_job; + break; + } + xmlChar* job_file = xmlNodeGetContent(job_file_node); + try + { + new_job->setJobFile(std::string((const char *)job_file)); + } + catch(const LauncherException &ex) + { + LAUNCHER_INFOS("Exception receice for job_file, cannot add the job" << ex.msg.c_str()); + delete new_job; + xmlFree(job_file); + break; + } + xmlChar* env_file = xmlNodeGetContent(env_file_node); + xmlChar* work_directory = xmlNodeGetContent(work_directory_node); + xmlChar* local_directory = xmlNodeGetContent(local_directory_node); + xmlChar* result_directory = xmlNodeGetContent(result_directory_node); + + // Parameters for COORM + xmlChar* launcher_file = xmlNodeGetContent(launcher_file_node); + + new_job->setEnvFile(std::string((const char *)env_file)); + new_job->setWorkDirectory(std::string((const char *)work_directory)); + new_job->setLocalDirectory(std::string((const char *)local_directory)); + new_job->setResultDirectory(std::string((const char *)result_directory)); + + // Parameters for COORM + new_job->setLauncherFile(std::string((const char *)launcher_file)); + + xmlFree(job_file); + xmlFree(env_file); + xmlFree(work_directory); + xmlFree(local_directory); + xmlFree(result_directory); + + // Parameters for COORM + xmlFree(launcher_file); + + // Get in and out files + xmlNodePtr files_node = xmlNextElementSibling(launcher_file_node); + if (files_node == NULL) + { + LAUNCHER_INFOS("A bad job is found, user_part files is not found"); + delete new_job; + break; + } + if (xmlStrcmp(files_node->name, xmlCharStrdup("files"))) + { + LAUNCHER_INFOS("A bad job is found, files node are not in the rigth place or does not have a correct name or does not exist"); + delete new_job; + break; + } + xmlNodePtr file_node = xmlFirstElementChild(files_node); + while (file_node != NULL) + { + if (!xmlStrcmp(file_node->name, xmlCharStrdup("in_file"))) + { + xmlChar* in_file = xmlNodeGetContent(file_node); + new_job->add_in_file(std::string((const char *)in_file)); + xmlFree(in_file); + } + else if (!xmlStrcmp(file_node->name, xmlCharStrdup("out_file"))) + { + xmlChar* out_file = xmlNodeGetContent(file_node); + new_job->add_out_file(std::string((const char *)out_file)); + xmlFree(out_file); + } + file_node = xmlNextElementSibling(file_node); + } + + // Get resource part + xmlNodePtr res_node = xmlNextElementSibling(files_node); + xmlNodePtr maximum_duration_node = xmlNextElementSibling(res_node); + xmlNodePtr queue_node = xmlNextElementSibling(maximum_duration_node); + xmlNodePtr exclusive_node = xmlNextElementSibling(queue_node); + xmlNodePtr mem_per_cpu_node = xmlNextElementSibling(exclusive_node); + xmlNodePtr launcher_args_node = xmlNextElementSibling(mem_per_cpu_node); + if (res_node == NULL || + maximum_duration_node == NULL || + queue_node == NULL || + exclusive_node == NULL || + mem_per_cpu_node == NULL || + // For COORM + launcher_args_node == NULL + ) + { + LAUNCHER_INFOS("A bad job is found, some user_part are not found"); + delete new_job; + break; + } + if (xmlStrcmp(res_node->name, xmlCharStrdup("resource_params")) || + xmlStrcmp(maximum_duration_node->name, xmlCharStrdup("maximum_duration")) || + xmlStrcmp(queue_node->name, xmlCharStrdup("queue")) || + xmlStrcmp(exclusive_node->name, xmlCharStrdup("exclusive")) || + xmlStrcmp(mem_per_cpu_node->name, xmlCharStrdup("mem_per_cpu")) || + // For COORM + xmlStrcmp(launcher_args_node->name, xmlCharStrdup("launcher_args")) + ) + { + LAUNCHER_INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name"); + delete new_job; + break; + } + xmlChar* maximum_duration = xmlNodeGetContent(maximum_duration_node); + try + { + new_job->setMaximumDuration(std::string((const char *)maximum_duration)); + } + catch(const LauncherException &ex) + { + LAUNCHER_INFOS("Exception receice for maximum_duration, cannot add the job" << ex.msg.c_str()); + delete new_job; + xmlFree(maximum_duration); + break; + } + xmlChar* queue = xmlNodeGetContent(queue_node); + new_job->setQueue(std::string((const char *)queue)); + xmlFree(maximum_duration); + xmlFree(queue); + + xmlChar* exclusive = xmlNodeGetContent(exclusive_node); + try + { + new_job->setExclusiveStr(std::string((const char *)exclusive)); + } + catch(const LauncherException &ex) + { + LAUNCHER_INFOS("Exception received for exclusive, cannot add the job. " << ex.msg.c_str()); + delete new_job; + xmlFree(exclusive); + break; + } + xmlFree(exclusive); + + xmlChar* mem_per_cpu_str = xmlNodeGetContent(mem_per_cpu_node); + std::istringstream mem_per_cpu_stream((const char *)mem_per_cpu_str); + unsigned long mem_per_cpu = 0; + if (!(mem_per_cpu_stream >> mem_per_cpu)) + { + LAUNCHER_INFOS("A bad job is found, mem_per_cpu parameter is not correct"); + delete new_job; + break; + } + else + new_job->setMemPerCpu(mem_per_cpu); + + // For COORM + xmlChar* launcher_args = xmlNodeGetContent(launcher_args_node); + new_job->setLauncherArgs(std::string((const char *)launcher_args)); + xmlFree(launcher_args); + + xmlNodePtr specific_node = xmlNextElementSibling(launcher_args_node); + if (specific_node == NULL) + { + LAUNCHER_INFOS("A bad job is found, specific_parameters part is not found"); + delete new_job; + break; + } + xmlNodePtr parameter_node = xmlFirstElementChild(specific_node); + while (parameter_node != NULL) + { + if (!xmlStrcmp(parameter_node->name, xmlCharStrdup("specific_parameter"))) + { + xmlNodePtr name_node = xmlFirstElementChild(parameter_node); + xmlNodePtr value_node = xmlNextElementSibling(name_node); + if (name_node == NULL || + value_node == NULL) + { + LAUNCHER_INFOS("A bad job is found, specific_parameter parts are not found"); + delete new_job; + break; + } + if (xmlStrcmp(name_node->name, xmlCharStrdup("name")) || + xmlStrcmp(value_node->name, xmlCharStrdup("value"))) + { + LAUNCHER_INFOS("A bad job is found, specific_parameter bad parts are found"); + delete new_job; + break; + } + + xmlChar* name = xmlNodeGetContent(name_node); + xmlChar* value = xmlNodeGetContent(value_node); + try + { + new_job->addSpecificParameter(std::string((const char*)name), std::string((const char*)value)); + xmlFree(name); + xmlFree(value); + } + catch(const LauncherException &ex) + { + LAUNCHER_INFOS("Exception receice for a specific parameter, cannot add the job" << ex.msg.c_str()); + delete new_job; + xmlFree(name); + xmlFree(value); + break; + } + } + else + { + LAUNCHER_INFOS("A bad job is found, specific_parameters part is bad, a node that is not a specific parameter is found"); + delete new_job; + break; + } + parameter_node = xmlNextElementSibling(parameter_node); + } + + xmlNodePtr res_name_node = xmlFirstElementChild(res_node); + xmlNodePtr res_hostname_node = xmlNextElementSibling(res_name_node); + xmlNodePtr res_os_node = xmlNextElementSibling(res_hostname_node); + xmlNodePtr res_nb_proc_node = xmlNextElementSibling(res_os_node); + xmlNodePtr res_nb_node_node = xmlNextElementSibling(res_nb_proc_node); + xmlNodePtr res_nb_proc_per_node_node = xmlNextElementSibling(res_nb_node_node); + xmlNodePtr res_cpu_clock_node = xmlNextElementSibling(res_nb_proc_per_node_node); + xmlNodePtr res_mem_mb_node = xmlNextElementSibling(res_cpu_clock_node); + if (res_name_node == NULL || + res_hostname_node == NULL || + res_os_node == NULL || + res_nb_proc_node == NULL || + res_nb_node_node == NULL || + res_nb_proc_per_node_node == NULL || + res_cpu_clock_node == NULL || + res_mem_mb_node == NULL + ) + { + LAUNCHER_INFOS("A bad job is found, some resource_params user_part are not found"); + delete new_job; + break; + } + if (xmlStrcmp(res_name_node->name, xmlCharStrdup("name")) || + xmlStrcmp(res_hostname_node->name, xmlCharStrdup("hostname")) || + xmlStrcmp(res_os_node->name, xmlCharStrdup("OS")) || + xmlStrcmp(res_nb_proc_node->name, xmlCharStrdup("nb_proc")) || + xmlStrcmp(res_nb_node_node->name, xmlCharStrdup("nb_node")) || + xmlStrcmp(res_nb_proc_per_node_node->name, xmlCharStrdup("nb_proc_per_node")) || + xmlStrcmp(res_cpu_clock_node->name, xmlCharStrdup("cpu_clock")) || + xmlStrcmp(res_mem_mb_node->name, xmlCharStrdup("mem_mb")) + ) + { + LAUNCHER_INFOS("A bad job is found, some resource_params user_part node are not in the rigth or does not have a correct name"); + delete new_job; + break; + } + xmlChar* res_name = xmlNodeGetContent(res_name_node); + xmlChar* res_hostname = xmlNodeGetContent(res_hostname_node); + xmlChar* res_os = xmlNodeGetContent(res_os_node); + resourceParams p; + p.name = std::string((const char*) res_name); + p.hostname = std::string((const char*) res_hostname); + p.OS = std::string((const char*) res_os); + xmlFree(res_name); + xmlFree(res_hostname); + xmlFree(res_os); + xmlChar* res_nb_proc = xmlNodeGetContent(res_nb_proc_node); + xmlChar* res_nb_node = xmlNodeGetContent(res_nb_node_node); + xmlChar* res_nb_proc_per_node = xmlNodeGetContent(res_nb_proc_per_node_node); + xmlChar* res_cpu_clock = xmlNodeGetContent(res_cpu_clock_node); + xmlChar* res_mem_mb = xmlNodeGetContent(res_mem_mb_node); + bool import_value = true; + std::istringstream nb_proc_stream((const char *) res_nb_proc); + if (!(nb_proc_stream >> p.nb_proc)) + import_value = false; + std::istringstream nb_node_stream((const char *) res_nb_node); + if (!(nb_node_stream >> p.nb_node)) + import_value = false; + std::istringstream nb_proc_per_node_stream((const char *) res_nb_proc_per_node); + if (!(nb_proc_per_node_stream >> p.nb_proc_per_node)) + import_value = false; + std::istringstream cpu_clock_stream((const char *) res_cpu_clock); + if (!(cpu_clock_stream >> p.cpu_clock)) + import_value = false; + std::istringstream mem_mb_stream((const char *) res_mem_mb); + if (!(mem_mb_stream >> p.mem_mb)) + import_value = false; + xmlFree(res_nb_proc); + xmlFree(res_nb_node); + xmlFree(res_nb_proc_per_node); + xmlFree(res_cpu_clock); + xmlFree(res_mem_mb); + if (!import_value) + { + LAUNCHER_INFOS("A bad job is found, some resource_params value are not correct"); + delete new_job; + break; + } + try + { + new_job->setResourceRequiredParams(p); + } + catch(const LauncherException &ex) + { + LAUNCHER_INFOS("A bad job is found, an error when inserting resource_params:" << ex.msg.c_str()); + delete new_job; + break; + } + + // We finally get run part to figure out what to do + xmlNodePtr job_state_node = xmlFirstElementChild(run_node); + xmlNodePtr resource_choosed_name_node = xmlNextElementSibling(job_state_node); + xmlNodePtr job_reference_node = xmlNextElementSibling(resource_choosed_name_node); + if (job_state_node == NULL || + resource_choosed_name_node == NULL || + job_reference_node == NULL + ) + { + LAUNCHER_INFOS("A bad job is found, some run_part are not found"); + delete new_job; + break; + } + if (xmlStrcmp(job_state_node->name, xmlCharStrdup("job_state")) || + xmlStrcmp(resource_choosed_name_node->name, xmlCharStrdup("resource_choosed_name")) || + xmlStrcmp(job_reference_node->name, xmlCharStrdup("job_reference")) + ) + { + LAUNCHER_INFOS("A bad job is found, some run_part nodes are not in the rigth or does not have a correct name"); + delete new_job; + break; + } + xmlChar* job_state_xml = xmlNodeGetContent(job_state_node); + xmlChar* resource_choosed_name_xml = xmlNodeGetContent(resource_choosed_name_node); + xmlChar* job_reference_xml = xmlNodeGetContent(job_reference_node); + std::string job_state((const char *) job_state_xml); + std::string resource_choosed_name((const char *) resource_choosed_name_xml); + std::string job_reference((const char *) job_reference_xml); + xmlFree(job_state_xml); + xmlFree(resource_choosed_name_xml); + xmlFree(job_reference_xml); + new_job->setState(job_state); + new_job->setReference(job_reference); + + jobs_list.push_back(new_job); + } + xmlCurrentNode = xmlCurrentNode->next; + } + } + else + { + xmlFreeDoc(doc); + fclose(xml_file); + std::string error = "Error in xml file, could not find root_node named jobs: " + std::string(jobs_file); + LAUNCHER_INFOS(error); + throw LauncherException(error); + } + + // Clean + xmlFreeDoc(doc); + fclose(xml_file); + + return jobs_list; +} + +void +XML_Persistence::saveJobs(const char* jobs_file, const list & jobs_list) +{ + // Step 1: check jobs_file write access + FILE* xml_file = fopen(jobs_file, "w"); + if (xml_file == NULL) + { + std::string error = "Error opening jobs_file in SALOME_Launcher::saveJobs: " + std::string(jobs_file); + LAUNCHER_INFOS(error); + throw LauncherException(error); + } + + // Step 2: First lines + xmlKeepBlanksDefault(0); + xmlDocPtr doc = xmlNewDoc(xmlCharStrdup("1.0")); + xmlNodePtr root_node = xmlNewNode(NULL, xmlCharStrdup("jobs")); + xmlDocSetRootElement(doc, root_node); + xmlNodePtr doc_comment = xmlNewDocComment(doc, xmlCharStrdup("SALOME Launcher save jobs file")); + xmlAddPrevSibling(root_node, doc_comment); + + // Step 3: For each job write it on the xml document + // We could put a mutex but are not foing to do that currently + list::const_iterator it_job; + for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++) + { + addToXmlDocument(root_node, *it_job); + } + + // Final step: write file + int isOk = xmlSaveFormatFile(jobs_file, doc, 1); + if (!isOk) + { + std::string error = "Error during xml file saving in SALOME_Launcher::saveJobs: " + std::string(jobs_file); + LAUNCHER_INFOS(error); + xmlFreeDoc(doc); + fclose(xml_file); + throw LauncherException(error); + } + + // Clean + xmlFreeDoc(doc); + fclose(xml_file); + LAUNCHER_MESSAGE("SALOME_Launcher::saveJobs : WRITING DONE!"); +} + +} diff --git a/src/Launcher/Launcher_XML_Persistence.hxx b/src/Launcher/Launcher_XML_Persistence.hxx new file mode 100644 index 000000000..772b1e9d3 --- /dev/null +++ b/src/Launcher/Launcher_XML_Persistence.hxx @@ -0,0 +1,55 @@ +// Copyright (C) 2007-2013 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 +// +// 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. +// +// 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 +// + +#ifndef __LAUNCHER_XML_PERSISTENCE_HXX__ +#define __LAUNCHER_XML_PERSISTENCE_HXX__ + +#include + +#include "Launcher_Utils.hxx" +#include "Launcher_Job.hxx" + +namespace Launcher +{ + class LAUNCHER_EXPORT XML_Persistence + { + public: + virtual ~XML_Persistence() {} + + /*! Load the jobs from the XML file "jobs_file". + * Return a list with the jobs that were successfully loaded. + * The ownership of the created jobs is transferred to the caller. + */ + static std::list loadJobs(const char* jobs_file); + + //! Save the jobs in the list "jobs_list" to the XML file "jobs_file". + static void saveJobs(const char* jobs_file, const std::list & jobs_list); + + private: + // This class is static only, not instanciable + XML_Persistence() {} + + }; + +} + +#endif diff --git a/src/Launcher/SALOME_Launcher.cxx b/src/Launcher/SALOME_Launcher.cxx index 45d5c946b..f823275b2 100644 --- a/src/Launcher/SALOME_Launcher.cxx +++ b/src/Launcher/SALOME_Launcher.cxx @@ -39,11 +39,13 @@ #endif #include #include +#include -#include #include #include +using namespace std; + const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher"; //============================================================================= @@ -539,523 +541,17 @@ SALOME_Launcher::getJobParameters(CORBA::Long job_id) void SALOME_Launcher::loadJobs(const char* jobs_file) { - // Step 1: check jobs_file read access - FILE* xml_file = fopen(jobs_file, "r"); - if (xml_file == NULL) - { - std::string error = "Error opening jobs_file in SALOME_Launcher::loadJobs: " + std::string(jobs_file); - INFOS(error); - THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR); - } + // Load the jobs in Launcher + list new_jobs_id_list = _l.loadJobs(jobs_file); - // Step 2: read xml file - xmlDocPtr doc = xmlReadFile(jobs_file, NULL, 0); - if (doc == NULL) + // Notify observers of the new jobs + list::const_iterator it_jobs_id; + for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++) { - std::string error = "Error in xmlReadFile in SALOME_Launcher::loadJobs, could not parse file: " + std::string(jobs_file); - INFOS(error); - fclose(xml_file); - THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR); + ostringstream job_id_sstr; + job_id_sstr << *it_jobs_id; + notifyObservers("NEW_JOB", job_id_sstr.str()); } - - // Step 3: Find jobs - xmlNodePtr root_node = xmlDocGetRootElement(doc); - xmlNodePtr xmlCurrentNode = root_node->xmlChildrenNode; - if (!xmlStrcmp(root_node->name, xmlCharStrdup("jobs"))) - { - while(xmlCurrentNode != NULL) - { - if (!xmlStrcmp(xmlCurrentNode->name, xmlCharStrdup("job"))) - { - INFOS("A job is found"); - Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it - xmlNodePtr job_node = xmlCurrentNode; - - // Begin Job - if (!xmlHasProp(job_node, xmlCharStrdup("type")) || - !xmlHasProp(job_node, xmlCharStrdup("name"))) - { - INFOS("A bad job is found, type or name not found"); - break; - } - xmlChar* type = xmlGetProp(job_node, xmlCharStrdup("type")); - xmlChar* name = xmlGetProp(job_node, xmlCharStrdup("name")); - std::string job_type((const char*) type); - if (job_type == "command") - new_job = new Launcher::Job_Command(); - else if (job_type == "yacs_file") - new_job = new Launcher::Job_YACSFile(); - else if (job_type == "python_salome") - new_job = new Launcher::Job_PythonSALOME(); - new_job->setJobName(std::string((const char *)name)); - xmlFree(type); - xmlFree(name); - - xmlNodePtr user_node = xmlFirstElementChild(job_node); - xmlNodePtr run_node = xmlNextElementSibling(user_node); - if (user_node == NULL || run_node == NULL) - { - INFOS("A bad job is found, user_part or run_part not found"); - delete new_job; - break; - } - if (xmlStrcmp(user_node->name, xmlCharStrdup("user_part")) || - xmlStrcmp(run_node->name, xmlCharStrdup("run_part"))) - { - INFOS("A bad job is found, cannot get a correct user_part or run_part node"); - delete new_job; - break; - } - - // Add user part - - // Get job_file env_file work_directory local_directory result_directory - xmlNodePtr job_file_node = xmlFirstElementChild(user_node); - xmlNodePtr env_file_node = xmlNextElementSibling(job_file_node); - xmlNodePtr work_directory_node = xmlNextElementSibling(env_file_node); - xmlNodePtr local_directory_node = xmlNextElementSibling(work_directory_node); - xmlNodePtr result_directory_node = xmlNextElementSibling(local_directory_node); - - // Parameters for COORM - xmlNodePtr launcher_file_node = xmlNextElementSibling(result_directory_node); - - if (job_file_node == NULL || - env_file_node == NULL || - work_directory_node == NULL || - local_directory_node == NULL || - result_directory_node == NULL || - // For COORM - launcher_file_node == NULL - ) - { - INFOS("A bad job is found, some user_part are not found"); - delete new_job; - break; - } - if (xmlStrcmp(job_file_node->name, xmlCharStrdup("job_file")) || - xmlStrcmp(env_file_node->name, xmlCharStrdup("env_file")) || - xmlStrcmp(work_directory_node->name, xmlCharStrdup("work_directory")) || - xmlStrcmp(local_directory_node->name, xmlCharStrdup("local_directory")) || - xmlStrcmp(result_directory_node->name, xmlCharStrdup("result_directory")) || - // For COORM - xmlStrcmp(launcher_file_node->name, xmlCharStrdup("launcher_file")) - ) - { - INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name"); - delete new_job; - break; - } - xmlChar* job_file = xmlNodeGetContent(job_file_node); - try - { - new_job->setJobFile(std::string((const char *)job_file)); - } - catch(const LauncherException &ex) - { - INFOS("Exception receice for job_file, cannot add the job" << ex.msg.c_str()); - delete new_job; - xmlFree(job_file); - break; - } - xmlChar* env_file = xmlNodeGetContent(env_file_node); - xmlChar* work_directory = xmlNodeGetContent(work_directory_node); - xmlChar* local_directory = xmlNodeGetContent(local_directory_node); - xmlChar* result_directory = xmlNodeGetContent(result_directory_node); - - // Parameters for COORM - xmlChar* launcher_file = xmlNodeGetContent(launcher_file_node); - - new_job->setEnvFile(std::string((const char *)env_file)); - new_job->setWorkDirectory(std::string((const char *)work_directory)); - new_job->setLocalDirectory(std::string((const char *)local_directory)); - new_job->setResultDirectory(std::string((const char *)result_directory)); - - // Parameters for COORM - new_job->setLauncherFile(std::string((const char *)launcher_file)); - - xmlFree(job_file); - xmlFree(env_file); - xmlFree(work_directory); - xmlFree(local_directory); - xmlFree(result_directory); - - // Parameters for COORM - xmlFree(launcher_file); - - // Get in and out files - xmlNodePtr files_node = xmlNextElementSibling(launcher_file_node); - if (files_node == NULL) - { - INFOS("A bad job is found, user_part files is not found"); - delete new_job; - break; - } - if (xmlStrcmp(files_node->name, xmlCharStrdup("files"))) - { - INFOS("A bad job is found, files node are not in the rigth place or does not have a correct name or does not exist"); - delete new_job; - break; - } - xmlNodePtr file_node = xmlFirstElementChild(files_node); - while (file_node != NULL) - { - if (!xmlStrcmp(file_node->name, xmlCharStrdup("in_file"))) - { - xmlChar* in_file = xmlNodeGetContent(file_node); - new_job->add_in_file(std::string((const char *)in_file)); - xmlFree(in_file); - } - else if (!xmlStrcmp(file_node->name, xmlCharStrdup("out_file"))) - { - xmlChar* out_file = xmlNodeGetContent(file_node); - new_job->add_out_file(std::string((const char *)out_file)); - xmlFree(out_file); - } - file_node = xmlNextElementSibling(file_node); - } - - // Get resource part - xmlNodePtr res_node = xmlNextElementSibling(files_node); - xmlNodePtr maximum_duration_node = xmlNextElementSibling(res_node); - xmlNodePtr queue_node = xmlNextElementSibling(maximum_duration_node); - xmlNodePtr exclusive_node = xmlNextElementSibling(queue_node); - xmlNodePtr mem_per_cpu_node = xmlNextElementSibling(exclusive_node); - xmlNodePtr launcher_args_node = xmlNextElementSibling(mem_per_cpu_node); - if (res_node == NULL || - maximum_duration_node == NULL || - queue_node == NULL || - exclusive_node == NULL || - mem_per_cpu_node == NULL || - // For COORM - launcher_args_node == NULL - ) - { - INFOS("A bad job is found, some user_part are not found"); - delete new_job; - break; - } - if (xmlStrcmp(res_node->name, xmlCharStrdup("resource_params")) || - xmlStrcmp(maximum_duration_node->name, xmlCharStrdup("maximum_duration")) || - xmlStrcmp(queue_node->name, xmlCharStrdup("queue")) || - xmlStrcmp(exclusive_node->name, xmlCharStrdup("exclusive")) || - xmlStrcmp(mem_per_cpu_node->name, xmlCharStrdup("mem_per_cpu")) || - // For COORM - xmlStrcmp(launcher_args_node->name, xmlCharStrdup("launcher_args")) - ) - { - INFOS("A bad job is found, some user part node are not in the rigth or does not have a correct name"); - delete new_job; - break; - } - xmlChar* maximum_duration = xmlNodeGetContent(maximum_duration_node); - try - { - new_job->setMaximumDuration(std::string((const char *)maximum_duration)); - } - catch(const LauncherException &ex) - { - INFOS("Exception receice for maximum_duration, cannot add the job" << ex.msg.c_str()); - delete new_job; - xmlFree(maximum_duration); - break; - } - xmlChar* queue = xmlNodeGetContent(queue_node); - new_job->setQueue(std::string((const char *)queue)); - xmlFree(maximum_duration); - xmlFree(queue); - - xmlChar* exclusive = xmlNodeGetContent(exclusive_node); - try - { - new_job->setExclusiveStr(std::string((const char *)exclusive)); - } - catch(const LauncherException &ex) - { - INFOS("Exception received for exclusive, cannot add the job. " << ex.msg.c_str()); - delete new_job; - xmlFree(exclusive); - break; - } - xmlFree(exclusive); - - xmlChar* mem_per_cpu_str = xmlNodeGetContent(mem_per_cpu_node); - std::istringstream mem_per_cpu_stream((const char *)mem_per_cpu_str); - unsigned long mem_per_cpu = 0; - if (!(mem_per_cpu_stream >> mem_per_cpu)) - { - INFOS("A bad job is found, mem_per_cpu parameter is not correct"); - delete new_job; - break; - } - else - new_job->setMemPerCpu(mem_per_cpu); - - // For COORM - xmlChar* launcher_args = xmlNodeGetContent(launcher_args_node); - new_job->setLauncherArgs(std::string((const char *)launcher_args)); - xmlFree(launcher_args); - - xmlNodePtr specific_node = xmlNextElementSibling(launcher_args_node); - if (specific_node == NULL) - { - INFOS("A bad job is found, specific_parameters part is not found"); - delete new_job; - break; - } - xmlNodePtr parameter_node = xmlFirstElementChild(specific_node); - while (parameter_node != NULL) - { - if (!xmlStrcmp(parameter_node->name, xmlCharStrdup("specific_parameter"))) - { - xmlNodePtr name_node = xmlFirstElementChild(parameter_node); - xmlNodePtr value_node = xmlNextElementSibling(name_node); - if (name_node == NULL || - value_node == NULL) - { - INFOS("A bad job is found, specific_parameter parts are not found"); - delete new_job; - break; - } - if (xmlStrcmp(name_node->name, xmlCharStrdup("name")) || - xmlStrcmp(value_node->name, xmlCharStrdup("value"))) - { - INFOS("A bad job is found, specific_parameter bad parts are found"); - delete new_job; - break; - } - - xmlChar* name = xmlNodeGetContent(name_node); - xmlChar* value = xmlNodeGetContent(value_node); - try - { - new_job->addSpecificParameter(std::string((const char*)name), std::string((const char*)value)); - xmlFree(name); - xmlFree(value); - } - catch(const LauncherException &ex) - { - INFOS("Exception receice for a specific parameter, cannot add the job" << ex.msg.c_str()); - delete new_job; - xmlFree(name); - xmlFree(value); - break; - } - } - else - { - INFOS("A bad job is found, specific_parameters part is bad, a node that is not a specific parameter is found"); - delete new_job; - break; - } - parameter_node = xmlNextElementSibling(parameter_node); - } - - xmlNodePtr res_name_node = xmlFirstElementChild(res_node); - xmlNodePtr res_hostname_node = xmlNextElementSibling(res_name_node); - xmlNodePtr res_os_node = xmlNextElementSibling(res_hostname_node); - xmlNodePtr res_nb_proc_node = xmlNextElementSibling(res_os_node); - xmlNodePtr res_nb_node_node = xmlNextElementSibling(res_nb_proc_node); - xmlNodePtr res_nb_proc_per_node_node = xmlNextElementSibling(res_nb_node_node); - xmlNodePtr res_cpu_clock_node = xmlNextElementSibling(res_nb_proc_per_node_node); - xmlNodePtr res_mem_mb_node = xmlNextElementSibling(res_cpu_clock_node); - if (res_name_node == NULL || - res_hostname_node == NULL || - res_os_node == NULL || - res_nb_proc_node == NULL || - res_nb_node_node == NULL || - res_nb_proc_per_node_node == NULL || - res_cpu_clock_node == NULL || - res_mem_mb_node == NULL - ) - { - INFOS("A bad job is found, some resource_params user_part are not found"); - delete new_job; - break; - } - if (xmlStrcmp(res_name_node->name, xmlCharStrdup("name")) || - xmlStrcmp(res_hostname_node->name, xmlCharStrdup("hostname")) || - xmlStrcmp(res_os_node->name, xmlCharStrdup("OS")) || - xmlStrcmp(res_nb_proc_node->name, xmlCharStrdup("nb_proc")) || - xmlStrcmp(res_nb_node_node->name, xmlCharStrdup("nb_node")) || - xmlStrcmp(res_nb_proc_per_node_node->name, xmlCharStrdup("nb_proc_per_node")) || - xmlStrcmp(res_cpu_clock_node->name, xmlCharStrdup("cpu_clock")) || - xmlStrcmp(res_mem_mb_node->name, xmlCharStrdup("mem_mb")) - ) - { - INFOS("A bad job is found, some resource_params user_part node are not in the rigth or does not have a correct name"); - delete new_job; - break; - } - xmlChar* res_name = xmlNodeGetContent(res_name_node); - xmlChar* res_hostname = xmlNodeGetContent(res_hostname_node); - xmlChar* res_os = xmlNodeGetContent(res_os_node); - resourceParams p; - p.name = std::string((const char*) res_name); - p.hostname = std::string((const char*) res_hostname); - p.OS = std::string((const char*) res_os); - xmlFree(res_name); - xmlFree(res_hostname); - xmlFree(res_os); - xmlChar* res_nb_proc = xmlNodeGetContent(res_nb_proc_node); - xmlChar* res_nb_node = xmlNodeGetContent(res_nb_node_node); - xmlChar* res_nb_proc_per_node = xmlNodeGetContent(res_nb_proc_per_node_node); - xmlChar* res_cpu_clock = xmlNodeGetContent(res_cpu_clock_node); - xmlChar* res_mem_mb = xmlNodeGetContent(res_mem_mb_node); - bool import_value = true; - std::istringstream nb_proc_stream((const char *) res_nb_proc); - if (!(nb_proc_stream >> p.nb_proc)) - import_value = false; - std::istringstream nb_node_stream((const char *) res_nb_node); - if (!(nb_node_stream >> p.nb_node)) - import_value = false; - std::istringstream nb_proc_per_node_stream((const char *) res_nb_proc_per_node); - if (!(nb_proc_per_node_stream >> p.nb_proc_per_node)) - import_value = false; - std::istringstream cpu_clock_stream((const char *) res_cpu_clock); - if (!(cpu_clock_stream >> p.cpu_clock)) - import_value = false; - std::istringstream mem_mb_stream((const char *) res_mem_mb); - if (!(mem_mb_stream >> p.mem_mb)) - import_value = false; - xmlFree(res_nb_proc); - xmlFree(res_nb_node); - xmlFree(res_nb_proc_per_node); - xmlFree(res_cpu_clock); - xmlFree(res_mem_mb); - if (!import_value) - { - INFOS("A bad job is found, some resource_params value are not correct"); - delete new_job; - break; - } - try - { - new_job->setResourceRequiredParams(p); - } - catch(const LauncherException &ex) - { - INFOS("A bad job is found, an error when inserting resource_params:" << ex.msg.c_str()); - delete new_job; - break; - } - - // We finally get run part to figure out what to do - xmlNodePtr job_state_node = xmlFirstElementChild(run_node); - xmlNodePtr resource_choosed_name_node = xmlNextElementSibling(job_state_node); - xmlNodePtr job_reference_node = xmlNextElementSibling(resource_choosed_name_node); - if (job_state_node == NULL || - resource_choosed_name_node == NULL || - job_reference_node == NULL - ) - { - INFOS("A bad job is found, some run_part are not found"); - delete new_job; - break; - } - if (xmlStrcmp(job_state_node->name, xmlCharStrdup("job_state")) || - xmlStrcmp(resource_choosed_name_node->name, xmlCharStrdup("resource_choosed_name")) || - xmlStrcmp(job_reference_node->name, xmlCharStrdup("job_reference")) - ) - { - INFOS("A bad job is found, some run_part nodes are not in the rigth or does not have a correct name"); - delete new_job; - break; - } - xmlChar* job_state_xml = xmlNodeGetContent(job_state_node); - xmlChar* resource_choosed_name_xml = xmlNodeGetContent(resource_choosed_name_node); - xmlChar* job_reference_xml = xmlNodeGetContent(job_reference_node); - std::string job_state((const char *) job_state_xml); - std::string resource_choosed_name((const char *) resource_choosed_name_xml); - std::string job_reference((const char *) job_reference_xml); - xmlFree(job_state_xml); - xmlFree(resource_choosed_name_xml); - xmlFree(job_reference_xml); - - if (job_state == "CREATED") - { - // In this case, we ignore run_part informations - try - { - _l.createJob(new_job); - std::ostringstream job_id; - job_id << new_job->getNumber(); - notifyObservers("NEW_JOB", job_id.str()); - } - catch(const LauncherException &ex) - { - INFOS("Load failed: " << ex.msg.c_str()); - } - } - else if (job_state == "QUEUED" || - job_state == "RUNNING" || - job_state == "IN_PROCESS" || - job_state == "PAUSED") - { - try - { - new_job->setState(job_state); - _l.addJobDirectlyToMap(new_job, job_reference); - - // Step 4: We check that the BatchManager could resume - // the job -#ifdef WITH_LIBBATCH - if (new_job->getBatchManagerJobId().getReference() != job_reference) - { - INFOS("BatchManager type cannot resume a job - job state is set to ERROR"); - new_job->setState("ERROR"); - } -#endif - std::ostringstream job_id; - job_id << new_job->getNumber(); - notifyObservers("NEW_JOB", job_id.str()); - } - catch(const LauncherException &ex) - { - INFOS("Cannot load the job! Exception: " << ex.msg.c_str()); - delete new_job; - } - } - else if (job_state == "FINISHED" || - job_state == "FAILED" || - job_state == "ERROR") - { - try - { - // Step 2: We add run_part informations - new_job->setState(job_state); - _l.addJobDirectlyToMap(new_job, job_reference); - std::ostringstream job_id; - job_id << new_job->getNumber(); - notifyObservers("NEW_JOB", job_id.str()); - } - catch(const LauncherException &ex) - { - INFOS("Cannot load the job! Exception: " << ex.msg.c_str()); - delete new_job; - } - } - else - { - INFOS("A bad job is found, state unknown " << job_state); - delete new_job; - } - - } - xmlCurrentNode = xmlCurrentNode->next; - } - } - else - { - xmlFreeDoc(doc); - fclose(xml_file); - std::string error = "Error in xml file, could not find root_node named jobs: " + std::string(jobs_file); - INFOS(error); - THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR); - } - - // Clean - xmlFreeDoc(doc); - fclose(xml_file); notifyObservers("LOAD_JOBS", jobs_file); } @@ -1067,48 +563,7 @@ SALOME_Launcher::loadJobs(const char* jobs_file) void SALOME_Launcher::saveJobs(const char* jobs_file) { - - // Step 1: check jobs_file write access - FILE* xml_file = fopen(jobs_file, "w"); - if (xml_file == NULL) - { - std::string error = "Error opening jobs_file in SALOME_Launcher::saveJobs: " + std::string(jobs_file); - INFOS(error); - THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR); - } - - // Step 2: First lines - xmlKeepBlanksDefault(0); - xmlDocPtr doc = xmlNewDoc(xmlCharStrdup("1.0")); - xmlNodePtr root_node = xmlNewNode(NULL, xmlCharStrdup("jobs")); - xmlDocSetRootElement(doc, root_node); - xmlNodePtr doc_comment = xmlNewDocComment(doc, xmlCharStrdup("SALOME Launcher save jobs file")); - xmlAddPrevSibling(root_node, doc_comment); - - // Step 3: For each job write it on the xml document - // We could put a mutex but are not foing to do that currently - std::map jobs_list = _l.getJobs(); - std::map::const_iterator it_job; - for(it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++) - { - addToXmlDocument(root_node, it_job->second); - } - - // Final step: write file - int isOk = xmlSaveFormatFile(jobs_file, doc, 1); - if (!isOk) - { - std::string error = "Error during xml file saving in SALOME_Launcher::saveJobs: " + std::string(jobs_file); - INFOS(error); - xmlFreeDoc(doc); - fclose(xml_file); - THROW_SALOME_CORBA_EXCEPTION(error.c_str(), SALOME::INTERNAL_ERROR); - } - - // Clean - xmlFreeDoc(doc); - fclose(xml_file); - MESSAGE("SALOME_Launcher::saveJobs : WRITING DONE!"); + _l.saveJobs(jobs_file); notifyObservers("SAVE_JOBS", jobs_file); }