1 // Copyright (C) 2007-2023 CEA, EDF, 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,_NS);
77 _l.SetResourcesManager(_ResManager->GetImpl());
78 _ContManager = new SALOME_ContainerManager(orb,poa,_NS);
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);
244 void SALOME_Launcher::DeclareUsingSalomeSession()
246 this->_ContManager->DeclareUsingSalomeSession();
250 SALOME_Launcher::removeJob(CORBA::Long job_id)
254 _l.removeJob(job_id);
255 std::ostringstream job_id_str;
256 job_id_str << job_id;
257 notifyObservers("REMOVE_JOB", job_id_str.str());
259 catch(const LauncherException &ex)
261 INFOS(ex.msg.c_str());
262 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
267 SALOME_Launcher::stopJob(CORBA::Long job_id)
272 std::ostringstream job_id_str;
273 job_id_str << job_id;
274 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
276 catch(const LauncherException &ex)
278 INFOS(ex.msg.c_str());
279 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
284 SALOME_Launcher::dumpJob(CORBA::Long job_id)
289 result = _l.dumpJob(job_id);
291 catch(const LauncherException &ex)
293 INFOS(ex.msg.c_str());
294 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
296 return CORBA::string_dup(result.c_str());
300 SALOME_Launcher::restoreJob(const char * dumpedJob)
304 jobId = _l.restoreJob(dumpedJob);
307 std::ostringstream job_str;
309 notifyObservers("NEW_JOB", job_str.str());
312 catch(const LauncherException &ex){
313 INFOS(ex.msg.c_str());
314 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
320 //=============================================================================
322 * Create a job in the launcher with a file
323 * \param xmlExecuteFile : .xml to parse that contains job description
324 * \param clusterName : machine chosen
326 //=============================================================================
328 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
329 const char * clusterName)
333 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
335 catch(const LauncherException &ex){
336 INFOS(ex.msg.c_str());
337 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
343 //=============================================================================
345 * the test batch configuration
346 * \param params : The batch cluster
348 //=============================================================================
350 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
352 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
353 CORBA::Boolean rtn = false;
356 // Consider only resources that can run batch jobs
357 Engines::ResourceParameters new_params(params);
358 new_params.can_launch_batch_jobs = true;
360 // find a resource matching the required parameters
361 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
362 if (aMachineList->length() == 0)
363 throw SALOME_Exception("No resources have been found with your parameters");
365 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
366 std::string resource_name(p->name);
367 INFOS("Choose resource for test: " << resource_name);
375 catch(const LauncherException &ex){
376 INFOS(ex.msg.c_str());
377 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
382 //=============================================================================
384 * shutdown all the containers, then the ContainerManager servant
386 //=============================================================================
387 void SALOME_Launcher::Shutdown()
392 _NS->Destroy_Name(_LauncherNameInNS);
393 _ContManager->Shutdown();
394 _ResManager->Shutdown();
395 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
396 _poa->deactivate_object(oid);
397 if(!CORBA::is_nil(_orb))
401 //=============================================================================
403 * Returns the PID of the process
405 //=============================================================================
406 CORBA::Long SALOME_Launcher::getPID()
410 (CORBA::Long)getpid();
412 (CORBA::Long)_getpid();
416 //=============================================================================
418 * Returns current launcher jobs list
420 //=============================================================================
422 SALOME_Launcher::getJobsList()
424 Engines::JobsList_var jobs_list = new Engines::JobsList();
425 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
426 std::map<int, Launcher::Job *>::const_iterator it_job;
428 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
430 int number = it_job->first;
433 // Prepare CORBA job description
434 Engines::JobDescription_var job_descr = new Engines::JobDescription();
435 Engines::JobParameters_var job_parameters = getJobParameters(number);
436 job_descr->job_id = number;
437 job_descr->job_parameters = job_parameters;
439 // Add job description to the sequence
440 jobs_list->length(list_id + 1);
441 jobs_list[list_id] = job_descr;
446 return jobs_list._retn();
449 //=============================================================================
451 * Returns the job description
453 //=============================================================================
454 Engines::JobParameters *
455 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
457 Engines::JobParameters_var job_parameters;
460 JobParameters_cpp cpp_parameters = _l.getJobParameters(job_id);
461 job_parameters = JobParameters_CPP2CORBA(cpp_parameters);
463 catch(const LauncherException &ex)
465 INFOS(ex.msg.c_str());
466 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
469 return job_parameters._retn();
472 //=============================================================================
474 * Loads jobs saved in jobs_file
476 //=============================================================================
478 SALOME_Launcher::loadJobs(const char* jobs_file)
480 list<int> new_jobs_id_list;
483 // Load the jobs in Launcher
484 new_jobs_id_list = _l.loadJobs(jobs_file);
486 catch (const LauncherException & ex)
488 INFOS(ex.msg.c_str());
489 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
492 // Notify observers of the new jobs
493 list<int>::const_iterator it_jobs_id;
494 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
496 ostringstream job_id_sstr;
497 job_id_sstr << *it_jobs_id;
498 notifyObservers("NEW_JOB", job_id_sstr.str());
500 notifyObservers("LOAD_JOBS", jobs_file);
503 //=============================================================================
505 * Save jobs of Launcher (in any steps) in file jobs_file
507 //=============================================================================
509 SALOME_Launcher::saveJobs(const char* jobs_file)
511 _l.saveJobs(jobs_file);
512 notifyObservers("SAVE_JOBS", jobs_file);
515 //=============================================================================
517 * Add a new observer to the launcher
519 //=============================================================================
521 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
523 bool new_observer = true;
524 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
525 while(iter != _observers.end())
527 if (std::string(_orb->object_to_string(*iter)) ==
528 std::string(_orb->object_to_string(observer)))
530 new_observer = false;
536 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
538 // We notify the new observer with all jobs that are currently in the Launcher
539 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
540 std::map<int, Launcher::Job *>::const_iterator it_job;
541 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
543 int number = it_job->first;
544 std::ostringstream job_id;
548 observer->notify("NEW_JOB", job_id.str().c_str());
552 MESSAGE("Notify Observer, exception catch");
558 //=============================================================================
560 * Add a new observer to the launcher
562 //=============================================================================
564 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
566 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
567 while(iter != _observers.end())
569 if (std::string(_orb->object_to_string(*iter)) ==
570 std::string(_orb->object_to_string(observer)))
573 iter =_observers.erase(iter++);
582 //=============================================================================
584 * Notify observers on a new event
586 //=============================================================================
588 SALOME_Launcher::notifyObservers(const std::string & event_name,
589 const std::string & event_data)
591 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
592 while(iter != _observers.end())
596 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
597 CORBA::string_dup(event_data.c_str()));
601 MESSAGE("Notify Observer, exception catch");
609 SALOME_Launcher::JobParameters_CORBA2CPP(
610 const Engines::JobParameters& job_parameters)
612 JobParameters_cpp result;
614 result.job_name = job_parameters.job_name.in();
615 result.job_type = job_parameters.job_type.in();
616 result.job_file = job_parameters.job_file.in();
617 result.pre_command = job_parameters.pre_command.in();
618 result.env_file = job_parameters.env_file.in();
620 result.in_files.clear();
621 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
622 result.in_files.push_back(job_parameters.in_files[i].in());
623 result.out_files.clear();
624 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
625 result.out_files.push_back(job_parameters.out_files[i].in());
627 result.work_directory = job_parameters.work_directory.in();
628 result.local_directory = job_parameters.local_directory.in();
629 result.result_directory = job_parameters.result_directory.in();
630 result.maximum_duration = job_parameters.maximum_duration.in();
632 result.resource_required = resourceParameters_CORBAtoCPP(job_parameters.resource_required);
634 result.queue = job_parameters.queue.in();
635 result.partition = job_parameters.partition.in();
636 result.exclusive = job_parameters.exclusive;
637 result.mem_per_cpu = job_parameters.mem_per_cpu;
638 result.wckey = job_parameters.wckey.in();
639 result.extra_params = job_parameters.extra_params.in();
641 result.specific_parameters.clear();
642 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
643 result.specific_parameters[job_parameters.specific_parameters[i].name.in()]
644 = job_parameters.specific_parameters[i].value.in();
646 result.launcher_file = job_parameters.launcher_file.in();
647 result.launcher_args = job_parameters.launcher_args.in();
651 Engines::JobParameters_var
652 SALOME_Launcher::JobParameters_CPP2CORBA(const JobParameters_cpp& job_parameters)
654 Engines::JobParameters_var result = new Engines::JobParameters;
655 result->job_name = CORBA::string_dup(job_parameters.job_name.c_str());
656 result->job_type = CORBA::string_dup(job_parameters.job_type.c_str());
657 result->job_file = CORBA::string_dup(job_parameters.job_file.c_str());
658 result->pre_command = CORBA::string_dup(job_parameters.pre_command.c_str());
659 result->env_file = CORBA::string_dup(job_parameters.env_file.c_str());
660 result->in_files.length((CORBA::ULong)job_parameters.in_files.size()); //!< TODO: conversion from size_t to CORBA::ULong
663 for(const std::string& it : job_parameters.in_files)
665 result->in_files[i] = CORBA::string_dup(it.c_str());
668 result->out_files.length((CORBA::ULong)job_parameters.out_files.size()); //!< TODO: conversion from size_t to CORBA::ULong
670 for(const std::string& it : job_parameters.out_files)
672 result->out_files[i] = CORBA::string_dup(it.c_str());
676 result->work_directory = CORBA::string_dup(job_parameters.work_directory.c_str());
677 result->local_directory = CORBA::string_dup(job_parameters.local_directory.c_str());
678 result->result_directory = CORBA::string_dup(job_parameters.result_directory.c_str());
679 result->maximum_duration = CORBA::string_dup(job_parameters.maximum_duration.c_str());
681 result->resource_required = resourceParameters_CPPtoCORBA(job_parameters.resource_required);
683 result->queue = CORBA::string_dup(job_parameters.queue.c_str());
684 result->partition = CORBA::string_dup(job_parameters.partition.c_str());
685 result->exclusive = job_parameters.exclusive;
686 result->mem_per_cpu = job_parameters.mem_per_cpu;
687 result->wckey = CORBA::string_dup(job_parameters.wckey.c_str());
688 result->extra_params = CORBA::string_dup(job_parameters.extra_params.c_str());
690 const std::map<std::string, std::string>& specific_parameters
691 = job_parameters.specific_parameters;
692 if (!specific_parameters.empty())
694 result->specific_parameters.length((CORBA::ULong)specific_parameters.size()); //!< TODO: conversion from size_t to CORBA::ULong
696 for (const auto& it_specific : specific_parameters)
698 Engines::Parameter_var new_param = new Engines::Parameter;
699 new_param->name = CORBA::string_dup(it_specific.first.c_str());
700 new_param->value = CORBA::string_dup(it_specific.second.c_str());
701 result->specific_parameters[i] = new_param;
706 result->launcher_file = CORBA::string_dup(job_parameters.launcher_file.c_str());
707 result->launcher_args = CORBA::string_dup(job_parameters.launcher_args.c_str());
711 static SALOME_Launcher *_launcher_singleton_ssl = nullptr;
713 #include "SALOME_Fake_NamingService.hxx"
715 SALOME_Launcher *KERNEL::getLauncherSA()
717 if(!_launcher_singleton_ssl)
719 CORBA::ORB_var orb = KERNEL::GetRefToORB();
720 //[EDF26673] : do not release the POA object returned by _the_root_poa.
721 PortableServer::POA_ptr root_poa = PortableServer::POA::_the_root_poa();
722 PortableServer::POAManager_var pman = root_poa->the_POAManager();
723 CORBA::PolicyList policies;
725 PortableServer::ThreadPolicy_var threadPol(root_poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL));
726 policies[0] = PortableServer::ThreadPolicy::_duplicate(threadPol);
727 PortableServer::POA_var safePOA = root_poa->create_POA("SingleThreadPOA",
730 threadPol->destroy();
731 SALOME_Fake_NamingService *ns=new SALOME_Fake_NamingService(orb);
732 _launcher_singleton_ssl = new SALOME_Launcher(orb,safePOA,ns);//3rd arg is important to skip NS !
734 return _launcher_singleton_ssl;