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::exportInputFiles(CORBA::Long job_id)
166 _l.exportInputFiles(job_id);
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::getJobResults(CORBA::Long job_id, const char * directory)
180 _l.getJobResults(job_id, directory);
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::clearJobWorkingDir(CORBA::Long job_id)
194 _l.clearJobWorkingDir(job_id);
196 catch(const LauncherException &ex)
198 INFOS(ex.msg.c_str());
199 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
204 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
206 CORBA::Boolean rtn = false;
209 rtn = _l.getJobDumpState(job_id, directory);
211 catch(const LauncherException &ex)
213 INFOS(ex.msg.c_str());
214 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
220 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
222 CORBA::Boolean rtn = false;
225 rtn = _l.getJobWorkFile(job_id, work_file, directory);
227 catch(const LauncherException &ex)
229 INFOS(ex.msg.c_str());
230 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
236 SALOME_Launcher::removeJob(CORBA::Long job_id)
240 _l.removeJob(job_id);
241 std::ostringstream job_id_str;
242 job_id_str << job_id;
243 notifyObservers("REMOVE_JOB", job_id_str.str());
245 catch(const LauncherException &ex)
247 INFOS(ex.msg.c_str());
248 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
253 SALOME_Launcher::stopJob(CORBA::Long job_id)
258 std::ostringstream job_id_str;
259 job_id_str << job_id;
260 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
262 catch(const LauncherException &ex)
264 INFOS(ex.msg.c_str());
265 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
270 SALOME_Launcher::dumpJob(CORBA::Long job_id)
275 result = _l.dumpJob(job_id);
277 catch(const LauncherException &ex)
279 INFOS(ex.msg.c_str());
280 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
282 return CORBA::string_dup(result.c_str());
286 SALOME_Launcher::restoreJob(const char * dumpedJob)
290 jobId = _l.restoreJob(dumpedJob);
293 std::ostringstream job_str;
295 notifyObservers("NEW_JOB", job_str.str());
298 catch(const LauncherException &ex){
299 INFOS(ex.msg.c_str());
300 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
306 //=============================================================================
308 * Create a job in the launcher with a file
309 * \param xmlExecuteFile : .xml to parse that contains job description
310 * \param clusterName : machine chosen
312 //=============================================================================
314 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
315 const char * clusterName)
319 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
321 catch(const LauncherException &ex){
322 INFOS(ex.msg.c_str());
323 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
329 //=============================================================================
331 * the test batch configuration
332 * \param params : The batch cluster
334 //=============================================================================
336 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
338 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
339 CORBA::Boolean rtn = false;
342 // Consider only resources that can run batch jobs
343 Engines::ResourceParameters new_params(params);
344 new_params.can_launch_batch_jobs = true;
346 // find a resource matching the required parameters
347 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
348 if (aMachineList->length() == 0)
349 throw SALOME_Exception("No resources have been found with your parameters");
351 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
352 std::string resource_name(p->name);
353 INFOS("Choose resource for test: " << resource_name);
361 catch(const LauncherException &ex){
362 INFOS(ex.msg.c_str());
363 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
368 //=============================================================================
370 * shutdown all the containers, then the ContainerManager servant
372 //=============================================================================
373 void SALOME_Launcher::Shutdown()
376 _NS->Destroy_Name(_LauncherNameInNS);
377 _ContManager->Shutdown();
378 _ResManager->Shutdown();
379 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
380 _poa->deactivate_object(oid);
381 if(!CORBA::is_nil(_orb))
385 //=============================================================================
387 * Returns the PID of the process
389 //=============================================================================
390 CORBA::Long SALOME_Launcher::getPID()
394 (CORBA::Long)getpid();
396 (CORBA::Long)_getpid();
400 //=============================================================================
402 * Returns current launcher jobs list
404 //=============================================================================
406 SALOME_Launcher::getJobsList()
408 Engines::JobsList_var jobs_list = new Engines::JobsList();
409 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
410 std::map<int, Launcher::Job *>::const_iterator it_job;
412 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
414 int number = it_job->first;
417 // Prepare CORBA job description
418 Engines::JobDescription_var job_descr = new Engines::JobDescription();
419 Engines::JobParameters_var job_parameters = getJobParameters(number);
420 job_descr->job_id = number;
421 job_descr->job_parameters = job_parameters;
423 // Add job description to the sequence
424 jobs_list->length(list_id + 1);
425 jobs_list[list_id] = job_descr;
430 return jobs_list._retn();
433 //=============================================================================
435 * Returns the job description
437 //=============================================================================
438 Engines::JobParameters *
439 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
441 Engines::JobParameters_var job_parameters;
444 JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
445 job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
447 catch(const LauncherException &ex)
449 INFOS(ex.msg.c_str());
450 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
453 return job_parameters._retn();
456 //=============================================================================
458 * Loads jobs saved in jobs_file
460 //=============================================================================
462 SALOME_Launcher::loadJobs(const char* jobs_file)
464 list<int> new_jobs_id_list;
467 // Load the jobs in Launcher
468 new_jobs_id_list = _l.loadJobs(jobs_file);
470 catch (const LauncherException & ex)
472 INFOS(ex.msg.c_str());
473 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
476 // Notify observers of the new jobs
477 list<int>::const_iterator it_jobs_id;
478 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
480 ostringstream job_id_sstr;
481 job_id_sstr << *it_jobs_id;
482 notifyObservers("NEW_JOB", job_id_sstr.str());
484 notifyObservers("LOAD_JOBS", jobs_file);
487 //=============================================================================
489 * Save jobs of Launcher (in any steps) in file jobs_file
491 //=============================================================================
493 SALOME_Launcher::saveJobs(const char* jobs_file)
495 _l.saveJobs(jobs_file);
496 notifyObservers("SAVE_JOBS", jobs_file);
499 //=============================================================================
501 * Add a new observer to the launcher
503 //=============================================================================
505 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
507 bool new_observer = true;
508 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
509 while(iter != _observers.end())
511 if (std::string(_orb->object_to_string(*iter)) ==
512 std::string(_orb->object_to_string(observer)))
514 new_observer = false;
520 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
522 // We notify the new observer with all jobs that are currently in the Launcher
523 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
524 std::map<int, Launcher::Job *>::const_iterator it_job;
525 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
527 int number = it_job->first;
528 std::ostringstream job_id;
532 observer->notify("NEW_JOB", job_id.str().c_str());
536 MESSAGE("Notify Observer, exception catch");
542 //=============================================================================
544 * Add a new observer to the launcher
546 //=============================================================================
548 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
550 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
551 while(iter != _observers.end())
553 if (std::string(_orb->object_to_string(*iter)) ==
554 std::string(_orb->object_to_string(observer)))
557 iter =_observers.erase(iter++);
566 //=============================================================================
568 * Notify observers on a new event
570 //=============================================================================
572 SALOME_Launcher::notifyObservers(const std::string & event_name,
573 const std::string & event_data)
575 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
576 while(iter != _observers.end())
580 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
581 CORBA::string_dup(event_data.c_str()));
585 MESSAGE("Notify Observer, exception catch");
593 SALOME_Launcher::JobParameters_CORBA2CPP(
594 const Engines::JobParameters& job_parameters)
596 JobParameters_cpp result;
598 result.job_name = job_parameters.job_name.in();
599 result.job_type = job_parameters.job_type.in();
600 result.job_file = job_parameters.job_file.in();
601 result.pre_command = job_parameters.pre_command.in();
602 result.env_file = job_parameters.env_file.in();
604 result.in_files.clear();
605 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
606 result.in_files.push_back(job_parameters.in_files[i].in());
607 result.out_files.clear();
608 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
609 result.out_files.push_back(job_parameters.out_files[i].in());
611 result.work_directory = job_parameters.work_directory.in();
612 result.local_directory = job_parameters.local_directory.in();
613 result.result_directory = job_parameters.result_directory.in();
614 result.maximum_duration = job_parameters.maximum_duration.in();
616 result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
618 result.queue = job_parameters.queue.in();
619 result.partition = job_parameters.partition.in();
620 result.exclusive = job_parameters.exclusive;
621 result.mem_per_cpu = job_parameters.mem_per_cpu;
622 result.wckey = job_parameters.wckey.in();
623 result.extra_params = job_parameters.extra_params.in();
625 result.specific_parameters.clear();
626 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
627 result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
628 = job_parameters.specific_parameters[i].value.in();
630 result.launcher_file = job_parameters.launcher_file.in();
631 result.launcher_args = job_parameters.launcher_args.in();
635 Engines::JobParameters_var
636 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
638 Engines::JobParameters_var result = new Engines::JobParameters;
639 result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
640 result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
641 result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
642 result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
643 result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
644 result->in_files.length(job_parameters.in_files.size());
647 for(const std::string& it : job_parameters.in_files)
649 result->in_files[i] = CORBA::string_dup(it.c_str());
652 result->out_files.length(job_parameters.out_files.size());
654 for(const std::string& it : job_parameters.out_files)
656 result->out_files[i] = CORBA::string_dup(it.c_str());
660 result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
661 result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
662 result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
663 result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
665 result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
667 result->queue = CORBA::string_dup(job_parameters.queue.c_str());
668 result->partition = CORBA::string_dup(job_parameters.partition.c_str());
669 result->exclusive = job_parameters.exclusive;
670 result->mem_per_cpu = job_parameters.mem_per_cpu;
671 result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
672 result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
674 const std::map<std::string, std::string>& specific_parameters
675 = job_parameters.specific_parameters;
676 if (!specific_parameters.empty())
678 result->specific_parameters.length(specific_parameters.size());
680 for (const auto& it_specific : specific_parameters)
682 Engines::Parameter_var new_param = new Engines::Parameter;
683 new_param->name = CORBA::string_dup(it_specific.first.c_str());
684 new_param->value = CORBA::string_dup(it_specific.second.c_str());
685 result->specific_parameters[i] = new_param;
690 result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
691 result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());