Salome HOME
Copyright update: 2016
[modules/kernel.git] / src / Launcher / Launcher.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <list>
24 #include <iostream>
25 #include <sstream>
26 #include <sys/stat.h>
27 #include <time.h>
28
29 #ifdef WITH_LIBBATCH
30 #include <libbatch/BatchManagerCatalog.hxx>
31 #include <libbatch/FactBatchManager.hxx>
32 #include <libbatch/BatchManager.hxx>
33 #endif
34
35 #include "Basics_Utils.hxx"
36 #include "Basics_DirUtils.hxx"
37 #include "SALOME_Launcher_Handler.hxx"
38 #include "Launcher.hxx"
39 #include "Launcher_Job_Command.hxx"
40 #include "Launcher_XML_Persistence.hxx"
41
42 using namespace std;
43
44 //=============================================================================
45 /*! 
46  *  Constructor
47  *  \param orb
48  *  Define a CORBA single thread policy for the server, which avoid to deal
49  *  with non thread-safe usage like Change_Directory in SALOME naming service
50  */
51 //=============================================================================
52 Launcher_cpp::Launcher_cpp()
53 {
54   LAUNCHER_MESSAGE("Launcher_cpp constructor");
55   _job_cpt = 0;
56   _job_cpt_mutex = new pthread_mutex_t();
57   pthread_mutex_init(_job_cpt_mutex, NULL);
58 }
59
60 //=============================================================================
61 /*! 
62  * destructor
63  */
64 //=============================================================================
65 Launcher_cpp::~Launcher_cpp()
66 {
67   LAUNCHER_MESSAGE("Launcher_cpp destructor");
68 #ifdef WITH_LIBBATCH
69   std::map<int, Launcher::Job *>::const_iterator it_job;
70   for(it_job = _launcher_job_map.begin(); it_job != _launcher_job_map.end(); it_job++)
71     delete it_job->second;
72   std::map <int, Batch::BatchManager * >::const_iterator it1;
73   for(it1=_batchmap.begin();it1!=_batchmap.end();it1++)
74     delete it1->second;
75 #endif
76
77   pthread_mutex_destroy(_job_cpt_mutex);
78   delete _job_cpt_mutex;
79 }
80
81 #ifdef WITH_LIBBATCH
82
83 //=============================================================================
84 /*!
85  * Add a job into the launcher - check resource and choose one 
86  */ 
87 //=============================================================================
88 void 
89 Launcher_cpp::createJob(Launcher::Job * new_job)
90 {
91   LAUNCHER_MESSAGE("Creating a new job");
92   // Add job to the jobs map
93   pthread_mutex_lock(_job_cpt_mutex);
94   new_job->setNumber(_job_cpt);
95   _job_cpt++;
96   pthread_mutex_unlock(_job_cpt_mutex);
97   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
98   if (it_job == _launcher_job_map.end())
99     _launcher_job_map[new_job->getNumber()] = new_job;
100   else
101   {
102     LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
103     delete new_job;
104     throw LauncherException("A job as already the same id - job is not created !");
105   }
106   LAUNCHER_MESSAGE("New Job created");
107 }
108
109 //=============================================================================
110 /*!
111  * Launch a job 
112  */ 
113 //=============================================================================
114 void 
115 Launcher_cpp::launchJob(int job_id)
116 {
117   LAUNCHER_MESSAGE("Launch a job");
118
119   // Check if job exist
120   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
121   if (it_job == _launcher_job_map.end())
122   {
123     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
124     throw LauncherException("Cannot find the job, is it created ?");
125   }
126
127   Launcher::Job * job = it_job->second;
128
129   // Check job state (cannot launch a job already launched...)
130   if (job->getState() != "CREATED")
131   {
132     LAUNCHER_INFOS("Bad state of the job: " << job->getState());
133     throw LauncherException("Bad state of the job: " + job->getState());
134   }
135
136   // Third step search batch manager for the job into the map -> instanciate one if does not exist
137 #ifdef WITH_LIBBATCH
138   std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
139   if(it == _batchmap.end())
140   {
141     createBatchManagerForJob(job);
142   }
143 #endif
144
145   try {
146     Batch::JobId batch_manager_job_id = _batchmap[job_id]->submitJob(*(job->getBatchJob()));
147     job->setBatchManagerJobId(batch_manager_job_id);
148     job->setState("QUEUED");
149     job->setReference(batch_manager_job_id.getReference());
150   }
151   catch(const Batch::GenericException &ex)
152   {
153     LAUNCHER_INFOS("Job is not launched, exception in submitJob: " << ex.message);
154     throw LauncherException(ex.message.c_str());
155   }
156   LAUNCHER_MESSAGE("Job launched");
157 }
158
159 //=============================================================================
160 /*!
161  * Get job state
162  */ 
163 //=============================================================================
164 const char *
165 Launcher_cpp::getJobState(int job_id)
166 {
167   LAUNCHER_MESSAGE("Get job state");
168
169   // Check if job exist
170   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
171   if (it_job == _launcher_job_map.end())
172   {
173     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
174     throw LauncherException("Cannot find the job, is it created ?");
175   }
176
177   Launcher::Job * job = it_job->second;
178
179   std::string state;
180   try
181   {
182     state = job->updateJobState();
183   }
184   catch(const Batch::GenericException &ex)
185   {
186     LAUNCHER_INFOS("getJobState failed, exception: " << ex.message);
187     throw LauncherException(ex.message.c_str());
188   }
189
190   return state.c_str();
191 }
192
193 //=============================================================================
194 /*!
195  * Get job assigned hostnames
196  */
197 //=============================================================================
198 const char *
199 Launcher_cpp::getAssignedHostnames(int job_id)
200 {
201   LAUNCHER_MESSAGE("Get job assigned hostnames");
202
203   // Check if job exist
204   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
205   if (it_job == _launcher_job_map.end())
206   {
207     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
208     throw LauncherException("Cannot find the job, is it created ?");
209   }
210
211   Launcher::Job * job = it_job->second;
212   std::string assigned_hostnames = job->getAssignedHostnames();
213
214   return assigned_hostnames.c_str();
215 }
216
217 //=============================================================================
218 /*!
219  * Get Job result - the result directory could be changed
220  */ 
221 //=============================================================================
222 void
223 Launcher_cpp::getJobResults(int job_id, std::string directory)
224 {
225   LAUNCHER_MESSAGE("Get Job results");
226
227   // Check if job exist
228   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
229   if (it_job == _launcher_job_map.end())
230   {
231     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
232     throw LauncherException("Cannot find the job, is it created ?");
233   }
234
235   Launcher::Job * job = it_job->second;
236   std::string resource_name = job->getResourceDefinition().Name;
237   try 
238   {
239     if (directory != "")
240       _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), directory);
241     else
242       _batchmap[job_id]->importOutputFiles(*(job->getBatchJob()), job->getResultDirectory());
243   }
244   catch(const Batch::GenericException &ex)
245   {
246     LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
247     throw LauncherException(ex.message.c_str());
248   }
249   LAUNCHER_MESSAGE("getJobResult ended");
250 }
251
252 //=============================================================================
253 /*!
254  * Get Job dump state - the result directory could be changed
255  */ 
256 //=============================================================================
257 bool
258 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
259 {
260   bool rtn;
261   LAUNCHER_MESSAGE("Get Job dump state");
262
263   // Check if job exist
264   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
265   if (it_job == _launcher_job_map.end())
266   {
267     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
268     throw LauncherException("Cannot find the job, is it created ?");
269   }
270
271   Launcher::Job * job = it_job->second;
272   std::string resource_name = job->getResourceDefinition().Name;
273   try 
274   {
275     if (directory != "")
276       rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), directory);
277     else
278       rtn = _batchmap[job_id]->importDumpStateFile(*(job->getBatchJob()), job->getResultDirectory());
279   }
280   catch(const Batch::GenericException &ex)
281   {
282     LAUNCHER_INFOS("getJobResult is maybe incomplete, exception: " << ex.message);
283     throw LauncherException(ex.message.c_str());
284   }
285   LAUNCHER_MESSAGE("getJobResult ended");
286   return rtn;
287 }
288
289 //=============================================================================
290 /*!
291  * Get one file from the working directory - the result directory can be changed
292  */
293 //=============================================================================
294 bool
295 Launcher_cpp::getJobWorkFile(int job_id,
296                              std::string work_file,
297                              std::string directory)
298 {
299   bool rtn;
300   LAUNCHER_MESSAGE("Get working file " << work_file);
301
302   // Check if job exist
303   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(job_id);
304   if (it_job == _launcher_job_map.end())
305   {
306     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
307     throw LauncherException("Cannot find the job, is it created ?");
308   }
309
310   Launcher::Job * job = it_job->second;
311   std::string resource_name = job->getResourceDefinition().Name;
312   try
313   {
314     if (directory != "")
315       rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, directory);
316     else
317       rtn = _batchmap[job_id]->importWorkFile(*(job->getBatchJob()), work_file, job->getResultDirectory());
318   }
319   catch(const Batch::GenericException &ex)
320   {
321     LAUNCHER_INFOS("getJobWorkFile is maybe incomplete, exception: " << ex.message);
322     throw LauncherException(ex.message.c_str());
323   }
324   LAUNCHER_MESSAGE("getJobWorkFile ended");
325   return rtn;
326 }
327
328 //=============================================================================
329 /*!
330  * Remove the job - into the Launcher and its batch manager
331  */ 
332 //=============================================================================
333 void
334 Launcher_cpp::removeJob(int job_id)
335 {
336   LAUNCHER_MESSAGE("Remove Job");
337
338   // Check if job exist
339   std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
340   if (it_job == _launcher_job_map.end())
341   {
342     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
343     throw LauncherException("Cannot find the job, is it created ?");
344   }
345
346   it_job->second->removeJob();
347   delete it_job->second;
348   _launcher_job_map.erase(it_job);
349 }
350
351 //=============================================================================
352 /*!
353  * stop the job
354  */ 
355 //=============================================================================
356 void
357 Launcher_cpp::stopJob(int job_id)
358 {
359   LAUNCHER_MESSAGE("Stop Job");
360
361   // Check if job exist
362   std::map<int, Launcher::Job *>::iterator it_job = _launcher_job_map.find(job_id);
363   if (it_job == _launcher_job_map.end())
364   {
365     LAUNCHER_INFOS("Cannot find the job, is it created ? job number: " << job_id);
366     throw LauncherException("Cannot find the job, is it created ?");
367   }
368
369   it_job->second->stopJob();
370 }
371
372 //=============================================================================
373 /*! 
374  *  create a launcher job based on a file
375  *  \param xmlExecuteFile     : to define the execution on the batch cluster
376  */
377 //=============================================================================
378 long 
379 Launcher_cpp::createJobWithFile(const std::string xmlExecuteFile, 
380                                 const std::string clusterName)
381 {
382   LAUNCHER_MESSAGE("Begin of Launcher_cpp::createJobWithFile");
383
384   // Parsing xml file
385   ParserLauncherType job_params = ParseXmlFile(xmlExecuteFile);
386
387   // Creating a new job
388   Launcher::Job_Command * new_job = new Launcher::Job_Command();
389
390   std::string cmdFile = Kernel_Utils::GetTmpFileName();  
391 #ifndef WIN32
392   cmdFile += ".sh";
393 #else
394   cmdFile += ".bat";
395 #endif
396   std::ofstream os;
397   os.open(cmdFile.c_str(), std::ofstream::out );
398   os << "#! /bin/sh" << std::endl;
399   os << job_params.Command;
400   os.close();
401
402   new_job->setJobFile(cmdFile);
403   new_job->setLocalDirectory(job_params.RefDirectory);
404   new_job->setWorkDirectory(job_params.MachinesList[clusterName].WorkDirectory);
405   new_job->setEnvFile(job_params.MachinesList[clusterName].EnvFile);
406
407   for(int i=0; i < job_params.InputFile.size(); i++)
408     new_job->add_in_file(job_params.InputFile[i]);
409   for(int i=0; i < job_params.OutputFile.size();i++)
410     new_job->add_out_file(job_params.OutputFile[i]);
411
412   resourceParams p;
413   p.hostname = clusterName;
414   p.name = "";
415   p.OS = "";
416   p.nb_proc = job_params.NbOfProcesses;
417   p.nb_node = 0;
418   p.nb_proc_per_node = 0;
419   p.cpu_clock = 0;
420   p.mem_mb = 0;
421   new_job->setResourceRequiredParams(p);
422
423   createJob(new_job);
424   return new_job->getNumber();
425 }
426
427 //=============================================================================
428 /*!
429  *  Factory to instanciate the good batch manager for choosen cluster.
430  */ 
431 //=============================================================================
432 Batch::BatchManager *
433 Launcher_cpp::FactoryBatchManager(ParserResourcesType& params)
434 {
435   std::string mpi;
436   Batch::CommunicationProtocolType protocol;
437   Batch::FactBatchManager * fact;
438
439   std::string hostname = params.HostName;
440
441   switch(params.Protocol)
442   {
443     case sh:
444       protocol = Batch::SH;
445       break;
446     case rsh:
447       protocol = Batch::RSH;
448       break;
449     case ssh:
450       protocol = Batch::SSH;
451       break;
452     default:
453       throw LauncherException("Unknown protocol for this resource");
454       break;
455   }
456
457   switch(params.mpi)
458   {
459     case lam:
460       mpi = "lam";
461       break;
462     case mpich1:
463       mpi = "mpich1";
464       break;
465     case mpich2:
466       mpi = "mpich2";
467       break;
468     case openmpi:
469       mpi = "openmpi";
470       break;
471     case ompi:
472       mpi = "ompi";
473       break;
474     case slurmmpi:
475       mpi = "slurmmpi";
476       break;
477     case prun:
478       mpi = "prun";
479       break;
480     default:
481       mpi = "nompi";
482   }
483
484   const char * bmType;
485   switch( params.Batch )
486   {
487     case pbs:
488       bmType = "PBS";
489       break;
490     case lsf:
491       bmType = "LSF";
492       break;
493     case sge:
494       bmType = "SGE";
495       break;
496     case ccc:
497       bmType = "CCC";
498       break;
499     case slurm:
500       bmType = "SLURM";
501       break;
502     case none:
503       bmType = "LOCAL";
504       break;
505     case ll:
506       bmType = "LL";
507       break;
508     case vishnu:
509       bmType = "VISHNU";
510       break;
511     case oar:
512       bmType = "OAR";
513       break;
514     case coorm:
515       bmType = "COORM";
516       break;
517     default:
518       LAUNCHER_MESSAGE("Bad batch description of the resource: Batch = " << params.Batch);
519       throw LauncherException("No batchmanager for that cluster - Bad batch description of the resource");
520   }
521   Batch::BatchManagerCatalog & cata = Batch::BatchManagerCatalog::getInstance();
522   fact = dynamic_cast<Batch::FactBatchManager*>(cata(bmType));
523   if (fact == NULL) {
524     LAUNCHER_MESSAGE("Cannot find batch manager factory for " << bmType << ". Check your version of libBatch.");
525     throw LauncherException("Cannot find batch manager factory");
526   }
527   LAUNCHER_MESSAGE("Instanciation of batch manager of type: " << bmType);
528   Batch::BatchManager * batch_client = (*fact)(hostname.c_str(), params.UserName.c_str(),
529                                                protocol, mpi.c_str());
530   return batch_client;
531 }
532
533 //----------------------------------------------------------
534 // Without LIBBATCH - Launcher_cpp do nothing...
535 //----------------------------------------------------------
536 #else
537
538 void 
539 Launcher_cpp::createJob(Launcher::Job * new_job)
540 {
541   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot create a job !!!");
542   delete new_job;
543   throw LauncherException("Method Launcher_cpp::createJob is not available "
544                           "(libBatch was not present at compilation time)");
545 }
546
547 void 
548 Launcher_cpp::launchJob(int job_id)
549 {
550   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot launch a job !!!");
551   throw LauncherException("Method Launcher_cpp::launchJob is not available "
552                           "(libBatch was not present at compilation time)");
553 }
554
555 const char *
556 Launcher_cpp::getJobState(int job_id)
557 {
558   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job state!!!");
559   throw LauncherException("Method Launcher_cpp::getJobState is not available "
560                           "(libBatch was not present at compilation time)");
561 }
562
563 const char *
564 Launcher_cpp::getAssignedHostnames(int job_id)
565 {
566   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job assigned hostnames!!!");
567   throw LauncherException("Method Launcher_cpp::getAssignedHostnames is not available "
568                           "(libBatch was not present at compilation time)");
569 }
570
571 void
572 Launcher_cpp::getJobResults(int job_id, std::string directory)
573 {
574   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job results!!!");
575   throw LauncherException("Method Launcher_cpp::getJobResults is not available "
576                           "(libBatch was not present at compilation time)");
577 }
578
579 bool
580 Launcher_cpp::getJobDumpState(int job_id, std::string directory)
581 {
582   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
583   throw LauncherException("Method Launcher_cpp::getJobDumpState is not available "
584                           "(libBatch was not present at compilation time)");
585 }
586
587 bool
588 Launcher_cpp::getJobWorkFile(int job_id, std::string work_file, std::string directory)
589 {
590   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot get job dump state!!!");
591   throw LauncherException("Method Launcher_cpp::getJobWorkFile is not available "
592                           "(libBatch was not present at compilation time)");
593 }
594
595 void
596 Launcher_cpp::removeJob(int job_id)
597 {
598   LAUNCHER_INFOS("Launcher compiled without LIBBATCH - cannot remove job!!!");
599   throw LauncherException("Method Launcher_cpp::removeJob is not available "
600                           "(libBatch was not present at compilation time)");
601 }
602
603 void
604 Launcher_cpp::stopJob(int job_id)
605 {
606   throw LauncherException("Method Launcher_cpp::stopJob is not available "
607                           "(libBatch was not present at compilation time)");
608 }
609
610 long 
611 Launcher_cpp::createJobWithFile( const std::string xmlExecuteFile, std::string clusterName)
612 {
613   throw LauncherException("Method Launcher_cpp::createJobWithFile is not available "
614                           "(libBatch was not present at compilation time)");
615   return 0;
616 }
617
618 #endif
619
620 ParserLauncherType 
621 Launcher_cpp::ParseXmlFile(std::string xmlExecuteFile)
622 {
623   ParserLauncherType job_params;
624   SALOME_Launcher_Handler * handler = new SALOME_Launcher_Handler(job_params);
625
626   const char* aFilePath = xmlExecuteFile.c_str();
627   FILE* aFile = fopen(aFilePath, "r");
628   if (aFile != NULL)
629   {
630     xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
631     if (aDoc != NULL)
632       handler->ProcessXmlDocument(aDoc);
633     else
634     {
635       std::string message = "ResourcesManager_cpp: could not parse file: " + xmlExecuteFile;
636       LAUNCHER_MESSAGE(message);
637       delete handler;
638       throw LauncherException(message);
639     }
640     // Free the document
641     xmlFreeDoc(aDoc);
642     fclose(aFile);
643   }
644   else
645   {
646     std::string message = "ResourcesManager_cpp: file is not readable: " + xmlExecuteFile;
647     LAUNCHER_MESSAGE(message);
648     delete handler;
649     throw LauncherException(message);
650   }
651
652   // Return
653   delete handler;
654   return job_params;
655 }
656
657 std::map<int, Launcher::Job *>
658 Launcher_cpp::getJobs()
659 {
660   return _launcher_job_map;
661 }
662
663 void 
664 Launcher_cpp::createBatchManagerForJob(Launcher::Job * job)
665 {
666   int job_id = job->getNumber();
667
668   // Select a ressource for the job
669   std::vector<std::string> ResourceList;
670   resourceParams params = job->getResourceRequiredParams();
671   // Consider only resources that can launch batch jobs
672   params.can_launch_batch_jobs = true;
673   try
674   {
675     ResourceList = _ResManager->GetFittingResources(params);
676   }
677   catch(const ResourcesException &ex)
678   {
679     throw LauncherException(ex.msg.c_str());
680   }
681   if (ResourceList.size() == 0)
682   {
683     LAUNCHER_INFOS("No adequate resource found for the job, number " << job->getNumber());
684     job->setState("ERROR");
685     throw LauncherException("No resource found the job");
686   }
687
688   // Configure the job with the resource selected - the first of the list
689   ParserResourcesType resource_definition = _ResManager->GetResourcesDescr(ResourceList[0]);
690
691   // Set resource definition to the job
692   // The job will check if the definitions needed
693   try
694   {
695     job->setResourceDefinition(resource_definition);
696   }
697   catch(const LauncherException &ex)
698   {
699     LAUNCHER_INFOS("Error in the definition of the resource, mess: " << ex.msg);
700     job->setState("ERROR");
701     throw ex;
702   }
703
704   // Step 2: We can now add a Factory if the resource is correctly define
705 #ifdef WITH_LIBBATCH
706   std::map<int, Batch::BatchManager *>::const_iterator it = _batchmap.find(job_id);
707   if(it == _batchmap.end())
708   {
709     try
710     {
711       // Warning cannot write on one line like this, because map object is constructed before
712       // the method is called...
713       //_batchmap[job_id] = FactoryBatchManager(resource_definition);
714       Batch::BatchManager * batch_client = FactoryBatchManager(resource_definition);
715       _batchmap[job_id] = batch_client;
716     }
717     catch(const LauncherException &ex)
718     {
719       LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.msg);
720       throw ex;
721     }
722     catch(const Batch::GenericException &ex)
723     {
724       LAUNCHER_INFOS("Error during creation of the batch manager of the job, mess: " << ex.message);
725       throw LauncherException(ex.message);
726     }
727   }
728 #endif
729 }
730
731 void 
732 Launcher_cpp::addJobDirectlyToMap(Launcher::Job * new_job)
733 {
734   // Step 0: Calculated job_id
735   pthread_mutex_lock(_job_cpt_mutex);
736   int job_id = _job_cpt;
737   _job_cpt++;
738   new_job->setNumber(job_id);
739   pthread_mutex_unlock(_job_cpt_mutex);
740
741   // Step 1: check if resource is already in the map
742   createBatchManagerForJob(new_job);
743
744   // Step 2: add the job to the batch manager
745 #ifdef WITH_LIBBATCH
746   try
747   {
748     Batch::JobId batch_manager_job_id = _batchmap[job_id]->addJob(*(new_job->getBatchJob()),
749                                                                   new_job->getReference());
750     new_job->setBatchManagerJobId(batch_manager_job_id);
751   }
752   catch(const Batch::GenericException &ex)
753   {
754     LAUNCHER_INFOS("Job cannot be added, exception in addJob: " << ex.message);
755     throw LauncherException(ex.message.c_str());
756   }
757
758   // Step 3: add job to launcher map
759   std::map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(new_job->getNumber());
760   if (it_job == _launcher_job_map.end())
761     _launcher_job_map[new_job->getNumber()] = new_job;
762   else
763   {
764     LAUNCHER_INFOS("A job as already the same id: " << new_job->getNumber());
765     delete new_job;
766     throw LauncherException("A job as already the same id - job is not created !");
767   }
768   LAUNCHER_MESSAGE("New job added");
769 #endif
770 }
771
772 list<int>
773 Launcher_cpp::loadJobs(const char* jobs_file)
774 {
775   list<int> new_jobs_id_list;
776
777   // Load the jobs from XML file
778   list<Launcher::Job *> jobs_list = Launcher::XML_Persistence::loadJobs(jobs_file);
779
780   // Create each job in the launcher
781   list<Launcher::Job *>::const_iterator it_job;
782   for (it_job = jobs_list.begin(); it_job != jobs_list.end(); it_job++)
783   {
784     Launcher::Job * new_job = *it_job;
785     string job_state = new_job->getState();
786
787     try
788     {
789       if (job_state == "CREATED")
790       {
791         // In this case, we ignore run_part informations
792         createJob(new_job);
793         new_jobs_id_list.push_back(new_job->getNumber());
794       }
795       else if (job_state == "QUEUED"     ||
796                job_state == "RUNNING"    ||
797                job_state == "IN_PROCESS" ||
798                job_state == "PAUSED")
799       {
800         addJobDirectlyToMap(new_job);
801         new_jobs_id_list.push_back(new_job->getNumber());
802
803         // Step 4: We check that the BatchManager could resume
804         // the job
805 #ifdef WITH_LIBBATCH
806         if (new_job->getBatchManagerJobId().getReference() != new_job->getReference())
807         {
808           LAUNCHER_INFOS("BatchManager type cannot resume a job - job state is set to ERROR");
809           new_job->setState("ERROR");
810         }
811 #endif
812       }
813       else if (job_state == "FINISHED" ||
814                job_state == "FAILED"   ||
815                job_state == "ERROR")
816       {
817         // Step 2: We add run_part informations
818         addJobDirectlyToMap(new_job);
819         new_jobs_id_list.push_back(new_job->getNumber());
820       }
821       else
822       {
823         LAUNCHER_INFOS("A bad job is found, state unknown " << job_state);
824         delete new_job;
825       }
826     }
827     catch(const LauncherException &ex)
828     {
829       LAUNCHER_INFOS("Cannot load the job. Exception: " << ex.msg.c_str());
830       delete new_job;
831     }
832   }
833
834   return new_jobs_id_list;
835 }
836
837 void
838 Launcher_cpp::saveJobs(const char* jobs_file)
839 {
840   // Create a sorted list from the internal job map
841   list<const Launcher::Job *> jobs_list;
842   for (int i=0; i<_job_cpt; i++)
843   {
844     map<int, Launcher::Job *>::const_iterator it_job = _launcher_job_map.find(i);
845     if (it_job != _launcher_job_map.end())
846       jobs_list.push_back(it_job->second);
847   }
848
849   // Save the jobs in XML file
850   Launcher::XML_Persistence::saveJobs(jobs_file, jobs_list);
851 }