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"
36 #include "utilities.h"
43 #include <sys/types.h>
52 const char *SALOME_Launcher::_LauncherNameInNS = "/SalomeLauncher";
54 //=============================================================================
59 //=============================================================================
60 SALOME_Launcher::SALOME_Launcher(CORBA::ORB_ptr orb, PortableServer::POA_var poa) : _l()
62 MESSAGE("SALOME_Launcher constructor");
63 _NS = new SALOME_NamingService(orb);
64 _ResManager = new SALOME_ResourcesManager(orb,poa,_NS);
65 _l.SetResourcesManager(_ResManager->GetImpl());
66 _ContManager = new SALOME_ContainerManager(orb,poa,_NS);
67 _ResManager->_remove_ref();
68 _ContManager->_remove_ref();
70 _orb = CORBA::ORB::_duplicate(orb) ;
71 _poa = PortableServer::POA::_duplicate(poa) ;
72 PortableServer::ObjectId_var id = _poa->activate_object(this);
73 CORBA::Object_var obj = _poa->id_to_reference(id);
74 Engines::SalomeLauncher_var refContMan = Engines::SalomeLauncher::_narrow(obj);
76 _NS->Register(refContMan,_LauncherNameInNS);
77 MESSAGE("SALOME_Launcher constructor end");
80 //=============================================================================
84 //=============================================================================
85 SALOME_Launcher::~SALOME_Launcher()
87 MESSAGE("SALOME_Launcher destructor");
89 MESSAGE("SALOME_Launcher destructor end");
94 SALOME_Launcher::createJob(const Engines::JobParameters & job_parameters)
96 std::string job_type = job_parameters.job_type.in();
98 if (job_type != "command" && job_type != "yacs_file" && job_type != "python_salome")
100 std::string message("SALOME_Launcher::createJob: bad job type: ");
102 THROW_SALOME_CORBA_EXCEPTION(message.c_str(), SALOME::INTERNAL_ERROR);
105 Launcher::Job * new_job; // It is Launcher_cpp that is going to destroy it
107 if (job_type == "command")
108 new_job = new Launcher::Job_Command();
109 else if (job_type == "yacs_file")
110 new_job = new Launcher::Job_YACSFile();
111 else if (job_type == "python_salome")
112 new_job = new Launcher::Job_PythonSALOME();
115 new_job->setJobName(job_parameters.job_name.in());
118 std::string work_directory = job_parameters.work_directory.in();
119 std::string local_directory = job_parameters.local_directory.in();
120 std::string result_directory = job_parameters.result_directory.in();
121 new_job->setWorkDirectory(work_directory);
122 new_job->setLocalDirectory(local_directory);
123 new_job->setResultDirectory(result_directory);
125 // Parameters for COORM
126 std::string launcher_file = job_parameters.launcher_file.in();
127 std::string launcher_args = job_parameters.launcher_args.in();
128 new_job->setLauncherFile(launcher_file);
129 new_job->setLauncherArgs(launcher_args);
132 std::string job_file = job_parameters.job_file.in();
135 new_job->setJobFile(job_file);
137 catch(const LauncherException &ex)
139 INFOS(ex.msg.c_str());
140 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
142 new_job->setPreCommand(job_parameters.pre_command.in());
145 std::string env_file = job_parameters.env_file.in();
146 new_job->setEnvFile(env_file);
147 for (CORBA::ULong i = 0; i < job_parameters.in_files.length(); i++)
148 new_job->add_in_file(job_parameters.in_files[i].in());
149 for (CORBA::ULong i = 0; i < job_parameters.out_files.length(); i++)
150 new_job->add_out_file(job_parameters.out_files[i].in());
152 // Expected During Time
155 std::string maximum_duration = job_parameters.maximum_duration.in();
156 new_job->setMaximumDuration(maximum_duration);
158 catch(const LauncherException &ex){
159 INFOS(ex.msg.c_str());
160 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
164 std::string queue = job_parameters.queue.in();
165 new_job->setQueue(queue);
168 std::string partition = job_parameters.partition.in();
169 new_job->setPartition(partition);
172 new_job->setExclusive(job_parameters.exclusive);
174 // Memory required per CPU
175 new_job->setMemPerCpu(job_parameters.mem_per_cpu);
178 std::string wckey = job_parameters.wckey.in();
179 new_job->setWCKey(wckey);
182 std::string extra_params = job_parameters.extra_params.in();
183 new_job->setExtraParams(extra_params);
185 // Resources requirements
189 p.name = job_parameters.resource_required.name;
190 p.hostname = job_parameters.resource_required.hostname;
191 p.OS = job_parameters.resource_required.OS;
192 p.nb_proc = job_parameters.resource_required.nb_proc;
193 p.nb_node = job_parameters.resource_required.nb_node;
194 p.nb_proc_per_node = job_parameters.resource_required.nb_proc_per_node;
195 p.cpu_clock = job_parameters.resource_required.cpu_clock;
196 p.mem_mb = job_parameters.resource_required.mem_mb;
197 new_job->setResourceRequiredParams(p);
199 catch(const LauncherException &ex){
200 INFOS(ex.msg.c_str());
201 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
204 // Adding specific parameters to the job
205 for (CORBA::ULong i = 0; i < job_parameters.specific_parameters.length(); i++)
206 new_job->addSpecificParameter(job_parameters.specific_parameters[i].name.in(),
207 job_parameters.specific_parameters[i].value.in());
210 new_job->checkSpecificParameters();
212 catch(const LauncherException &ex)
214 INFOS(ex.msg.c_str());
215 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
220 _l.createJob(new_job);
221 std::ostringstream job_id;
222 job_id << new_job->getNumber();
223 notifyObservers("NEW_JOB", job_id.str());
225 catch(const LauncherException &ex)
227 INFOS(ex.msg.c_str());
228 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
230 return new_job->getNumber();
234 SALOME_Launcher::launchJob(CORBA::Long job_id)
238 _l.launchJob(job_id);
240 catch(const LauncherException &ex)
242 INFOS(ex.msg.c_str());
243 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
248 SALOME_Launcher::getJobState(CORBA::Long job_id)
253 result = _l.getJobState(job_id);
255 catch(const LauncherException &ex)
257 INFOS(ex.msg.c_str());
258 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
260 return CORBA::string_dup(result.c_str());
263 // Get names or ids of hosts assigned to the job
265 SALOME_Launcher::getAssignedHostnames(CORBA::Long job_id)
270 result = _l.getAssignedHostnames(job_id);
272 catch(const LauncherException &ex)
274 INFOS(ex.msg.c_str());
275 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
277 return CORBA::string_dup(result.c_str());
281 SALOME_Launcher::getJobResults(CORBA::Long job_id, const char * directory)
285 _l.getJobResults(job_id, directory);
287 catch(const LauncherException &ex)
289 INFOS(ex.msg.c_str());
290 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
295 SALOME_Launcher::clearJobWorkingDir(CORBA::Long job_id)
299 _l.clearJobWorkingDir(job_id);
301 catch(const LauncherException &ex)
303 INFOS(ex.msg.c_str());
304 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
309 SALOME_Launcher::getJobDumpState(CORBA::Long job_id, const char * directory)
311 CORBA::Boolean rtn = false;
314 rtn = _l.getJobDumpState(job_id, directory);
316 catch(const LauncherException &ex)
318 INFOS(ex.msg.c_str());
319 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
325 SALOME_Launcher::getJobWorkFile(CORBA::Long job_id, const char * work_file, const char * directory)
327 CORBA::Boolean rtn = false;
330 rtn = _l.getJobWorkFile(job_id, work_file, directory);
332 catch(const LauncherException &ex)
334 INFOS(ex.msg.c_str());
335 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
341 SALOME_Launcher::removeJob(CORBA::Long job_id)
345 _l.removeJob(job_id);
346 std::ostringstream job_id_str;
347 job_id_str << job_id;
348 notifyObservers("REMOVE_JOB", job_id_str.str());
350 catch(const LauncherException &ex)
352 INFOS(ex.msg.c_str());
353 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
358 SALOME_Launcher::stopJob(CORBA::Long job_id)
363 std::ostringstream job_id_str;
364 job_id_str << job_id;
365 notifyObservers("UPDATE_JOB_STATE", job_id_str.str());
367 catch(const LauncherException &ex)
369 INFOS(ex.msg.c_str());
370 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::BAD_PARAM);
374 //=============================================================================
376 * Create a job in the launcher with a file
377 * \param xmlExecuteFile : .xml to parse that contains job description
378 * \param clusterName : machine choosed
380 //=============================================================================
382 SALOME_Launcher::createJobWithFile(const char * xmlExecuteFile,
383 const char * clusterName)
387 jobId = _l.createJobWithFile(xmlExecuteFile, clusterName);
389 catch(const LauncherException &ex){
390 INFOS(ex.msg.c_str());
391 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
397 //=============================================================================
399 * the test batch configuration
400 * \param params : The batch cluster
402 //=============================================================================
404 SALOME_Launcher::testBatch(const Engines::ResourceParameters& params)
406 MESSAGE("BEGIN OF SALOME_Launcher::testBatch");
407 CORBA::Boolean rtn = false;
410 // Consider only resources that can run batch jobs
411 Engines::ResourceParameters new_params(params);
412 new_params.can_launch_batch_jobs = true;
414 // find a resource matching the required parameters
415 Engines::ResourceList *aMachineList = _ResManager->GetFittingResources(new_params);
416 if (aMachineList->length() == 0)
417 throw SALOME_Exception("No resources have been found with your parameters");
419 const Engines::ResourceDefinition* p = _ResManager->GetResourceDefinition((*aMachineList)[0]);
420 std::string resource_name(p->name);
421 INFOS("Choose resource for test: " << resource_name);
429 catch(const LauncherException &ex){
430 INFOS(ex.msg.c_str());
431 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(),SALOME::INTERNAL_ERROR);
436 //=============================================================================
438 * shutdown all the containers, then the ContainerManager servant
440 //=============================================================================
441 void SALOME_Launcher::Shutdown()
444 _NS->Destroy_Name(_LauncherNameInNS);
445 _ContManager->Shutdown();
446 _ResManager->Shutdown();
447 PortableServer::ObjectId_var oid = _poa->servant_to_id(this);
448 _poa->deactivate_object(oid);
449 if(!CORBA::is_nil(_orb))
453 //=============================================================================
455 * Returns the PID of the process
457 //=============================================================================
458 CORBA::Long SALOME_Launcher::getPID()
462 (CORBA::Long)getpid();
464 (CORBA::Long)_getpid();
468 //=============================================================================
470 * Returns current launcher jobs list
472 //=============================================================================
474 SALOME_Launcher::getJobsList()
476 Engines::JobsList_var jobs_list = new Engines::JobsList();
477 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
478 std::map<int, Launcher::Job *>::const_iterator it_job;
480 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
482 int number = it_job->first;
485 // Prepare CORBA job description
486 Engines::JobDescription_var job_descr = new Engines::JobDescription();
487 Engines::JobParameters_var job_parameters = getJobParameters(number);
488 job_descr->job_id = number;
489 job_descr->job_parameters = job_parameters;
491 // Add job description to the sequence
492 jobs_list->length(list_id + 1);
493 jobs_list[list_id] = job_descr;
498 return jobs_list._retn();
501 //=============================================================================
503 * Returns the job description
505 //=============================================================================
506 Engines::JobParameters *
507 SALOME_Launcher::getJobParameters(CORBA::Long job_id)
509 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
510 std::map<int, Launcher::Job *>::const_iterator it_job = cpp_jobs.find(job_id);
511 if (it_job == cpp_jobs.end())
513 INFOS("Cannot find the job, is it created ? job number: " << job_id);
514 THROW_SALOME_CORBA_EXCEPTION("Job does not exist", SALOME::INTERNAL_ERROR);
517 Launcher::Job * job = it_job->second;
518 Engines::JobParameters_var job_parameters = new Engines::JobParameters;
519 job_parameters->job_name = CORBA::string_dup(job->getJobName().c_str());
520 job_parameters->job_type = CORBA::string_dup(job->getJobType().c_str());
521 job_parameters->job_file = CORBA::string_dup(job->getJobFile().c_str());
522 job_parameters->env_file = CORBA::string_dup(job->getEnvFile().c_str());
523 job_parameters->work_directory = CORBA::string_dup(job->getWorkDirectory().c_str());
524 job_parameters->local_directory = CORBA::string_dup(job->getLocalDirectory().c_str());
525 job_parameters->result_directory = CORBA::string_dup(job->getResultDirectory().c_str());
526 job_parameters->pre_command = CORBA::string_dup(job->getPreCommand().c_str());
528 // Parameters for COORM
529 job_parameters->launcher_file = CORBA::string_dup(job->getLauncherFile().c_str());
530 job_parameters->launcher_args = CORBA::string_dup(job->getLauncherArgs().c_str());
534 std::list<std::string> in_files = job->get_in_files();
535 std::list<std::string> out_files = job->get_out_files();
536 job_parameters->in_files.length(in_files.size());
537 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
539 job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
542 job_parameters->out_files.length(out_files.size());
543 for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
545 job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
549 job_parameters->maximum_duration = CORBA::string_dup(job->getMaximumDuration().c_str());
550 job_parameters->queue = CORBA::string_dup(job->getQueue().c_str());
551 job_parameters->partition = CORBA::string_dup(job->getPartition().c_str());
552 job_parameters->exclusive = job->getExclusive();
553 job_parameters->mem_per_cpu = job->getMemPerCpu();
554 job_parameters->wckey = CORBA::string_dup(job->getWCKey().c_str());
555 job_parameters->extra_params = CORBA::string_dup(job->getExtraParams().c_str());
557 resourceParams resource_params = job->getResourceRequiredParams();
558 job_parameters->resource_required.name = CORBA::string_dup(resource_params.name.c_str());
559 job_parameters->resource_required.hostname = CORBA::string_dup(resource_params.hostname.c_str());
560 job_parameters->resource_required.OS = CORBA::string_dup(resource_params.OS.c_str());
561 job_parameters->resource_required.nb_proc = resource_params.nb_proc;
562 job_parameters->resource_required.nb_node = resource_params.nb_node;
563 job_parameters->resource_required.nb_proc_per_node = resource_params.nb_proc_per_node;
564 job_parameters->resource_required.cpu_clock = resource_params.cpu_clock;
565 job_parameters->resource_required.mem_mb = resource_params.mem_mb;
567 std::map<std::string, std::string> specific_parameters = job->getSpecificParameters();
568 if (!specific_parameters.empty())
570 job_parameters->specific_parameters.length(specific_parameters.size());
571 std::map<std::string, std::string>::const_iterator it_specific;
573 for (it_specific = specific_parameters.begin() ; it_specific != specific_parameters.end(); it_specific++)
575 Engines::Parameter_var new_param = new Engines::Parameter;
576 new_param->name = CORBA::string_dup((it_specific->first).c_str());
577 new_param->value = CORBA::string_dup((it_specific->second).c_str());
578 job_parameters->specific_parameters[i] = new_param;
583 return job_parameters._retn();
586 //=============================================================================
588 * Loads jobs saved in jobs_file
590 //=============================================================================
592 SALOME_Launcher::loadJobs(const char* jobs_file)
594 list<int> new_jobs_id_list;
597 // Load the jobs in Launcher
598 new_jobs_id_list = _l.loadJobs(jobs_file);
600 catch (const LauncherException & ex)
602 INFOS(ex.msg.c_str());
603 THROW_SALOME_CORBA_EXCEPTION(ex.msg.c_str(), SALOME::INTERNAL_ERROR);
606 // Notify observers of the new jobs
607 list<int>::const_iterator it_jobs_id;
608 for (it_jobs_id = new_jobs_id_list.begin(); it_jobs_id != new_jobs_id_list.end(); it_jobs_id++)
610 ostringstream job_id_sstr;
611 job_id_sstr << *it_jobs_id;
612 notifyObservers("NEW_JOB", job_id_sstr.str());
614 notifyObservers("LOAD_JOBS", jobs_file);
617 //=============================================================================
619 * Save jobs of Launcher (in any steps) in file jobs_file
621 //=============================================================================
623 SALOME_Launcher::saveJobs(const char* jobs_file)
625 _l.saveJobs(jobs_file);
626 notifyObservers("SAVE_JOBS", jobs_file);
629 //=============================================================================
631 * Add a new observer to the launcher
633 //=============================================================================
635 SALOME_Launcher::addObserver(Engines::SalomeLauncherObserver_ptr observer)
637 bool new_observer = true;
638 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
639 while(iter != _observers.end())
641 if (std::string(_orb->object_to_string(*iter)) ==
642 std::string(_orb->object_to_string(observer)))
644 new_observer = false;
650 _observers.push_back(Engines::SalomeLauncherObserver::_duplicate(observer));
652 // We notify the new observer with all jobs that are currently in the Launcher
653 std::map<int, Launcher::Job *> cpp_jobs = _l.getJobs();
654 std::map<int, Launcher::Job *>::const_iterator it_job;
655 for(it_job = cpp_jobs.begin(); it_job != cpp_jobs.end(); it_job++)
657 int number = it_job->first;
658 std::ostringstream job_id;
662 observer->notify("NEW_JOB", job_id.str().c_str());
666 MESSAGE("Notify Observer, exception catch");
672 //=============================================================================
674 * Add a new observer to the launcher
676 //=============================================================================
678 SALOME_Launcher::removeObserver(Engines::SalomeLauncherObserver_ptr observer)
680 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
681 while(iter != _observers.end())
683 if (std::string(_orb->object_to_string(*iter)) ==
684 std::string(_orb->object_to_string(observer)))
687 iter =_observers.erase(iter++);
696 //=============================================================================
698 * Notify observers on a new event
700 //=============================================================================
702 SALOME_Launcher::notifyObservers(const std::string & event_name,
703 const std::string & event_data)
705 std::list<Engines::SalomeLauncherObserver_var>::iterator iter = _observers.begin();
706 while(iter != _observers.end())
710 (*iter)->notify(CORBA::string_dup(event_name.c_str()),
711 CORBA::string_dup(event_data.c_str()));
715 MESSAGE("Notify Observer, exception catch");