1 // Copyright (C) 2007-2021 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)
63 MESSAGE("SALOME_Launcher constructor");
64 _NS = new SALOME_NamingService(orb);
66 MESSAGE("SALOME_Launcher constructor end");
69 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *externalNS):_NS(externalNS)
74 void SALOME_Launcher::init(CORBA::ORB_ptr orb, PortableServer::POA_var poa)
76 _ResManager = new SALOME_ResourcesManager(orb,poa,tradNS());
77 _l.SetResourcesManager(_ResManager->GetImpl());
78 _ContManager = new SALOME_ContainerManager(orb,poa,tradNS());
79 _ResManager->_remove_ref();
80 _ContManager->_remove_ref();
82 _orb = CORBA::ORB::_duplicate(orb) ;
83 _poa = PortableServer::POA::_duplicate(poa) ;
84 PortableServer::ObjectId_var id = _poa->activate_object(this);
85 CORBA::Object_var obj = _poa->id_to_reference(id);
86 Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
87 _NS->Register(refContMan,_LauncherNameInNS);
90 //=============================================================================
94 //=============================================================================
95 SALOME_Launcher::~SALOME_Launcher()
97 MESSAGE("SALOME_Launcher destructor");
99 MESSAGE("SALOME_Launcher destructor end");
104 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
106 JobParameters_cpp cpp_parameters = JobParameters_CORBA2CPP(job_parameters);
107 CORBA::Long jobNumber = -1;
110 jobNumber = _l.createJob(cpp_parameters);
111 std::ostringstream job_id;
113 notifyObservers("NEW_JOB", job_id.str());
115 catch(const LauncherException &ex)
117 INFOS(ex.msg.c_str());
118 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
124 SALOME_Launcher::launchJob(CORBA::Long job_id)
128 _l.launchJob(job_id);
130 catch(const LauncherException &ex)
132 INFOS(ex.msg.c_str());
133 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
138 SALOME_Launcher::getJobState(CORBA::Long job_id)
143 result = _l.getJobState(job_id);
145 catch(const LauncherException &ex)
147 INFOS(ex.msg.c_str());
148 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
150 return CORBA::string_dup(result.c_str());
153 // Get names or ids of hosts assigned to the job
155 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
160 result = _l.getAssignedHostnames(job_id);
162 catch(const LauncherException &ex)
164 INFOS(ex.msg.c_str());
165 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
167 return CORBA::string_dup(result.c_str());
171 SALOME_Launcher::exportInputFiles(CORBA::Long job_id)
175 _l.exportInputFiles(job_id);
177 catch(const LauncherException &ex)
179 INFOS(ex.msg.c_str());
180 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
185 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
189 _l.getJobResults(job_id, directory);
191 catch(const LauncherException &ex)
193 INFOS(ex.msg.c_str());
194 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
199 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
203 _l.clearJobWorkingDir(job_id);
205 catch(const LauncherException &ex)
207 INFOS(ex.msg.c_str());
208 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
213 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
215 CORBA::Boolean rtn = false;
218 rtn = _l.getJobDumpState(job_id, directory);
220 catch(const LauncherException &ex)
222 INFOS(ex.msg.c_str());
223 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
229 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
231 CORBA::Boolean rtn = false;
234 rtn = _l.getJobWorkFile(job_id, work_file, directory);
236 catch(const LauncherException &ex)
238 INFOS(ex.msg.c_str());
239 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
245 SALOME_Launcher::removeJob(CORBA::Long job_id)
249 _l.removeJob(job_id);
250 std::ostringstream job_id_str;
251 job_id_str << job_id;
252 notifyObservers("REMOVE_JOB", job_id_str.str());
254 catch(const LauncherException &ex)
256 INFOS(ex.msg.c_str());
257 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
262 SALOME_Launcher::stopJob(CORBA::Long job_id)
267 std::ostringstream job_id_str;
268 job_id_str << job_id;
269 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
271 catch(const LauncherException &ex)
273 INFOS(ex.msg.c_str());
274 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
279 SALOME_Launcher::dumpJob(CORBA::Long job_id)
284 result = _l.dumpJob(job_id);
286 catch(const LauncherException &ex)
288 INFOS(ex.msg.c_str());
289 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
291 return CORBA::string_dup(result.c_str());
295 SALOME_Launcher::restoreJob(const char * dumpedJob)
299 jobId = _l.restoreJob(dumpedJob);
302 std::ostringstream job_str;
304 notifyObservers("NEW_JOB", job_str.str());
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 * Create a job in the launcher with a file
318 * \param xmlExecuteFile : .xml to parse that contains job description
319 * \param clusterName : machine chosen
321 //=============================================================================
323 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
324 const char * clusterName)
328 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
330 catch(const LauncherException &ex){
331 INFOS(ex.msg.c_str());
332 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
338 //=============================================================================
340 * the test batch configuration
341 * \param params : The batch cluster
343 //=============================================================================
345 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
347 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
348 CORBA::Boolean rtn = false;
351 // Consider only resources that can run batch jobs
352 Engines::ResourceParameters new_params(params);
353 new_params.can_launch_batch_jobs = true;
355 // find a resource matching the required parameters
356 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
357 if (aMachineList->length() == 0)
358 throw SALOME_Exception("No resources have been found with your parameters");
360 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
361 std::string resource_name(p->name);
362 INFOS("Choose resource for test: " << resource_name);
370 catch(const LauncherException &ex){
371 INFOS(ex.msg.c_str());
372 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
377 //=============================================================================
379 * shutdown all the containers, then the ContainerManager servant
381 //=============================================================================
382 void SALOME_Launcher::Shutdown()
387 _NS->Destroy_Name(_LauncherNameInNS);
388 _ContManager->Shutdown();
389 _ResManager->Shutdown();
390 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
391 _poa->deactivate_object(oid);
392 if(!CORBA::is_nil(_orb))
396 //=============================================================================
398 * Returns the PID of the process
400 //=============================================================================
401 CORBA::Long SALOME_Launcher::getPID()
405 (CORBA::Long)getpid();
407 (CORBA::Long)_getpid();
411 //=============================================================================
413 * Returns current launcher jobs list
415 //=============================================================================
417 SALOME_Launcher::getJobsList()
419 Engines::JobsList_var jobs_list = new Engines::JobsList();
420 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
421 std::map<int, Launcher::Job *>::const_iterator it_job;
423 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
425 int number = it_job->first;
428 // Prepare CORBA job description
429 Engines::JobDescription_var job_descr = new Engines::JobDescription();
430 Engines::JobParameters_var job_parameters = getJobParameters(number);
431 job_descr->job_id = number;
432 job_descr->job_parameters = job_parameters;
434 // Add job description to the sequence
435 jobs_list->length(list_id + 1);
436 jobs_list[list_id] = job_descr;
441 return jobs_list._retn();
444 //=============================================================================
446 * Returns the job description
448 //=============================================================================
449 Engines::JobParameters *
450 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
452 Engines::JobParameters_var job_parameters;
455 JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
456 job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
458 catch(const LauncherException &ex)
460 INFOS(ex.msg.c_str());
461 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
464 return job_parameters._retn();
467 //=============================================================================
469 * Loads jobs saved in jobs_file
471 //=============================================================================
473 SALOME_Launcher::loadJobs(const char* jobs_file)
475 list<int> new_jobs_id_list;
478 // Load the jobs in Launcher
479 new_jobs_id_list = _l.loadJobs(jobs_file);
481 catch (const LauncherException & ex)
483 INFOS(ex.msg.c_str());
484 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
487 // Notify observers of the new jobs
488 list<int>::const_iterator it_jobs_id;
489 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
491 ostringstream job_id_sstr;
492 job_id_sstr << *it_jobs_id;
493 notifyObservers("NEW_JOB", job_id_sstr.str());
495 notifyObservers("LOAD_JOBS", jobs_file);
498 //=============================================================================
500 * Save jobs of Launcher (in any steps) in file jobs_file
502 //=============================================================================
504 SALOME_Launcher::saveJobs(const char* jobs_file)
506 _l.saveJobs(jobs_file);
507 notifyObservers("SAVE_JOBS", jobs_file);
510 //=============================================================================
512 * Add a new observer to the launcher
514 //=============================================================================
516 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
518 bool new_observer = true;
519 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
520 while(iter != _observers.end())
522 if (std::string(_orb->object_to_string(*iter)) ==
523 std::string(_orb->object_to_string(observer)))
525 new_observer = false;
531 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
533 // We notify the new observer with all jobs that are currently in the Launcher
534 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
535 std::map<int, Launcher::Job *>::const_iterator it_job;
536 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
538 int number = it_job->first;
539 std::ostringstream job_id;
543 observer->notify("NEW_JOB", job_id.str().c_str());
547 MESSAGE("Notify Observer, exception catch");
553 //=============================================================================
555 * Add a new observer to the launcher
557 //=============================================================================
559 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
561 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
562 while(iter != _observers.end())
564 if (std::string(_orb->object_to_string(*iter)) ==
565 std::string(_orb->object_to_string(observer)))
568 iter =_observers.erase(iter++);
577 //=============================================================================
579 * Notify observers on a new event
581 //=============================================================================
583 SALOME_Launcher::notifyObservers(const std::string & event_name,
584 const std::string & event_data)
586 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
587 while(iter != _observers.end())
591 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
592 CORBA::string_dup(event_data.c_str()));
596 MESSAGE("Notify Observer, exception catch");
604 SALOME_Launcher::JobParameters_CORBA2CPP(
605 const Engines::JobParameters& job_parameters)
607 JobParameters_cpp result;
609 result.job_name = job_parameters.job_name.in();
610 result.job_type = job_parameters.job_type.in();
611 result.job_file = job_parameters.job_file.in();
612 result.pre_command = job_parameters.pre_command.in();
613 result.env_file = job_parameters.env_file.in();
615 result.in_files.clear();
616 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
617 result.in_files.push_back(job_parameters.in_files[i].in());
618 result.out_files.clear();
619 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
620 result.out_files.push_back(job_parameters.out_files[i].in());
622 result.work_directory = job_parameters.work_directory.in();
623 result.local_directory = job_parameters.local_directory.in();
624 result.result_directory = job_parameters.result_directory.in();
625 result.maximum_duration = job_parameters.maximum_duration.in();
627 result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
629 result.queue = job_parameters.queue.in();
630 result.partition = job_parameters.partition.in();
631 result.exclusive = job_parameters.exclusive;
632 result.mem_per_cpu = job_parameters.mem_per_cpu;
633 result.wckey = job_parameters.wckey.in();
634 result.extra_params = job_parameters.extra_params.in();
636 result.specific_parameters.clear();
637 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
638 result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
639 = job_parameters.specific_parameters[i].value.in();
641 result.launcher_file = job_parameters.launcher_file.in();
642 result.launcher_args = job_parameters.launcher_args.in();
646 Engines::JobParameters_var
647 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
649 Engines::JobParameters_var result = new Engines::JobParameters;
650 result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
651 result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
652 result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
653 result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
654 result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
655 result->in_files.length((CORBA::ULong)job_parameters.in_files.size()); //!< TODO: conversion from size_t to CORBA::ULong
658 for(const std::string& it : job_parameters.in_files)
660 result->in_files[i] = CORBA::string_dup(it.c_str());
663 result->out_files.length((CORBA::ULong)job_parameters.out_files.size()); //!< TODO: conversion from size_t to CORBA::ULong
665 for(const std::string& it : job_parameters.out_files)
667 result->out_files[i] = CORBA::string_dup(it.c_str());
671 result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
672 result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
673 result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
674 result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
676 result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
678 result->queue = CORBA::string_dup(job_parameters.queue.c_str());
679 result->partition = CORBA::string_dup(job_parameters.partition.c_str());
680 result->exclusive = job_parameters.exclusive;
681 result->mem_per_cpu = job_parameters.mem_per_cpu;
682 result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
683 result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
685 const std::map<std::string, std::string>& specific_parameters
686 = job_parameters.specific_parameters;
687 if (!specific_parameters.empty())
689 result->specific_parameters.length((CORBA::ULong)specific_parameters.size()); //!< TODO: conversion from size_t to CORBA::ULong
691 for (const auto& it_specific : specific_parameters)
693 Engines::Parameter_var new_param = new Engines::Parameter;
694 new_param->name = CORBA::string_dup(it_specific.first.c_str());
695 new_param->value = CORBA::string_dup(it_specific.second.c_str());
696 result->specific_parameters[i] = new_param;
701 result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
702 result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());
706 static SALOME_Launcher *_launcher_singleton_ssl = nullptr;
708 #include "SALOME_Fake_NamingService.hxx"
710 SALOME_Launcher *KERNEL::getLauncherSA()
712 if(!_launcher_singleton_ssl)
715 CORBA::ORB_var orb = CORBA::ORB_init(argc,nullptr);
716 PortableServer::POA_var root_poa=PortableServer::POA::_the_root_poa();
717 SALOME_Fake_NamingService *ns=new SALOME_Fake_NamingService(orb);
718 _launcher_singleton_ssl = new SALOME_Launcher(orb,root_poa,ns);//3rd arg is important to skip NS !
720 return _launcher_singleton_ssl;