]> SALOME platform Git repositories - modules/jobmanager.git/blob - src/engine/BL_SALOMEServices.cxx
Salome HOME
06e1a1d522a245bfabc063810cb2128969a3477a
[modules/jobmanager.git] / src / engine / BL_SALOMEServices.cxx
1 // Copyright (C) 2009-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BL_SALOMEServices.hxx"
21 #include <sstream>
22
23 static std::ostream & 
24 operator<<(std::ostream & os, const CORBA::Exception & e)
25 {
26   CORBA::Any tmp;
27   tmp <<=e ;
28   CORBA::TypeCode_var tc = tmp.type();
29   const char * p = tc->name ();
30   if (*p != '\0')
31     os << p;
32   else
33     os << tc->id();
34   return os;
35 }
36
37 BL::SALOMEServices::SALOMEServices()
38 {
39   int nbargs = 0; char **args = 0;
40   _orb = CORBA::ORB_init (nbargs, args);
41   _salome_naming_service = NULL;
42   _lcc = NULL;
43   _state = false;
44   _manager = NULL;
45 }
46
47 BL::SALOMEServices::~SALOMEServices() {}
48
49 void
50 BL::SALOMEServices::end()
51 {
52   if (!CORBA::is_nil(_salome_launcher))
53     _salome_launcher->removeObserver(_this());
54   if (_salome_naming_service)
55     delete _salome_naming_service;
56   if (_lcc)
57     delete _lcc;
58 }
59
60 bool
61 BL::SALOMEServices::initNS()
62 {
63   bool return_value = true;
64   _salome_naming_service = new SALOME_NamingService(_orb);
65   _lcc = new SALOME_LifeCycleCORBA(_salome_naming_service);
66   CORBA::Object_var obj = _salome_naming_service->Resolve("/SalomeLauncher");
67   _salome_launcher = Engines::SalomeLauncher::_narrow(obj);
68
69   if (CORBA::is_nil(_salome_launcher))
70   {
71     DEBMSG("SALOME Launcher is not reachable!")
72     return_value = false;
73   }
74   _salome_launcher->addObserver(_this());
75   _remove_ref(); // POA will automatically destroy the object
76
77   obj = _salome_naming_service->Resolve("/ResourcesManager");
78   _resources_manager = Engines::ResourcesManager::_narrow(obj);
79   if (CORBA::is_nil(_resources_manager))
80   {
81     DEBMSG("SALOME Resource Manager is not reachable !");
82     return_value = false;
83   }
84
85   _state = return_value;
86   return return_value;
87 }
88
89 std::list<std::string> 
90 BL::SALOMEServices::getResourceList(bool batch_only)
91 {
92   std::list<std::string> resource_list;
93   
94   if (_state)
95   {
96     Engines::ResourceParameters params;
97     _lcc->preSet(params);
98     params.can_launch_batch_jobs = batch_only;
99     Engines::ResourceList * resourceList = NULL;
100     try
101     {
102       resourceList = _resources_manager->GetFittingResources(params);
103     }
104     catch (const SALOME::SALOME_Exception & ex)
105     {
106       DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
107     }
108     catch (const CORBA::SystemException& ex)
109     {
110       DEBMSG("Receive SALOME System Exception: " << ex);
111       DEBMSG("Check SALOME servers...");
112     }
113     if (resourceList)
114     {
115       for (int i = 0; i < resourceList->length(); i++)
116       {
117         const char* aResource = (*resourceList)[i];
118         resource_list.push_back(aResource);
119       }
120       delete resourceList;
121     }
122   }
123   return resource_list;
124 }
125
126 BL::ResourceDescr
127 BL::SALOMEServices::getResourceDescr(const std::string& name)
128 {
129   Engines::ResourceDefinition * resource_definition = NULL;
130   BL::ResourceDescr resource_descr;
131
132   try 
133   {
134     resource_definition = _resources_manager-> GetResourceDefinition(name.c_str());
135   }
136   catch (const SALOME::SALOME_Exception & ex)
137   {
138     DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
139   }
140   catch (const CORBA::SystemException& ex)
141   {
142     DEBMSG("Receive SALOME System Exception: " << ex);
143     DEBMSG("Check SALOME servers...");
144   }
145
146   if(resource_definition)
147   {
148     resource_descr.name = resource_definition->name.in();
149     resource_descr.hostname = resource_definition->hostname.in();
150     resource_descr.protocol = resource_definition->protocol.in();
151     resource_descr.username = resource_definition->username.in();
152     resource_descr.applipath = resource_definition->applipath.in();
153     for (int i = 0; i < resource_definition->componentList.length(); i++)
154     {
155       resource_descr.componentList.push_back(resource_definition->componentList[i].in());
156     }
157
158     resource_descr.OS = resource_definition->OS.in();
159     resource_descr.mem_mb = resource_definition->mem_mb;
160     resource_descr.cpu_clock = resource_definition->cpu_clock;
161     resource_descr.nb_node = resource_definition->nb_node;
162     resource_descr.nb_proc_per_node = resource_definition->nb_proc_per_node;
163     resource_descr.batch = resource_definition->batch.in();
164     resource_descr.mpiImpl = resource_definition->mpiImpl.in();
165     resource_descr.iprotocol = resource_definition->iprotocol.in();
166     resource_descr.can_launch_batch_jobs = resource_definition->can_launch_batch_jobs;
167     resource_descr.can_run_containers = resource_definition->can_run_containers;
168     resource_descr.working_directory = resource_definition->working_directory.in();
169
170     delete resource_definition;
171   }
172   return resource_descr;
173 }
174
175 void
176 BL::SALOMEServices::addResource(BL::ResourceDescr & new_resource)
177 {
178   Engines::ResourceDefinition_var resource_definition = new Engines::ResourceDefinition;
179
180   resource_definition->name = CORBA::string_dup(new_resource.name.c_str());
181   resource_definition->hostname = CORBA::string_dup(new_resource.hostname.c_str());
182   if (new_resource.batch == "" || new_resource.batch == "ssh_batch")
183     resource_definition->type = CORBA::string_dup("single_machine");
184   else
185     resource_definition->type = CORBA::string_dup("cluster");
186   resource_definition->protocol = CORBA::string_dup(new_resource.protocol.c_str());
187   resource_definition->username = CORBA::string_dup(new_resource.username.c_str());
188   resource_definition->applipath = CORBA::string_dup(new_resource.applipath.c_str());
189
190   int i = 0;
191   std::list<std::string>::iterator it = new_resource.componentList.begin();
192   resource_definition->componentList.length(new_resource.componentList.size());
193   for(; it != new_resource.componentList.end(); it++)
194   {
195     resource_definition->componentList[i] = CORBA::string_dup((*it).c_str());
196     i++;
197   }
198
199   resource_definition->OS = CORBA::string_dup(new_resource.OS.c_str());
200   resource_definition->mem_mb = new_resource.mem_mb;
201   resource_definition->cpu_clock = new_resource.cpu_clock;
202   resource_definition->nb_node = new_resource.nb_node;
203   resource_definition->nb_proc_per_node = new_resource.nb_proc_per_node;  
204   resource_definition->batch = CORBA::string_dup(new_resource.batch.c_str());
205   resource_definition->mpiImpl = CORBA::string_dup(new_resource.mpiImpl.c_str());
206   resource_definition->iprotocol = CORBA::string_dup(new_resource.iprotocol.c_str());
207   resource_definition->can_launch_batch_jobs = new_resource.can_launch_batch_jobs;
208   resource_definition->can_run_containers = new_resource.can_run_containers;
209   resource_definition->working_directory = CORBA::string_dup(new_resource.working_directory.c_str());
210
211   try
212   {
213     _resources_manager->AddResource(resource_definition, true, "");
214   }
215   catch (const SALOME::SALOME_Exception & ex)
216   {
217     DEBMSG("SALOME Exception in addResource ! " << ex.details.text.in());
218   }
219   catch (const CORBA::SystemException& ex)
220   {
221     DEBMSG("Receive SALOME System Exception: " << ex);
222     DEBMSG("Check SALOME servers...");
223   }
224 }
225
226 void 
227 BL::SALOMEServices::removeResource(const std::string & name)
228 {
229   try
230   {
231     _resources_manager->RemoveResource(name.c_str(), true, "");
232   }
233   catch (const SALOME::SALOME_Exception & ex)
234   {
235     DEBMSG("SALOME Exception in removeResource ! " << ex.details.text.in());
236   }
237   catch (const CORBA::SystemException& ex)
238   {
239     DEBMSG("Receive SALOME System Exception: " << ex);
240     DEBMSG("Check SALOME servers...");
241   }
242 }
243
244 std::string
245 BL::SALOMEServices::create_job(BL::Job * job)
246 {
247   DEBMSG("Begin of create_job");
248   std::string ret = "";
249   Engines::JobParameters_var job_parameters = new Engines::JobParameters;
250
251   // Job type
252   if (job->getType() == BL::Job::COMMAND)
253   {
254     job_parameters->job_type = CORBA::string_dup("command");
255   }
256   else if (job->getType() == BL::Job::YACS_SCHEMA)
257   {
258     job_parameters->job_type = CORBA::string_dup("yacs_file");
259   }
260   else if (job->getType() == BL::Job::PYTHON_SALOME)
261   {
262     job_parameters->job_type = CORBA::string_dup("python_salome");
263   }
264
265   // Specific parameters
266   if (job->getType() == BL::Job::YACS_SCHEMA)
267   {
268     if (job->getDumpYACSState() > 0)
269     {
270       job_parameters->specific_parameters.length(job_parameters->specific_parameters.length() + 1);
271       std::ostringstream oss;
272       oss << job->getDumpYACSState();
273       Engines::Parameter_var new_parameter = new Engines::Parameter;
274       new_parameter->name = CORBA::string_dup("EnableDumpYACS");
275       new_parameter->value = CORBA::string_dup(oss.str().c_str());
276       job_parameters->specific_parameters[job_parameters->specific_parameters.length() - 1] = new_parameter;
277     }
278   }
279   if (job->getLoadLevelerJobType() != "")
280   {
281     job_parameters->specific_parameters.length(job_parameters->specific_parameters.length() + 1);
282     Engines::Parameter_var new_parameter = new Engines::Parameter;
283     new_parameter->name = CORBA::string_dup("LoalLevelerJobType");
284     new_parameter->value = CORBA::string_dup(job->getLoadLevelerJobType().c_str());
285     job_parameters->specific_parameters[job_parameters->specific_parameters.length() - 1] = new_parameter;
286   }
287
288   // Files
289   job_parameters->job_name = CORBA::string_dup(job->getName().c_str());
290   job_parameters->job_file = CORBA::string_dup(job->getJobFile().c_str());
291   job_parameters->env_file = CORBA::string_dup(job->getEnvFile().c_str());
292   BL::Job::FilesParam files = job->getFilesParameters();
293   std::list<std::string>::iterator it;
294   int i = 0;
295   int j = 0;
296
297   job_parameters->in_files.length(files.input_files_list.size());
298   for (it = files.input_files_list.begin() ; it != files.input_files_list.end(); it++)
299   {
300     job_parameters->in_files[i] = CORBA::string_dup((*it).c_str());
301     i++;
302   }
303
304   job_parameters->out_files.length(files.output_files_list.size());
305   for (it = files.output_files_list.begin() ; it != files.output_files_list.end(); it++)
306   {
307     job_parameters->out_files[j] = CORBA::string_dup((*it).c_str());
308     j++;
309   }
310   job_parameters->local_directory = CORBA::string_dup("");
311   job_parameters->result_directory = CORBA::string_dup(files.result_directory.c_str());
312
313   BL::Job::BatchParam cpp_batch_params =  job->getBatchParameters();
314   job_parameters->work_directory = CORBA::string_dup(cpp_batch_params.batch_directory.c_str());
315
316   // Resource
317   job_parameters->maximum_duration = CORBA::string_dup(cpp_batch_params.maximum_duration.c_str());
318   job_parameters->resource_required.name = CORBA::string_dup(job->getResource().c_str());
319   job_parameters->resource_required.nb_proc = cpp_batch_params.nb_proc;
320   job_parameters->queue = CORBA::string_dup(job->getBatchQueue().c_str());
321
322   // Memory
323   CORBA::Long memory;
324   std::string ram = cpp_batch_params.expected_memory.substr(0,cpp_batch_params.expected_memory.size()-2);
325   std::istringstream iss(ram);
326   iss >> memory;
327   std::string unity = cpp_batch_params.expected_memory.substr(cpp_batch_params.expected_memory.size()-2, 2);
328   if((unity.find("gb") != std::string::npos))
329     memory = memory * 1024;
330   job_parameters->resource_required.mem_mb = memory;
331
332   // Create Job
333   try
334   {
335     int job_id = _salome_launcher->createJob(job_parameters);
336     job->setSalomeLauncherId(job_id);
337   }
338   catch (const SALOME::SALOME_Exception & ex)
339   {
340     DEBMSG("SALOME Exception in createJob !");
341     ret = ex.details.text.in();
342   }
343   catch (const CORBA::SystemException& ex)
344   {
345     DEBMSG("Receive SALOME System Exception: " << ex);
346     DEBMSG("Check SALOME servers...");
347     ret = "SALOME System Exception - see logs";
348   }
349   return ret;
350 }
351
352 std::string
353 BL::SALOMEServices::start_job(BL::Job * job)
354 {
355   std::string ret = "";
356   // Launch Job !
357   try
358   {
359     _salome_launcher->launchJob(job->getSalomeLauncherId());
360   }
361   catch (const SALOME::SALOME_Exception & ex)
362   {
363     DEBMSG("SALOME Exception in launchJob !");
364     ret = ex.details.text.in();
365   }
366   catch (const CORBA::SystemException& ex)
367   {
368     DEBMSG("Receive SALOME System Exception: " << ex);
369     DEBMSG("Check SALOME servers...");
370     ret = "SALOME System Exception - see logs";
371   }
372   return ret;
373 }
374
375 std::string
376 BL::SALOMEServices::refresh_job(BL::Job * job)
377 {
378   std::string ret = "";
379
380   // Refresh Job !
381   try
382   {
383     CORBA::String_var result = _salome_launcher->getJobState(job->getSalomeLauncherId());
384     ret = result.in();
385   }
386   catch (const SALOME::SALOME_Exception & ex)
387   {
388     DEBMSG("SALOME Exception in getJobState !");
389     ret = ex.details.text.in();
390   }
391   catch (const CORBA::SystemException& ex)
392   {
393     DEBMSG("Receive SALOME System Exception: " << ex);
394     DEBMSG("Check SALOME servers...");
395     ret = "SALOME System Exception - see logs";
396   }
397   return ret;
398 }
399
400 std::string
401 BL::SALOMEServices::delete_job(BL::Job * job)
402 {
403   std::string ret = "";
404   // Delete Job !
405   try
406   {
407     _salome_launcher->removeJob(job->getSalomeLauncherId());
408   }
409   catch (const SALOME::SALOME_Exception & ex)
410   {
411     DEBMSG("SALOME Exception in removeJob !");
412     ret = ex.details.text.in();
413   }
414   catch (const CORBA::SystemException& ex)
415   {
416     DEBMSG("Receive SALOME System Exception: " << ex);
417     DEBMSG("Check SALOME servers...");
418     ret = "SALOME System Exception - see logs";
419   }
420   return ret;
421 }
422
423 std::string
424 BL::SALOMEServices::stop_job(BL::Job * job)
425 {
426   std::string ret = "";
427   try
428   {
429     _salome_launcher->stopJob(job->getSalomeLauncherId());
430   }
431   catch (const SALOME::SALOME_Exception & ex)
432   {
433     DEBMSG("SALOME Exception in stopJob !");
434     ret = ex.details.text.in();
435   }
436   catch (const CORBA::SystemException& ex)
437   {
438     DEBMSG("Receive SALOME System Exception: " << ex);
439     DEBMSG("Check SALOME servers...");
440     ret = "SALOME System Exception - see logs";
441   }
442   return ret;
443 }
444
445 std::string
446 BL::SALOMEServices::get_results_job(BL::Job * job)
447 {
448   std::string ret = "";
449
450   BL::Job::FilesParam files = job->getFilesParameters();
451   CORBA::String_var directory = CORBA::string_dup(files.result_directory.c_str());
452
453   // get job results !
454   try
455   {
456     _salome_launcher->getJobResults(job->getSalomeLauncherId(), directory);
457   }
458   catch (const SALOME::SALOME_Exception & ex)
459   {
460     DEBMSG("SALOME Exception in refresh_job !");
461     ret = ex.details.text.in();
462   }
463   catch (const CORBA::SystemException& ex)
464   {
465     DEBMSG("Receive SALOME System Exception: " << ex);
466     DEBMSG("Check SALOME servers...");
467     ret = "SALOME System Exception - see logs";
468   }
469   return ret;
470 }
471
472 std::string
473 BL::SALOMEServices::save_jobs(const std::string & file_name)
474 {
475   CORBA::String_var file = CORBA::string_dup(file_name.c_str());
476   std::string ret = "";
477   try
478   {
479     _salome_launcher->saveJobs(file);
480   }
481   catch (const SALOME::SALOME_Exception & ex)
482   {
483     DEBMSG("SALOME Exception in saveJobs !");
484     ret = ex.details.text.in();
485   }
486   catch (const CORBA::SystemException& ex)
487   {
488     DEBMSG("Receive CORBA System Exception: " << ex);
489     DEBMSG("Check SALOME servers...");
490     ret = "CORBA System Exception - see SALOME logs";
491   }
492   return ret;
493 }
494
495 std::string
496 BL::SALOMEServices::load_jobs(const std::string & file_name)
497 {
498   CORBA::String_var file = CORBA::string_dup(file_name.c_str());
499   std::string ret = "";
500   try
501   {
502     _salome_launcher->loadJobs(file);
503   }
504   catch (const SALOME::SALOME_Exception & ex)
505   {
506     DEBMSG("SALOME Exception in loadJobs !");
507     ret = ex.details.text.in();
508   }
509   catch (const CORBA::SystemException& ex)
510   {
511     DEBMSG("Receive CORBA System Exception: " << ex);
512     DEBMSG("Check SALOME servers...");
513     ret = "CORBA System Exception - see SALOME logs";
514   }
515   return ret;
516 }
517
518 void
519 BL::SALOMEServices::notify(const char* event_name, const char * event_data)
520 {
521   DEBMSG("Launcher event received " << event_name << " " << event_data);
522
523   std::string event(event_name);
524   std::string data(event_data);
525
526   if (event == "SAVE_JOBS")
527   {
528     _manager->launcher_event_save_jobs(data);
529   }
530   else if (event == "LOAD_JOBS")
531   {
532     _manager->launcher_event_load_jobs(data);
533   }
534   else if (event == "NEW_JOB")
535   {
536     _manager->launcher_event_new_job(data);
537   }
538   else if (event == "REMOVE_JOB")
539   {
540     _manager->launcher_event_remove_job(data);
541   }
542   else if (event == "UPDATE_JOB_STATE")
543   {
544     _manager->launcher_event_update_job_state(data);
545   }
546   else
547   {
548     DEBMSG("Unkown launcher event received");
549   }
550 }
551
552 BL::Job * 
553 BL::SALOMEServices::get_new_job(int job_number)
554 {
555   DEBMSG("Start of BL::SALOMEServices::get_new_job");
556   BL::Job * job_return = NULL;
557   Engines::JobParameters * job_parameters = NULL;
558   try
559   {
560     job_parameters = _salome_launcher->getJobParameters(job_number);
561   }
562   catch (const SALOME::SALOME_Exception & ex)
563   {
564     DEBMSG("SALOME Exception in saveJobs !");
565   }
566   catch (const CORBA::SystemException& ex)
567   {
568     DEBMSG("Receive CORBA System Exception: " << ex);
569     DEBMSG("Check SALOME servers...");
570   }
571
572   if (job_parameters)
573   {
574     job_return = new BL::Job();
575     job_return->setSalomeLauncherId(job_number);
576
577     job_return->setName(job_parameters->job_name.in());
578     job_return->setType(job_parameters->job_type.in());
579     job_return->setJobFile(job_parameters->job_file.in());
580     job_return->setEnvFile(job_parameters->env_file.in());
581     job_return->setBatchQueue(job_parameters->queue.in());
582
583     BL::Job::FilesParam param;
584     param.result_directory = job_parameters->result_directory.in();
585     for (CORBA::ULong i = 0; i < job_parameters->in_files.length(); i++)
586       param.input_files_list.push_back(job_parameters->in_files[i].in());
587     for (CORBA::ULong i = 0; i < job_parameters->out_files.length(); i++)
588       param.output_files_list.push_back(job_parameters->out_files[i].in());
589     job_return->setFilesParameters(param);
590
591     BL::Job::BatchParam batch_param;
592     batch_param.batch_directory = job_parameters->work_directory.in();
593     batch_param.maximum_duration = job_parameters->maximum_duration.in();
594     batch_param.nb_proc = job_parameters->resource_required.nb_proc;
595     std::ostringstream mem_stream;
596     mem_stream << job_parameters->resource_required.mem_mb << "mb";
597     batch_param.expected_memory = mem_stream.str();
598     job_return->setBatchParameters(batch_param);
599
600     job_return->setResource(job_parameters->resource_required.name.in());
601
602     // Specific parameters
603     for (CORBA::ULong i = 0; i < job_parameters->specific_parameters.length(); i++)
604     {
605       if (std::string(job_parameters->specific_parameters[i].name.in()) == "EnableDumpYACS")
606       {
607         std::string user_value = job_parameters->specific_parameters[i].value.in();
608         std::istringstream iss(user_value);
609         int value;
610         iss >> value;
611         job_return->setDumpYACSState(value);
612       }
613       if (std::string(job_parameters->specific_parameters[i].name.in()) == "LoalLevelerJobType")
614       {
615         std::string user_value = job_parameters->specific_parameters[i].value.in();
616         job_return->setLoadLevelerJobType(user_value);
617       }
618     }
619
620     // Get current state
621     std::string result_job = job_return->setStringState(refresh_job(job_return));
622     if (result_job != "RefreshError") {}
623     else
624     {
625       // Error in getting state
626       DEBMSG("Error in getting state of the new job!");
627       delete job_return;
628       job_return = NULL;
629     }
630     delete job_parameters;
631   }
632
633   return job_return;
634 }