1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
31 #include <libbatch/BatchManagerCatalog.hxx>
32 #include <libbatch/FactBatchManager.hxx>
33 #include <libbatch/BatchManager.hxx>
36 #include "Basics_Utils.hxx"
37 #include "Basics_DirUtils.hxx"
38 #include "SALOME_Launcher_Handler.hxx"
39 #include "Launcher.hxx"
40 #include "Launcher_Job_Command.hxx"
41 #include "Launcher_Job_YACSFile.hxx"
42 #include "Launcher_Job_PythonSALOME.hxx"
43 #include "Launcher_Job_CommandSALOME.hxx"
44 #include "Launcher_XML_Persistence.hxx"
48 //=============================================================================
52 * Define a CORBA single thread policy for the server, which avoid to deal
53 * with non thread-safe usage like Change_Directory in SALOME naming service
55 //=============================================================================
56 Launcher_cpp::Launcher_cpp()
58 LAUNCHER_MESSAGE("Launcher_cpp constructor");
62 //=============================================================================
66 //=============================================================================
67 Launcher_cpp::~Launcher_cpp()
69 LAUNCHER_MESSAGE("Launcher_cpp destructor");
71 std::map<int, Launcher::Job *>::const_iterator it_job;
72 for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
73 delete it_job->second;
74 std::map <int, Batch::BatchManager * >::const_iterator it1;
75 for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
82 //=============================================================================
84 * Add a job into the launcher - check resource and choose one
86 //=============================================================================
88 Launcher_cpp::createJob(Launcher::Job * new_job)
90 LAUNCHER_MESSAGE("Creating a new job");
91 // Add job to the jobs map
92 new_job->setNumber(_job_cpt);
94 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
95 if (it_job == _launcher_job_map.end())
97 _launcher_job_map[new_job->getNumber()] = new_job;
101 LAUNCHER_INFOS("A job has already the same id: " << new_job->getNumber());
102 throw LauncherException("A job has already the same id - job is not created !");
104 LAUNCHER_MESSAGE("New Job created");
107 //=============================================================================
109 * Add a job into the launcher - check resource and choose one
111 //=============================================================================
113 Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
115 std::string job_type = job_parameters.job_type;
116 Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
118 if (job_type == Launcher::Job_Command::TYPE_NAME)
119 new_job = new Launcher::Job_Command();
120 else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME)
121 new_job = new Launcher::Job_CommandSALOME();
122 else if (job_type == Launcher::Job_YACSFile::TYPE_NAME)
123 new_job = new Launcher::Job_YACSFile();
124 else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME)
125 new_job = new Launcher::Job_PythonSALOME();
128 std::string message("Launcher_cpp::createJob: bad job type: ");
130 throw LauncherException(message.c_str());
134 new_job->setJobName(job_parameters.job_name);
137 new_job->setWorkDirectory(job_parameters.work_directory);
138 new_job->setLocalDirectory(job_parameters.local_directory);
139 new_job->setResultDirectory(job_parameters.result_directory);
141 // Parameters for COORM
142 new_job->setLauncherFile(job_parameters.launcher_file);
143 new_job->setLauncherArgs(job_parameters.launcher_args);
146 new_job->setJobFile(job_parameters.job_file);
147 new_job->setPreCommand(job_parameters.pre_command);
150 new_job->setEnvFile(job_parameters.env_file);
151 for(const std::string param : job_parameters.in_files)
152 new_job->add_in_file(param);
153 for(const std::string param : job_parameters.out_files)
154 new_job->add_out_file(param);
156 new_job->setMaximumDuration(job_parameters.maximum_duration);
157 new_job->setQueue(job_parameters.queue);
158 new_job->setPartition(job_parameters.partition);
159 new_job->setExclusive(job_parameters.exclusive);
160 new_job->setMemPerCpu(job_parameters.mem_per_cpu);
161 new_job->setWCKey(job_parameters.wckey);
162 new_job->setExtraParams(job_parameters.extra_params);
164 // Resources requirements
165 new_job->setResourceRequiredParams(job_parameters.resource_required);
167 // Adding specific parameters to the job
168 for (const auto& it_specific : job_parameters.specific_parameters)
169 new_job->addSpecificParameter(it_specific.first, it_specific.second);
171 new_job->checkSpecificParameters();
175 return new_job->getNumber();
178 //=============================================================================
182 //=============================================================================
184 Launcher_cpp::launchJob(int job_id)
186 LAUNCHER_MESSAGE("Launch a job");
188 // Check if job exists
189 Launcher::Job * job = findJob(job_id);
191 // Check job state (cannot launch a job already launched...)
192 if (job->getState() != "CREATED")
194 LAUNCHER_INFOS("Bad state of the job: " << job->getState());
195 throw LauncherException("Bad state of the job: " + job->getState());
198 Batch::BatchManager * bm = getBatchManager(job);
201 Batch::JobId batch_manager_job_id = bm->submitJob(*(job->getBatchJob()));
202 job->setBatchManagerJobId(batch_manager_job_id);
203 job->setState("QUEUED");
204 job->setReference(batch_manager_job_id.getReference());
206 catch(const Batch::GenericException &ex)
208 LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
209 throw LauncherException(ex.message.c_str());
211 LAUNCHER_MESSAGE("Job launched");
214 //=============================================================================
218 //=============================================================================
220 Launcher_cpp::getJobState(int job_id)
222 LAUNCHER_MESSAGE("Get job state");
224 // Check if job exist
225 Launcher::Job * job = findJob(job_id);
230 state = job->updateJobState();
232 catch(const Batch::GenericException &ex)
234 LAUNCHER_INFOS("getJobState failed, exception: " << ex.message);
235 throw LauncherException(ex.message.c_str());
241 //=============================================================================
243 * Get job assigned hostnames
245 //=============================================================================
247 Launcher_cpp::getAssignedHostnames(int job_id)
249 LAUNCHER_MESSAGE("Get job assigned hostnames");
251 // Check if job exist
252 Launcher::Job * job = findJob(job_id);
253 std::string assigned_hostnames = job->getAssignedHostnames();
255 return assigned_hostnames;
258 //=============================================================================
260 * Get Job result - the result directory could be changed
262 //=============================================================================
264 Launcher_cpp::getJobResults(int job_id, std::string directory)
266 LAUNCHER_MESSAGE("Get Job results");
268 Launcher::Job * job = findJob(job_id);
269 std::string resource_name = job->getResourceDefinition().Name;
273 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
275 _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
277 catch(const Batch::GenericException &ex)
279 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
280 throw LauncherException(ex.message.c_str());
282 LAUNCHER_MESSAGE("getJobResult ended");
285 //=============================================================================
287 * Clear the remote working directory
289 //=============================================================================
291 Launcher_cpp::clearJobWorkingDir(int job_id)
293 LAUNCHER_MESSAGE("Clear the remote working directory");
295 Launcher::Job * job = findJob(job_id);
298 _batchmap[job_id]->clearWorkingDir(*(job->getBatchJob()));
300 catch(const Batch::GenericException &ex)
302 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
303 throw LauncherException(ex.message.c_str());
305 LAUNCHER_MESSAGE("getJobResult ended");
308 //=============================================================================
310 * Get Job dump state - the result directory could be changed
312 //=============================================================================
314 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
317 LAUNCHER_MESSAGE("Get Job dump state");
319 Launcher::Job * job = findJob(job_id);
320 std::string resource_name = job->getResourceDefinition().Name;
324 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
326 rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), job->getResultDirectory());
328 catch(const Batch::GenericException &ex)
330 LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
331 throw LauncherException(ex.message.c_str());
333 LAUNCHER_MESSAGE("getJobResult ended");
337 //=============================================================================
339 * Get one file from the working directory - the result directory can be changed
341 //=============================================================================
343 Launcher_cpp::getJobWorkFile(int job_id,
344 std::string work_file,
345 std::string directory)
348 LAUNCHER_MESSAGE("Get working file " << work_file);
350 Launcher::Job * job = findJob(job_id);
351 std::string resource_name = job->getResourceDefinition().Name;
355 rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, directory);
357 rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, job->getResultDirectory());
359 catch(const Batch::GenericException &ex)
361 LAUNCHER_INFOS("getJobWorkFile is maybe incomplete, exception: " << ex.message);
362 throw LauncherException(ex.message.c_str());
364 LAUNCHER_MESSAGE("getJobWorkFile ended");
368 //=============================================================================
370 * Remove the job - into the Launcher and its batch manager
372 //=============================================================================
374 Launcher_cpp::removeJob(int job_id)
376 LAUNCHER_MESSAGE("Remove Job");
378 // Check if job exist
379 std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
380 if (it_job == _launcher_job_map.end())
382 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
383 throw LauncherException("Cannot find the job, is it created ?");
386 it_job->second->removeJob();
387 delete it_job->second;
388 _launcher_job_map.erase(it_job);
391 //=============================================================================
395 //=============================================================================
397 Launcher_cpp::stopJob(int job_id)
399 LAUNCHER_MESSAGE("Stop Job");
401 Launcher::Job * job = findJob(job_id);
406 Launcher_cpp::dumpJob(int job_id)
408 LAUNCHER_MESSAGE("dump Job");
410 Launcher::Job * job = findJob(job_id);
411 return Launcher::XML_Persistence::dumpJob(*job);
415 Launcher_cpp::restoreJob(const std::string& dumpedJob)
417 LAUNCHER_MESSAGE("restore Job");
418 Launcher::Job* new_job(nullptr);
423 new_job = Launcher::XML_Persistence::createJobFromString(dumpedJob);
427 jobId = addJob(new_job);
430 LAUNCHER_INFOS("Failed to create a new job.");
432 catch(const LauncherException &ex)
434 LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
440 Launcher_cpp::getJobParameters(int job_id)
442 Launcher::Job * job = findJob(job_id);
443 JobParameters_cpp job_parameters;
444 job_parameters.job_name = job->getJobName();
445 job_parameters.job_type = job->getJobType();
446 job_parameters.job_file = job->getJobFile();
447 job_parameters.env_file = job->getEnvFile();
448 job_parameters.work_directory = job->getWorkDirectory();
449 job_parameters.local_directory = job->getLocalDirectory();
450 job_parameters.result_directory = job->getResultDirectory();
451 job_parameters.pre_command = job->getPreCommand();
453 // Parameters for COORM
454 job_parameters.launcher_file = job->getLauncherFile();
455 job_parameters.launcher_args = job->getLauncherArgs();
457 job_parameters.in_files = job->get_in_files();
458 job_parameters.out_files = job->get_out_files();
460 job_parameters.maximum_duration = job->getMaximumDuration();
461 job_parameters.queue = job->getQueue();
462 job_parameters.partition = job->getPartition();
463 job_parameters.exclusive = job->getExclusive();
464 job_parameters.mem_per_cpu = job->getMemPerCpu();
465 job_parameters.wckey = job->getWCKey();
466 job_parameters.extra_params = job->getExtraParams();
468 resourceParams resource_params = job->getResourceRequiredParams();
469 job_parameters.resource_required.name = resource_params.name;
470 job_parameters.resource_required.hostname = resource_params.hostname;
471 job_parameters.resource_required.OS = resource_params.OS;
472 job_parameters.resource_required.nb_proc = resource_params.nb_proc;
473 job_parameters.resource_required.nb_node = resource_params.nb_node;
474 job_parameters.resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
475 job_parameters.resource_required.cpu_clock = resource_params.cpu_clock;
476 job_parameters.resource_required.mem_mb = resource_params.mem_mb;
478 job_parameters.specific_parameters = job->getSpecificParameters();
480 return job_parameters;
483 //=============================================================================
485 * create a launcher job based on a file
486 * \param xmlExecuteFile : to define the execution on the batch cluster
488 //=============================================================================
490 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile,
491 const std::string clusterName)
493 LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
496 ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
498 // Creating a new job
499 std::unique_ptr<Launcher::Job_Command> new_job(new Launcher::Job_Command);
501 std::string cmdFile = Kernel_Utils::GetTmpFileName();
508 os.open(cmdFile.c_str(), std::ofstream::out );
509 os << "#! /bin/sh" << std::endl;
510 os << job_params.Command;
513 new_job->setJobFile(cmdFile);
514 new_job->setLocalDirectory(job_params.RefDirectory);
515 new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
516 new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
518 for(int i=0; i < job_params.InputFile.size(); i++)
519 new_job->add_in_file(job_params.InputFile[i]);
520 for(int i=0; i < job_params.OutputFile.size();i++)
521 new_job->add_out_file(job_params.OutputFile[i]);
524 p.hostname = clusterName;
527 p.nb_proc = job_params.NbOfProcesses;
529 p.nb_proc_per_node = 0;
532 new_job->setResourceRequiredParams(p);
534 createJob(new_job.get());
535 return new_job->getNumber();
538 //=============================================================================
540 * Factory to instantiate the good batch manager for chosen cluster.
542 //=============================================================================
543 Batch::BatchManager *
544 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
547 Batch::CommunicationProtocolType protocol;
548 Batch::FactBatchManager * fact;
550 std::string hostname = params.HostName;
552 switch(params.Protocol)
555 protocol = Batch::SH;
558 protocol = Batch::RSH;
561 protocol = Batch::SSH;
564 protocol = Batch::RSYNC;
567 throw LauncherException("Unknown protocol for this resource");
599 switch( params.Batch )
632 LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
633 throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
635 Batch::BatchManagerCatalog & cata = Batch::BatchManagerCatalog::getInstance();
636 fact = dynamic_cast<Batch::FactBatchManager*>(cata(bmType));
638 LAUNCHER_MESSAGE("Cannot find batch manager factory for " << bmType << ". Check your version of libBatch.");
639 throw LauncherException("Cannot find batch manager factory");
641 LAUNCHER_MESSAGE("Instantiation of batch manager of type: " << bmType);
642 Batch::BatchManager * batch_client = (*fact)(hostname.c_str(), params.UserName.c_str(),
643 protocol, mpi.c_str());
647 //----------------------------------------------------------
648 // Without LIBBATCH - Launcher_cpp do nothing...
649 //----------------------------------------------------------
653 Launcher_cpp::createJob(Launcher::Job * new_job)
655 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
656 throw LauncherException("Method Launcher_cpp::createJob is not available "
657 "(libBatch was not present at compilation time)");
661 Launcher_cpp::createJob(const JobParameters_cpp& job_parameters)
663 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
664 throw LauncherException("Method Launcher_cpp::createJob is not available "
665 "(libBatch was not present at compilation time)");
669 Launcher_cpp::launchJob(int job_id)
671 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
672 throw LauncherException("Method Launcher_cpp::launchJob is not available "
673 "(libBatch was not present at compilation time)");
677 Launcher_cpp::getJobState(int job_id)
679 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
680 throw LauncherException("Method Launcher_cpp::getJobState is not available "
681 "(libBatch was not present at compilation time)");
685 Launcher_cpp::getAssignedHostnames(int job_id)
687 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
688 throw LauncherException("Method Launcher_cpp::getAssignedHostnames is not available "
689 "(libBatch was not present at compilation time)");
693 Launcher_cpp::getJobResults(int job_id, std::string directory)
695 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
696 throw LauncherException("Method Launcher_cpp::getJobResults is not available "
697 "(libBatch was not present at compilation time)");
701 Launcher_cpp::clearJobWorkingDir(int job_id)
703 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot clear directory!!!");
704 throw LauncherException("Method Launcher_cpp::clearJobWorkingDir is not available "
705 "(libBatch was not present at compilation time)");
709 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
711 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
712 throw LauncherException("Method Launcher_cpp::getJobDumpState is not available "
713 "(libBatch was not present at compilation time)");
717 Launcher_cpp::getJobWorkFile(int job_id, std::string work_file, std::string directory)
719 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
720 throw LauncherException("Method Launcher_cpp::getJobWorkFile is not available "
721 "(libBatch was not present at compilation time)");
725 Launcher_cpp::removeJob(int job_id)
727 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
728 throw LauncherException("Method Launcher_cpp::removeJob is not available "
729 "(libBatch was not present at compilation time)");
733 Launcher_cpp::stopJob(int job_id)
735 throw LauncherException("Method Launcher_cpp::stopJob is not available "
736 "(libBatch was not present at compilation time)");
740 Launcher_cpp::dumpJob(int job_id)
742 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot dump job!!!");
743 throw LauncherException("Method Launcher_cpp::dumpJob is not available "
744 "(libBatch was not present at compilation time)");
749 Launcher_cpp::restoreJob(const std::string& dumpedJob)
751 LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot restore job!!!");
752 throw LauncherException("Method Launcher_cpp::restoreJob is not available "
753 "(libBatch was not present at compilation time)");
758 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
760 throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
761 "(libBatch was not present at compilation time)");
767 Launcher_cpp::getJobParameters(int job_id)
769 throw LauncherException("Method Launcher_cpp::getJobParamaters is not available "
770 "(libBatch was not present at compilation time)");
776 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
778 ParserLauncherType job_params;
779 SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
781 const char* aFilePath = xmlExecuteFile.c_str();
782 FILE* aFile = fopen(aFilePath, "r");
785 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
787 handler->ProcessXmlDocument(aDoc);
790 std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
791 LAUNCHER_MESSAGE(message);
793 throw LauncherException(message);
801 std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
802 LAUNCHER_MESSAGE(message);
804 throw LauncherException(message);
812 std::map<int, Launcher::Job *>
813 Launcher_cpp::getJobs()
815 return _launcher_job_map;
820 Launcher_cpp::getBatchManager(Launcher::Job * job)
823 throw LauncherException("Resource manager is not set.");
825 Batch::BatchManager* result = nullptr;
826 int job_id = job->getNumber();
828 // Select a resource for the job
829 std::vector<std::string> ResourceList;
830 resourceParams params = job->getResourceRequiredParams();
831 // Consider only resources that can launch batch jobs
832 params.can_launch_batch_jobs = true;
835 ResourceList = _ResManager->GetFittingResources(params);
837 catch(const ResourcesException &ex)
839 throw LauncherException(ex.msg.c_str());
841 if (ResourceList.size() == 0)
843 LAUNCHER_INFOS("No adequate resource found for the job, number " << job->getNumber());
844 job->setState("ERROR");
845 throw LauncherException("No resource found the job");
848 // Configure the job with the resource selected - the first of the list
849 ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
851 // Set resource definition to the job
852 // The job will check if the definitions needed
855 job->setResourceDefinition(resource_definition);
857 catch(const LauncherException &ex)
859 LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
860 job->setState("ERROR");
864 // Step 2: We can now add a Factory if the resource is correctly define
865 std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
866 if(it == _batchmap.end())
870 // Warning cannot write on one line like this, because map object is constructed before
871 // the method is called...
872 //_batchmap[job_id] = FactoryBatchManager(resource_definition);
873 result = FactoryBatchManager(resource_definition);
874 _batchmap[job_id] = result;
876 catch(const LauncherException &ex)
878 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.msg);
881 catch(const Batch::GenericException &ex)
883 LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.message);
884 throw LauncherException(ex.message);
894 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
896 // Step 0: Calculated job_id
897 new_job->setNumber(_job_cpt);
901 // Step 1: check if resource is already in the map
902 Batch::BatchManager * bm = getBatchManager(new_job);
904 // Step 2: add the job to the batch manager
907 Batch::JobId batch_manager_job_id = bm->addJob(*(new_job->getBatchJob()),
908 new_job->getReference());
909 new_job->setBatchManagerJobId(batch_manager_job_id);
911 catch(const Batch::GenericException &ex)
913 LAUNCHER_INFOS("Job cannot be added, exception in addJob: " << ex.message);
914 throw LauncherException(ex.message.c_str());
917 // Step 3: add job to launcher map
918 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
919 if (it_job == _launcher_job_map.end())
921 _launcher_job_map[new_job->getNumber()] = new_job;
925 LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
926 throw LauncherException("A job as already the same id - job is not created !");
928 LAUNCHER_MESSAGE("New job added");
933 Launcher_cpp::addJob(Launcher::Job * new_job)
935 string job_state = new_job->getState();
937 if (job_state == "CREATED")
939 // In this case, we ignore run_part information
941 jobId = new_job->getNumber();
943 else if (job_state == "QUEUED" ||
944 job_state == "RUNNING" ||
945 job_state == "IN_PROCESS" ||
946 job_state == "PAUSED")
948 addJobDirectlyToMap(new_job);
949 jobId = new_job->getNumber();
951 // We check that the BatchManager could resume the job
953 if (new_job->getBatchManagerJobId().getReference() != new_job->getReference())
955 LAUNCHER_INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
956 new_job->setState("ERROR");
960 else if (job_state == "FINISHED" ||
961 job_state == "FAILED" ||
962 job_state == "ERROR")
964 // We add run_part information
965 addJobDirectlyToMap(new_job);
966 jobId = new_job->getNumber();
970 LAUNCHER_INFOS("A bad job is found, state unknown " << job_state);
977 Launcher_cpp::loadJobs(const char* jobs_file)
979 list<int> new_jobs_id_list;
981 // Load the jobs from XML file
982 list<Launcher::Job *> jobs_list = Launcher::XML_Persistence::loadJobs(jobs_file);
984 // Create each job in the launcher
985 list<Launcher::Job *>::const_iterator it_job;
986 for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
988 Launcher::Job* new_job(*it_job);
992 jobId = addJob(new_job);
994 new_jobs_id_list.push_back(jobId);
996 catch(const LauncherException &ex)
998 LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
1002 return new_jobs_id_list;
1006 Launcher_cpp::saveJobs(const char* jobs_file)
1008 // Create a sorted list from the internal job map
1009 list<const Launcher::Job *> jobs_list;
1011 for (int i=0; i<_job_cpt; i++)
1013 map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(i);
1014 if (it_job != _launcher_job_map.end())
1015 jobs_list.push_back(it_job->second);
1018 // Save the jobs in XML file
1019 Launcher::XML_Persistence::saveJobs(jobs_file, jobs_list);
1023 Launcher_cpp::findJob(int job_id)
1025 std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
1026 if (it_job == _launcher_job_map.end())
1028 LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
1029 throw LauncherException("Cannot find the job, is it created ?");
1031 Launcher::Job * job = it_job->second;