1 // Copyright (C) 2007-2014 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 "Utils_CorbaException.hxx"
30 #include "Launcher_Job_Command.hxx"
31 #include "Launcher_Job_YACSFile.hxx"
32 #include "Launcher_Job_PythonSALOME.hxx"
39 #include <sys/types.h>
48 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
50 //=============================================================================
55 //=============================================================================
56 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
58 MESSAGE("SALOME_Launcher constructor");
59 _NS = new SALOME_NamingService(orb);
60 _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
61 _l.SetResourcesManager(_ResManager->GetImpl());
62 _ContManager = new SALOME_ContainerManager(orb,poa,_ResManager,_NS);
63 _ResManager->_remove_ref();
64 _ContManager->_remove_ref();
66 _orb = CORBA::ORB::_duplicate(orb) ;
67 _poa = PortableServer::POA::_duplicate(poa) ;
68 PortableServer::ObjectId_var id = _poa->activate_object(this);
69 CORBA::Object_var obj = _poa->id_to_reference(id);
70 Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
72 _NS->Register(refContMan,_LauncherNameInNS);
73 MESSAGE("SALOME_Launcher constructor end");
76 //=============================================================================
80 //=============================================================================
81 SALOME_Launcher::~SALOME_Launcher()
83 MESSAGE("SALOME_Launcher destructor");
85 MESSAGE("SALOME_Launcher destructor end");
90 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
92 std::string job_type = job_parameters.job_type.in();
94 if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
96 std::string message("SALOME_Launcher::createJob: bad job type: ");
98 THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
101 Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
103 if (job_type == "command")
104 new_job = new Launcher::Job_Command();
105 else if (job_type == "yacs_file")
106 new_job = new Launcher::Job_YACSFile();
107 else if (job_type == "python_salome")
108 new_job = new Launcher::Job_PythonSALOME();
111 new_job->setJobName(job_parameters.job_name.in());
114 std::string work_directory = job_parameters.work_directory.in();
115 std::string local_directory = job_parameters.local_directory.in();
116 std::string result_directory = job_parameters.result_directory.in();
117 new_job->setWorkDirectory(work_directory);
118 new_job->setLocalDirectory(local_directory);
119 new_job->setResultDirectory(result_directory);
121 // Parameters for COORM
122 std::string launcher_file = job_parameters.launcher_file.in();
123 std::string launcher_args = job_parameters.launcher_args.in();
124 new_job->setLauncherFile(launcher_file);
125 new_job->setLauncherArgs(launcher_args);
128 std::string job_file = job_parameters.job_file.in();
131 new_job->setJobFile(job_file);
133 catch(const LauncherException &ex)
135 INFOS(ex.msg.c_str());
136 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
140 std::string env_file = job_parameters.env_file.in();
141 new_job->setEnvFile(env_file);
142 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
143 new_job->add_in_file(job_parameters.in_files[i].in());
144 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
145 new_job->add_out_file(job_parameters.out_files[i].in());
147 // Expected During Time
150 std::string maximum_duration = job_parameters.maximum_duration.in();
151 new_job->setMaximumDuration(maximum_duration);
153 catch(const LauncherException &ex){
154 INFOS(ex.msg.c_str());
155 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
159 std::string queue = job_parameters.queue.in();
160 new_job->setQueue(queue);
163 new_job->setExclusive(job_parameters.exclusive);
165 // Memory required per CPU
166 new_job->setMemPerCpu(job_parameters.mem_per_cpu);
169 std::string wckey = job_parameters.wckey.in();
170 new_job->setWCKey(wckey);
173 std::string extra_params = job_parameters.extra_params.in();
174 new_job->setExtraParams(extra_params);
176 // Resources requirements
180 p.name = job_parameters.resource_required.name;
181 p.hostname = job_parameters.resource_required.hostname;
182 p.OS = job_parameters.resource_required.OS;
183 p.nb_proc = job_parameters.resource_required.nb_proc;
184 p.nb_node = job_parameters.resource_required.nb_node;
185 p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
186 p.cpu_clock = job_parameters.resource_required.cpu_clock;
187 p.mem_mb = job_parameters.resource_required.mem_mb;
188 new_job->setResourceRequiredParams(p);
190 catch(const LauncherException &ex){
191 INFOS(ex.msg.c_str());
192 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
195 // Adding specific parameters to the job
196 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
197 new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
198 job_parameters.specific_parameters[i].value.in());
201 new_job->checkSpecificParameters();
203 catch(const LauncherException &ex)
205 INFOS(ex.msg.c_str());
206 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
211 _l.createJob(new_job);
212 std::ostringstream job_id;
213 job_id << new_job->getNumber();
214 notifyObservers("NEW_JOB", job_id.str());
216 catch(const LauncherException &ex)
218 INFOS(ex.msg.c_str());
219 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
221 return new_job->getNumber();
225 SALOME_Launcher::launchJob(CORBA::Long job_id)
229 _l.launchJob(job_id);
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::getJobState(CORBA::Long job_id)
244 result = _l.getJobState(job_id);
246 catch(const LauncherException &ex)
248 INFOS(ex.msg.c_str());
249 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
251 return CORBA::string_dup(result.c_str());
254 // Get names or ids of hosts assigned to the job
256 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
261 result = _l.getAssignedHostnames(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::getJobResults(CORBA::Long job_id, const char * directory)
276 _l.getJobResults(job_id, directory);
278 catch(const LauncherException &ex)
280 INFOS(ex.msg.c_str());
281 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
286 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
288 CORBA::Boolean rtn = false;
291 rtn = _l.getJobDumpState(job_id, directory);
293 catch(const LauncherException &ex)
295 INFOS(ex.msg.c_str());
296 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
302 SALOME_Launcher::removeJob(CORBA::Long job_id)
306 _l.removeJob(job_id);
307 std::ostringstream job_id_str;
308 job_id_str << job_id;
309 notifyObservers("REMOVE_JOB", job_id_str.str());
311 catch(const LauncherException &ex)
313 INFOS(ex.msg.c_str());
314 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
319 SALOME_Launcher::stopJob(CORBA::Long job_id)
324 std::ostringstream job_id_str;
325 job_id_str << job_id;
326 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
328 catch(const LauncherException &ex)
330 INFOS(ex.msg.c_str());
331 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
335 //=============================================================================
337 * Create a job in the launcher with a file
338 * \param xmlExecuteFile : .xml to parse that contains job description
339 * \param clusterName : machine choosed
341 //=============================================================================
343 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
344 const char * clusterName)
348 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
350 catch(const LauncherException &ex){
351 INFOS(ex.msg.c_str());
352 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
358 //=============================================================================
360 * the test batch configuration
361 * \param params : The batch cluster
363 //=============================================================================
365 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
367 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
368 CORBA::Boolean rtn = false;
371 // Consider only resources that can run batch jobs
372 Engines::ResourceParameters new_params(params);
373 new_params.can_launch_batch_jobs = true;
375 // find a resource matching the required parameters
376 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
377 if (aMachineList->length() == 0)
378 throw SALOME_Exception("No resources have been found with your parameters");
380 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
381 std::string resource_name(p->name);
382 INFOS("Choose resource for test: " << resource_name);
390 catch(const LauncherException &ex){
391 INFOS(ex.msg.c_str());
392 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
397 //=============================================================================
399 * shutdown all the containers, then the ContainerManager servant
401 //=============================================================================
402 void SALOME_Launcher::Shutdown()
405 _NS->Destroy_Name(_LauncherNameInNS);
406 _ContManager->Shutdown();
407 _ResManager->Shutdown();
408 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
409 _poa->deactivate_object(oid);
410 if(!CORBA::is_nil(_orb))
414 //=============================================================================
416 * Returns the PID of the process
418 //=============================================================================
419 CORBA::Long SALOME_Launcher::getPID()
423 (CORBA::Long)getpid();
425 (CORBA::Long)_getpid();
429 //=============================================================================
431 * Returns current launcher jobs list
433 //=============================================================================
435 SALOME_Launcher::getJobsList()
437 Engines::JobsList_var jobs_list = new Engines::JobsList();
438 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
439 std::map<int, Launcher::Job *>::const_iterator it_job;
441 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
443 int number = it_job->first;
446 // Prepare CORBA job description
447 Engines::JobDescription_var job_descr = new Engines::JobDescription();
448 Engines::JobParameters_var job_parameters = getJobParameters(number);
449 job_descr->job_id = number;
450 job_descr->job_parameters = job_parameters;
452 // Add job description to the sequence
453 jobs_list->length(list_id + 1);
454 jobs_list[list_id] = job_descr;
459 return jobs_list._retn();
462 //=============================================================================
464 * Returns the job description
466 //=============================================================================
467 Engines::JobParameters *
468 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
470 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
471 std::map<int, Launcher::Job *>::const_iterator it_job = cpp_jobs.find(job_id);
472 if (it_job == cpp_jobs.end())
474 INFOS("Cannot find the job, is it created ? job number: " << job_id);
475 THROW_SALOME_CORBA_EXCEPTION("Job does not exist", SALOME::INTERNAL_ERROR);
478 Launcher::Job * job = it_job->second;
479 Engines::JobParameters_var job_parameters = new Engines::JobParameters;
480 job_parameters->job_name = CORBA::string_dup(job->getJobName().c_str());
481 job_parameters->job_type = CORBA::string_dup(job->getJobType().c_str());
482 job_parameters->job_file = CORBA::string_dup(job->getJobFile().c_str());
483 job_parameters->env_file = CORBA::string_dup(job->getEnvFile().c_str());
484 job_parameters->work_directory = CORBA::string_dup(job->getWorkDirectory().c_str());
485 job_parameters->local_directory = CORBA::string_dup(job->getLocalDirectory().c_str());
486 job_parameters->result_directory = CORBA::string_dup(job->getResultDirectory().c_str());
488 // Parameters for COORM
489 job_parameters->launcher_file = CORBA::string_dup(job->getLauncherFile().c_str());
490 job_parameters->launcher_args = CORBA::string_dup(job->getLauncherArgs().c_str());
494 std::list<std::string> in_files = job->get_in_files();
495 std::list<std::string> out_files = job->get_out_files();
496 job_parameters->in_files.length(in_files.size());
497 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
499 job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
502 job_parameters->out_files.length(out_files.size());
503 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
505 job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
509 job_parameters->maximum_duration = CORBA::string_dup(job->getMaximumDuration().c_str());
510 job_parameters->queue = CORBA::string_dup(job->getQueue().c_str());
511 job_parameters->exclusive = job->getExclusive();
512 job_parameters->mem_per_cpu = job->getMemPerCpu();
513 job_parameters->wckey = CORBA::string_dup(job->getWCKey().c_str());
514 job_parameters->extra_params = CORBA::string_dup(job->getExtraParams().c_str());
516 resourceParams resource_params = job->getResourceRequiredParams();
517 job_parameters->resource_required.name = CORBA::string_dup(resource_params.name.c_str());
518 job_parameters->resource_required.hostname = CORBA::string_dup(resource_params.hostname.c_str());
519 job_parameters->resource_required.OS = CORBA::string_dup(resource_params.OS.c_str());
520 job_parameters->resource_required.nb_proc = resource_params.nb_proc;
521 job_parameters->resource_required.nb_node = resource_params.nb_node;
522 job_parameters->resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
523 job_parameters->resource_required.cpu_clock = resource_params.cpu_clock;
524 job_parameters->resource_required.mem_mb = resource_params.mem_mb;
526 std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
527 if (!specific_parameters.empty())
529 job_parameters->specific_parameters.length(specific_parameters.size());
530 std::map<std::string, std::string>::const_iterator it_specific;
532 for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
534 Engines::Parameter_var new_param = new Engines::Parameter;
535 new_param->name = CORBA::string_dup((it_specific->first).c_str());
536 new_param->value = CORBA::string_dup((it_specific->second).c_str());
537 job_parameters->specific_parameters[i] = new_param;
542 return job_parameters._retn();
545 //=============================================================================
547 * Loads jobs saved in jobs_file
549 //=============================================================================
551 SALOME_Launcher::loadJobs(const char* jobs_file)
553 list<int> new_jobs_id_list;
556 // Load the jobs in Launcher
557 new_jobs_id_list = _l.loadJobs(jobs_file);
559 catch (const LauncherException & ex)
561 INFOS(ex.msg.c_str());
562 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
565 // Notify observers of the new jobs
566 list<int>::const_iterator it_jobs_id;
567 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
569 ostringstream job_id_sstr;
570 job_id_sstr << *it_jobs_id;
571 notifyObservers("NEW_JOB", job_id_sstr.str());
573 notifyObservers("LOAD_JOBS", jobs_file);
576 //=============================================================================
578 * Save jobs of Launcher (in any steps) in file jobs_file
580 //=============================================================================
582 SALOME_Launcher::saveJobs(const char* jobs_file)
584 _l.saveJobs(jobs_file);
585 notifyObservers("SAVE_JOBS", jobs_file);
588 //=============================================================================
590 * Add a new observer to the launcher
592 //=============================================================================
594 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
596 bool new_observer = true;
597 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
598 while(iter != _observers.end())
600 if (std::string(_orb->object_to_string(*iter)) ==
601 std::string(_orb->object_to_string(observer)))
603 new_observer = false;
609 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
611 // We notify the new observer with all jobs that are currently in the Launcher
612 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
613 std::map<int, Launcher::Job *>::const_iterator it_job;
614 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
616 int number = it_job->first;
617 std::ostringstream job_id;
621 observer->notify("NEW_JOB", job_id.str().c_str());
625 MESSAGE("Notify Observer, exception catch");
631 //=============================================================================
633 * Add a new observer to the launcher
635 //=============================================================================
637 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
639 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
640 while(iter != _observers.end())
642 if (std::string(_orb->object_to_string(*iter)) ==
643 std::string(_orb->object_to_string(observer)))
646 iter =_observers.erase(iter++);
655 //=============================================================================
657 * Notify observers on a new event
659 //=============================================================================
661 SALOME_Launcher::notifyObservers(const std::string & event_name,
662 const std::string & event_data)
664 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
665 while(iter != _observers.end())
669 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
670 CORBA::string_dup(event_data.c_str()));
674 MESSAGE("Notify Observer, exception catch");