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
23 #include "SALOME_Launcher.hxx"
24 #include "BatchTest.hxx"
26 #include "SALOME_ContainerManager.hxx"
27 #include "SALOME_NamingService.hxx"
28 #include "SALOME_ResourcesManager.hxx"
29 #include "Utils_CorbaException.hxx"
32 #include "Launcher_Job_Command.hxx"
33 #include "Launcher_Job_YACSFile.hxx"
34 #include "Launcher_Job_PythonSALOME.hxx"
35 #include "Launcher_Job_CommandSALOME.hxx"
37 #include "utilities.h"
44 #include <sys/types.h>
53 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
55 //=============================================================================
60 //=============================================================================
61 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
63 MESSAGE("SALOME_Launcher constructor");
64 _NS = new SALOME_NamingService(orb);
65 _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
66 _l.SetResourcesManager(_ResManager->GetImpl());
67 _ContManager = new SALOME_ContainerManager(orb,poa,_NS);
68 _ResManager->_remove_ref();
69 _ContManager->_remove_ref();
71 _orb = CORBA::ORB::_duplicate(orb) ;
72 _poa = PortableServer::POA::_duplicate(poa) ;
73 PortableServer::ObjectId_var id = _poa->activate_object(this);
74 CORBA::Object_var obj = _poa->id_to_reference(id);
75 Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
77 _NS->Register(refContMan,_LauncherNameInNS);
78 MESSAGE("SALOME_Launcher constructor end");
81 //=============================================================================
85 //=============================================================================
86 SALOME_Launcher::~SALOME_Launcher()
88 MESSAGE("SALOME_Launcher destructor");
90 MESSAGE("SALOME_Launcher destructor end");
95 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
97 JobParameters_cpp cpp_parameters = JobParameters_CORBA2CPP(job_parameters);
98 CORBA::Long jobNumber = -1;
101 jobNumber = _l.createJob(cpp_parameters);
102 std::ostringstream job_id;
104 notifyObservers("NEW_JOB", job_id.str());
106 catch(const LauncherException &ex)
108 INFOS(ex.msg.c_str());
109 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
115 SALOME_Launcher::launchJob(CORBA::Long job_id)
119 _l.launchJob(job_id);
121 catch(const LauncherException &ex)
123 INFOS(ex.msg.c_str());
124 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
129 SALOME_Launcher::getJobState(CORBA::Long job_id)
134 result = _l.getJobState(job_id);
136 catch(const LauncherException &ex)
138 INFOS(ex.msg.c_str());
139 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
141 return CORBA::string_dup(result.c_str());
144 // Get names or ids of hosts assigned to the job
146 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
151 result = _l.getAssignedHostnames(job_id);
153 catch(const LauncherException &ex)
155 INFOS(ex.msg.c_str());
156 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
158 return CORBA::string_dup(result.c_str());
162 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
166 _l.getJobResults(job_id, directory);
168 catch(const LauncherException &ex)
170 INFOS(ex.msg.c_str());
171 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
176 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
180 _l.clearJobWorkingDir(job_id);
182 catch(const LauncherException &ex)
184 INFOS(ex.msg.c_str());
185 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
190 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
192 CORBA::Boolean rtn = false;
195 rtn = _l.getJobDumpState(job_id, directory);
197 catch(const LauncherException &ex)
199 INFOS(ex.msg.c_str());
200 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
206 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
208 CORBA::Boolean rtn = false;
211 rtn = _l.getJobWorkFile(job_id, work_file, directory);
213 catch(const LauncherException &ex)
215 INFOS(ex.msg.c_str());
216 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
222 SALOME_Launcher::removeJob(CORBA::Long job_id)
226 _l.removeJob(job_id);
227 std::ostringstream job_id_str;
228 job_id_str << job_id;
229 notifyObservers("REMOVE_JOB", job_id_str.str());
231 catch(const LauncherException &ex)
233 INFOS(ex.msg.c_str());
234 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
239 SALOME_Launcher::stopJob(CORBA::Long job_id)
244 std::ostringstream job_id_str;
245 job_id_str << job_id;
246 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
248 catch(const LauncherException &ex)
250 INFOS(ex.msg.c_str());
251 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
256 SALOME_Launcher::dumpJob(CORBA::Long job_id)
261 result = _l.dumpJob(job_id);
263 catch(const LauncherException &ex)
265 INFOS(ex.msg.c_str());
266 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
268 return CORBA::string_dup(result.c_str());
272 SALOME_Launcher::restoreJob(const char * dumpedJob)
276 jobId = _l.restoreJob(dumpedJob);
279 std::ostringstream job_str;
281 notifyObservers("NEW_JOB", job_str.str());
284 catch(const LauncherException &ex){
285 INFOS(ex.msg.c_str());
286 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
292 //=============================================================================
294 * Create a job in the launcher with a file
295 * \param xmlExecuteFile : .xml to parse that contains job description
296 * \param clusterName : machine chosen
298 //=============================================================================
300 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
301 const char * clusterName)
305 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
307 catch(const LauncherException &ex){
308 INFOS(ex.msg.c_str());
309 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
315 //=============================================================================
317 * the test batch configuration
318 * \param params : The batch cluster
320 //=============================================================================
322 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
324 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
325 CORBA::Boolean rtn = false;
328 // Consider only resources that can run batch jobs
329 Engines::ResourceParameters new_params(params);
330 new_params.can_launch_batch_jobs = true;
332 // find a resource matching the required parameters
333 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
334 if (aMachineList->length() == 0)
335 throw SALOME_Exception("No resources have been found with your parameters");
337 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
338 std::string resource_name(p->name);
339 INFOS("Choose resource for test: " << resource_name);
347 catch(const LauncherException &ex){
348 INFOS(ex.msg.c_str());
349 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
354 //=============================================================================
356 * shutdown all the containers, then the ContainerManager servant
358 //=============================================================================
359 void SALOME_Launcher::Shutdown()
362 _NS->Destroy_Name(_LauncherNameInNS);
363 _ContManager->Shutdown();
364 _ResManager->Shutdown();
365 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
366 _poa->deactivate_object(oid);
367 if(!CORBA::is_nil(_orb))
371 //=============================================================================
373 * Returns the PID of the process
375 //=============================================================================
376 CORBA::Long SALOME_Launcher::getPID()
380 (CORBA::Long)getpid();
382 (CORBA::Long)_getpid();
386 //=============================================================================
388 * Returns current launcher jobs list
390 //=============================================================================
392 SALOME_Launcher::getJobsList()
394 Engines::JobsList_var jobs_list = new Engines::JobsList();
395 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
396 std::map<int, Launcher::Job *>::const_iterator it_job;
398 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
400 int number = it_job->first;
403 // Prepare CORBA job description
404 Engines::JobDescription_var job_descr = new Engines::JobDescription();
405 Engines::JobParameters_var job_parameters = getJobParameters(number);
406 job_descr->job_id = number;
407 job_descr->job_parameters = job_parameters;
409 // Add job description to the sequence
410 jobs_list->length(list_id + 1);
411 jobs_list[list_id] = job_descr;
416 return jobs_list._retn();
419 //=============================================================================
421 * Returns the job description
423 //=============================================================================
424 Engines::JobParameters *
425 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
427 Engines::JobParameters_var job_parameters;
430 JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
431 job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
433 catch(const LauncherException &ex)
435 INFOS(ex.msg.c_str());
436 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
439 return job_parameters._retn();
442 //=============================================================================
444 * Loads jobs saved in jobs_file
446 //=============================================================================
448 SALOME_Launcher::loadJobs(const char* jobs_file)
450 list<int> new_jobs_id_list;
453 // Load the jobs in Launcher
454 new_jobs_id_list = _l.loadJobs(jobs_file);
456 catch (const LauncherException & ex)
458 INFOS(ex.msg.c_str());
459 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
462 // Notify observers of the new jobs
463 list<int>::const_iterator it_jobs_id;
464 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
466 ostringstream job_id_sstr;
467 job_id_sstr << *it_jobs_id;
468 notifyObservers("NEW_JOB", job_id_sstr.str());
470 notifyObservers("LOAD_JOBS", jobs_file);
473 //=============================================================================
475 * Save jobs of Launcher (in any steps) in file jobs_file
477 //=============================================================================
479 SALOME_Launcher::saveJobs(const char* jobs_file)
481 _l.saveJobs(jobs_file);
482 notifyObservers("SAVE_JOBS", jobs_file);
485 //=============================================================================
487 * Add a new observer to the launcher
489 //=============================================================================
491 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
493 bool new_observer = true;
494 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
495 while(iter != _observers.end())
497 if (std::string(_orb->object_to_string(*iter)) ==
498 std::string(_orb->object_to_string(observer)))
500 new_observer = false;
506 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
508 // We notify the new observer with all jobs that are currently in the Launcher
509 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
510 std::map<int, Launcher::Job *>::const_iterator it_job;
511 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
513 int number = it_job->first;
514 std::ostringstream job_id;
518 observer->notify("NEW_JOB", job_id.str().c_str());
522 MESSAGE("Notify Observer, exception catch");
528 //=============================================================================
530 * Add a new observer to the launcher
532 //=============================================================================
534 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
536 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
537 while(iter != _observers.end())
539 if (std::string(_orb->object_to_string(*iter)) ==
540 std::string(_orb->object_to_string(observer)))
543 iter =_observers.erase(iter++);
552 //=============================================================================
554 * Notify observers on a new event
556 //=============================================================================
558 SALOME_Launcher::notifyObservers(const std::string & event_name,
559 const std::string & event_data)
561 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
562 while(iter != _observers.end())
566 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
567 CORBA::string_dup(event_data.c_str()));
571 MESSAGE("Notify Observer, exception catch");
579 SALOME_Launcher::JobParameters_CORBA2CPP(
580 const Engines::JobParameters& job_parameters)
582 JobParameters_cpp result;
584 result.job_name = job_parameters.job_name.in();
585 result.job_type = job_parameters.job_type.in();
586 result.job_file = job_parameters.job_file.in();
587 result.pre_command = job_parameters.pre_command.in();
588 result.env_file = job_parameters.env_file.in();
590 result.in_files.clear();
591 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
592 result.in_files.push_back(job_parameters.in_files[i].in());
593 result.out_files.clear();
594 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
595 result.out_files.push_back(job_parameters.out_files[i].in());
597 result.work_directory = job_parameters.work_directory.in();
598 result.local_directory = job_parameters.local_directory.in();
599 result.result_directory = job_parameters.result_directory.in();
600 result.maximum_duration = job_parameters.maximum_duration.in();
602 result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
604 result.queue = job_parameters.queue.in();
605 result.partition = job_parameters.partition.in();
606 result.exclusive = job_parameters.exclusive;
607 result.mem_per_cpu = job_parameters.mem_per_cpu;
608 result.wckey = job_parameters.wckey.in();
609 result.extra_params = job_parameters.extra_params.in();
611 result.specific_parameters.clear();
612 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
613 result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
614 = job_parameters.specific_parameters[i].value.in();
616 result.launcher_file = job_parameters.launcher_file.in();
617 result.launcher_args = job_parameters.launcher_args.in();
621 Engines::JobParameters_var
622 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
624 Engines::JobParameters_var result = new Engines::JobParameters;
625 result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
626 result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
627 result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
628 result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
629 result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
630 result->in_files.length(job_parameters.in_files.size());
633 for(const std::string& it : job_parameters.in_files)
635 result->in_files[i] = CORBA::string_dup(it.c_str());
638 result->out_files.length(job_parameters.out_files.size());
640 for(const std::string& it : job_parameters.out_files)
642 result->out_files[i] = CORBA::string_dup(it.c_str());
646 result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
647 result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
648 result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
649 result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
651 result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
653 result->queue = CORBA::string_dup(job_parameters.queue.c_str());
654 result->partition = CORBA::string_dup(job_parameters.partition.c_str());
655 result->exclusive = job_parameters.exclusive;
656 result->mem_per_cpu = job_parameters.mem_per_cpu;
657 result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
658 result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
660 const std::map<std::string, std::string>& specific_parameters
661 = job_parameters.specific_parameters;
662 if (!specific_parameters.empty())
664 result->specific_parameters.length(specific_parameters.size());
666 for (const auto& it_specific : specific_parameters)
668 Engines::Parameter_var new_param = new Engines::Parameter;
669 new_param->name = CORBA::string_dup(it_specific.first.c_str());
670 new_param->value = CORBA::string_dup(it_specific.second.c_str());
671 result->specific_parameters[i] = new_param;
676 result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
677 result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());