1 // Copyright (C) 2007-2016 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 std::string job_type = job_parameters.job_type.in();
99 Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
101 if (job_type == Launcher::Job_Command::TYPE_NAME)
102 new_job = new Launcher::Job_Command();
103 else if (job_type == Launcher::Job_CommandSALOME::TYPE_NAME)
104 new_job = new Launcher::Job_CommandSALOME();
105 else if (job_type == Launcher::Job_YACSFile::TYPE_NAME)
106 new_job = new Launcher::Job_YACSFile();
107 else if (job_type == Launcher::Job_PythonSALOME::TYPE_NAME)
108 new_job = new Launcher::Job_PythonSALOME();
111 std::string message("SALOME_Launcher::createJob: bad job type: ");
113 THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
117 new_job->setJobName(job_parameters.job_name.in());
120 std::string work_directory = job_parameters.work_directory.in();
121 std::string local_directory = job_parameters.local_directory.in();
122 std::string result_directory = job_parameters.result_directory.in();
123 new_job->setWorkDirectory(work_directory);
124 new_job->setLocalDirectory(local_directory);
125 new_job->setResultDirectory(result_directory);
127 // Parameters for COORM
128 std::string launcher_file = job_parameters.launcher_file.in();
129 std::string launcher_args = job_parameters.launcher_args.in();
130 new_job->setLauncherFile(launcher_file);
131 new_job->setLauncherArgs(launcher_args);
134 std::string job_file = job_parameters.job_file.in();
137 new_job->setJobFile(job_file);
139 catch(const LauncherException &ex)
141 INFOS(ex.msg.c_str());
142 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
144 new_job->setPreCommand(job_parameters.pre_command.in());
147 std::string env_file = job_parameters.env_file.in();
148 new_job->setEnvFile(env_file);
149 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
150 new_job->add_in_file(job_parameters.in_files[i].in());
151 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
152 new_job->add_out_file(job_parameters.out_files[i].in());
154 // Expected During Time
157 std::string maximum_duration = job_parameters.maximum_duration.in();
158 new_job->setMaximumDuration(maximum_duration);
160 catch(const LauncherException &ex){
161 INFOS(ex.msg.c_str());
162 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
166 std::string queue = job_parameters.queue.in();
167 new_job->setQueue(queue);
170 std::string partition = job_parameters.partition.in();
171 new_job->setPartition(partition);
174 new_job->setExclusive(job_parameters.exclusive);
176 // Memory required per CPU
177 new_job->setMemPerCpu(job_parameters.mem_per_cpu);
180 std::string wckey = job_parameters.wckey.in();
181 new_job->setWCKey(wckey);
184 std::string extra_params = job_parameters.extra_params.in();
185 new_job->setExtraParams(extra_params);
187 // Resources requirements
191 p.name = job_parameters.resource_required.name;
192 p.hostname = job_parameters.resource_required.hostname;
193 p.OS = job_parameters.resource_required.OS;
194 p.nb_proc = job_parameters.resource_required.nb_proc;
195 p.nb_node = job_parameters.resource_required.nb_node;
196 p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
197 p.cpu_clock = job_parameters.resource_required.cpu_clock;
198 p.mem_mb = job_parameters.resource_required.mem_mb;
199 new_job->setResourceRequiredParams(p);
201 catch(const LauncherException &ex){
202 INFOS(ex.msg.c_str());
203 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
206 // Adding specific parameters to the job
207 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
208 new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
209 job_parameters.specific_parameters[i].value.in());
212 new_job->checkSpecificParameters();
214 catch(const LauncherException &ex)
216 INFOS(ex.msg.c_str());
217 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
222 _l.createJob(new_job);
223 std::ostringstream job_id;
224 job_id << new_job->getNumber();
225 notifyObservers("NEW_JOB", job_id.str());
227 catch(const LauncherException &ex)
229 INFOS(ex.msg.c_str());
230 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
232 return new_job->getNumber();
236 SALOME_Launcher::launchJob(CORBA::Long job_id)
240 _l.launchJob(job_id);
242 catch(const LauncherException &ex)
244 INFOS(ex.msg.c_str());
245 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
250 SALOME_Launcher::getJobState(CORBA::Long job_id)
255 result = _l.getJobState(job_id);
257 catch(const LauncherException &ex)
259 INFOS(ex.msg.c_str());
260 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
262 return CORBA::string_dup(result.c_str());
265 // Get names or ids of hosts assigned to the job
267 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
272 result = _l.getAssignedHostnames(job_id);
274 catch(const LauncherException &ex)
276 INFOS(ex.msg.c_str());
277 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
279 return CORBA::string_dup(result.c_str());
283 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
287 _l.getJobResults(job_id, directory);
289 catch(const LauncherException &ex)
291 INFOS(ex.msg.c_str());
292 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
297 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
301 _l.clearJobWorkingDir(job_id);
303 catch(const LauncherException &ex)
305 INFOS(ex.msg.c_str());
306 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
311 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
313 CORBA::Boolean rtn = false;
316 rtn = _l.getJobDumpState(job_id, directory);
318 catch(const LauncherException &ex)
320 INFOS(ex.msg.c_str());
321 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
327 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
329 CORBA::Boolean rtn = false;
332 rtn = _l.getJobWorkFile(job_id, work_file, directory);
334 catch(const LauncherException &ex)
336 INFOS(ex.msg.c_str());
337 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
343 SALOME_Launcher::removeJob(CORBA::Long job_id)
347 _l.removeJob(job_id);
348 std::ostringstream job_id_str;
349 job_id_str << job_id;
350 notifyObservers("REMOVE_JOB", job_id_str.str());
352 catch(const LauncherException &ex)
354 INFOS(ex.msg.c_str());
355 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
360 SALOME_Launcher::stopJob(CORBA::Long job_id)
365 std::ostringstream job_id_str;
366 job_id_str << job_id;
367 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
369 catch(const LauncherException &ex)
371 INFOS(ex.msg.c_str());
372 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
377 SALOME_Launcher::dumpJob(CORBA::Long job_id)
382 result = _l.dumpJob(job_id);
384 catch(const LauncherException &ex)
386 INFOS(ex.msg.c_str());
387 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
389 return CORBA::string_dup(result.c_str());
393 SALOME_Launcher::restoreJob(const char * dumpedJob)
397 jobId = _l.restoreJob(dumpedJob);
400 std::ostringstream job_str;
402 notifyObservers("NEW_JOB", job_str.str());
405 catch(const LauncherException &ex){
406 INFOS(ex.msg.c_str());
407 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
413 //=============================================================================
415 * Create a job in the launcher with a file
416 * \param xmlExecuteFile : .xml to parse that contains job description
417 * \param clusterName : machine chosen
419 //=============================================================================
421 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
422 const char * clusterName)
426 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
428 catch(const LauncherException &ex){
429 INFOS(ex.msg.c_str());
430 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
436 //=============================================================================
438 * the test batch configuration
439 * \param params : The batch cluster
441 //=============================================================================
443 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
445 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
446 CORBA::Boolean rtn = false;
449 // Consider only resources that can run batch jobs
450 Engines::ResourceParameters new_params(params);
451 new_params.can_launch_batch_jobs = true;
453 // find a resource matching the required parameters
454 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
455 if (aMachineList->length() == 0)
456 throw SALOME_Exception("No resources have been found with your parameters");
458 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
459 std::string resource_name(p->name);
460 INFOS("Choose resource for test: " << resource_name);
468 catch(const LauncherException &ex){
469 INFOS(ex.msg.c_str());
470 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
475 //=============================================================================
477 * shutdown all the containers, then the ContainerManager servant
479 //=============================================================================
480 void SALOME_Launcher::Shutdown()
483 _NS->Destroy_Name(_LauncherNameInNS);
484 _ContManager->Shutdown();
485 _ResManager->Shutdown();
486 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
487 _poa->deactivate_object(oid);
488 if(!CORBA::is_nil(_orb))
492 //=============================================================================
494 * Returns the PID of the process
496 //=============================================================================
497 CORBA::Long SALOME_Launcher::getPID()
501 (CORBA::Long)getpid();
503 (CORBA::Long)_getpid();
507 //=============================================================================
509 * Returns current launcher jobs list
511 //=============================================================================
513 SALOME_Launcher::getJobsList()
515 Engines::JobsList_var jobs_list = new Engines::JobsList();
516 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
517 std::map<int, Launcher::Job *>::const_iterator it_job;
519 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
521 int number = it_job->first;
524 // Prepare CORBA job description
525 Engines::JobDescription_var job_descr = new Engines::JobDescription();
526 Engines::JobParameters_var job_parameters = getJobParameters(number);
527 job_descr->job_id = number;
528 job_descr->job_parameters = job_parameters;
530 // Add job description to the sequence
531 jobs_list->length(list_id + 1);
532 jobs_list[list_id] = job_descr;
537 return jobs_list._retn();
540 //=============================================================================
542 * Returns the job description
544 //=============================================================================
545 Engines::JobParameters *
546 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
548 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
549 std::map<int, Launcher::Job *>::const_iterator it_job = cpp_jobs.find(job_id);
550 if (it_job == cpp_jobs.end())
552 INFOS("Cannot find the job, is it created ? job number: " << job_id);
553 THROW_SALOME_CORBA_EXCEPTION("Job does not exist", SALOME::INTERNAL_ERROR);
556 Launcher::Job * job = it_job->second;
557 Engines::JobParameters_var job_parameters = new Engines::JobParameters;
558 job_parameters->job_name = CORBA::string_dup(job->getJobName().c_str());
559 job_parameters->job_type = CORBA::string_dup(job->getJobType().c_str());
560 job_parameters->job_file = CORBA::string_dup(job->getJobFile().c_str());
561 job_parameters->env_file = CORBA::string_dup(job->getEnvFile().c_str());
562 job_parameters->work_directory = CORBA::string_dup(job->getWorkDirectory().c_str());
563 job_parameters->local_directory = CORBA::string_dup(job->getLocalDirectory().c_str());
564 job_parameters->result_directory = CORBA::string_dup(job->getResultDirectory().c_str());
565 job_parameters->pre_command = CORBA::string_dup(job->getPreCommand().c_str());
567 // Parameters for COORM
568 job_parameters->launcher_file = CORBA::string_dup(job->getLauncherFile().c_str());
569 job_parameters->launcher_args = CORBA::string_dup(job->getLauncherArgs().c_str());
573 std::list<std::string> in_files = job->get_in_files();
574 std::list<std::string> out_files = job->get_out_files();
575 job_parameters->in_files.length(in_files.size());
576 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
578 job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
581 job_parameters->out_files.length(out_files.size());
582 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
584 job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
588 job_parameters->maximum_duration = CORBA::string_dup(job->getMaximumDuration().c_str());
589 job_parameters->queue = CORBA::string_dup(job->getQueue().c_str());
590 job_parameters->partition = CORBA::string_dup(job->getPartition().c_str());
591 job_parameters->exclusive = job->getExclusive();
592 job_parameters->mem_per_cpu = job->getMemPerCpu();
593 job_parameters->wckey = CORBA::string_dup(job->getWCKey().c_str());
594 job_parameters->extra_params = CORBA::string_dup(job->getExtraParams().c_str());
596 resourceParams resource_params = job->getResourceRequiredParams();
597 job_parameters->resource_required.name = CORBA::string_dup(resource_params.name.c_str());
598 job_parameters->resource_required.hostname = CORBA::string_dup(resource_params.hostname.c_str());
599 job_parameters->resource_required.OS = CORBA::string_dup(resource_params.OS.c_str());
600 job_parameters->resource_required.nb_proc = resource_params.nb_proc;
601 job_parameters->resource_required.nb_node = resource_params.nb_node;
602 job_parameters->resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
603 job_parameters->resource_required.cpu_clock = resource_params.cpu_clock;
604 job_parameters->resource_required.mem_mb = resource_params.mem_mb;
606 std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
607 if (!specific_parameters.empty())
609 job_parameters->specific_parameters.length(specific_parameters.size());
610 std::map<std::string, std::string>::const_iterator it_specific;
612 for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
614 Engines::Parameter_var new_param = new Engines::Parameter;
615 new_param->name = CORBA::string_dup((it_specific->first).c_str());
616 new_param->value = CORBA::string_dup((it_specific->second).c_str());
617 job_parameters->specific_parameters[i] = new_param;
622 return job_parameters._retn();
625 //=============================================================================
627 * Loads jobs saved in jobs_file
629 //=============================================================================
631 SALOME_Launcher::loadJobs(const char* jobs_file)
633 list<int> new_jobs_id_list;
636 // Load the jobs in Launcher
637 new_jobs_id_list = _l.loadJobs(jobs_file);
639 catch (const LauncherException & ex)
641 INFOS(ex.msg.c_str());
642 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
645 // Notify observers of the new jobs
646 list<int>::const_iterator it_jobs_id;
647 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
649 ostringstream job_id_sstr;
650 job_id_sstr << *it_jobs_id;
651 notifyObservers("NEW_JOB", job_id_sstr.str());
653 notifyObservers("LOAD_JOBS", jobs_file);
656 //=============================================================================
658 * Save jobs of Launcher (in any steps) in file jobs_file
660 //=============================================================================
662 SALOME_Launcher::saveJobs(const char* jobs_file)
664 _l.saveJobs(jobs_file);
665 notifyObservers("SAVE_JOBS", jobs_file);
668 //=============================================================================
670 * Add a new observer to the launcher
672 //=============================================================================
674 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
676 bool new_observer = true;
677 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
678 while(iter != _observers.end())
680 if (std::string(_orb->object_to_string(*iter)) ==
681 std::string(_orb->object_to_string(observer)))
683 new_observer = false;
689 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
691 // We notify the new observer with all jobs that are currently in the Launcher
692 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
693 std::map<int, Launcher::Job *>::const_iterator it_job;
694 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
696 int number = it_job->first;
697 std::ostringstream job_id;
701 observer->notify("NEW_JOB", job_id.str().c_str());
705 MESSAGE("Notify Observer, exception catch");
711 //=============================================================================
713 * Add a new observer to the launcher
715 //=============================================================================
717 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
719 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
720 while(iter != _observers.end())
722 if (std::string(_orb->object_to_string(*iter)) ==
723 std::string(_orb->object_to_string(observer)))
726 iter =_observers.erase(iter++);
735 //=============================================================================
737 * Notify observers on a new event
739 //=============================================================================
741 SALOME_Launcher::notifyObservers(const std::string & event_name,
742 const std::string & event_data)
744 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
745 while(iter != _observers.end())
749 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
750 CORBA::string_dup(event_data.c_str()));
754 MESSAGE("Notify Observer, exception catch");