Salome HOME
Fix for previous integration: no more error message when a batch manager does not...
[modules/kernel.git] / src / Launcher / Launcher_Job.cxx
1 // Copyright (C) 2009-2013  CEA/DEN, EDF R&D, OPEN CASCADE
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 // Author: AndrĂ© RIBES - EDF R&D
21 //
22 #include "Launcher_Job.hxx"
23 #include "Launcher.hxx"
24
25 #ifdef WITH_LIBBATCH
26 #include <libbatch/Constants.hxx>
27 #endif
28
29 Launcher::Job::Job()
30 {
31   _number = -1;
32   _state = "CREATED";
33   _launch_date = getLaunchDate();
34
35   _env_file = "";
36   _job_name = "";
37   _job_file = "";
38   _job_file_name = "";
39   _job_file_name_complete = "";
40   _work_directory = "";
41   _local_directory = "";
42   _result_directory = "";
43   _maximum_duration = "";
44   _maximum_duration_in_second = -1;
45   _resource_required_params.name = "";
46   _resource_required_params.hostname = "";
47   _resource_required_params.OS = "";
48   _resource_required_params.nb_proc = -1;
49   _resource_required_params.nb_node = -1;
50   _resource_required_params.nb_proc_per_node = -1;
51   _resource_required_params.cpu_clock = -1;
52   _resource_required_params.mem_mb = -1;
53   _queue = "";
54   _job_type = "";
55
56   // Parameters for COORM
57   _launcher_file = "";
58   _launcher_args = "";
59
60 #ifdef WITH_LIBBATCH
61   _batch_job = new Batch::Job();
62 #endif
63 }
64
65 Launcher::Job::~Job() 
66 {
67   LAUNCHER_MESSAGE("Deleting job number: " << _number);
68 #ifdef WITH_LIBBATCH
69   if (_batch_job)
70     delete _batch_job;
71 #endif
72 }
73
74 void
75 Launcher::Job::stopJob()
76 {
77   LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
78   setState("FAILED");
79 #ifdef WITH_LIBBATCH
80   if (_batch_job_id.getReference() != "undefined")
81   {
82     try 
83     {
84       _batch_job_id.deleteJob();
85     }
86     catch (const Batch::GenericException &ex)
87     {
88       LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
89     }
90   }
91 #endif
92 }
93
94
95 void
96 Launcher::Job::removeJob()
97 {
98   LAUNCHER_MESSAGE("Removing job number: " << _number);
99 #ifdef WITH_LIBBATCH
100   if (_batch_job_id.getReference() != "undefined")
101   {
102     try 
103     {
104       _batch_job_id.deleteJob();
105     }
106     catch (const Batch::GenericException &ex)
107     {
108       LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
109     }
110   }
111 #endif
112 }
113
114 std::string
115 Launcher::Job::getJobType()
116 {
117   return _job_type;
118 }
119
120 void
121 Launcher::Job::setJobName(const std::string & job_name)
122 {
123   _job_name = job_name;
124 }
125
126 std::string
127 Launcher::Job::getJobName()
128 {
129   return _job_name;
130 }
131
132 void 
133 Launcher::Job::setState(const std::string & state)
134 {
135   // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
136   if (state != "CREATED" &&
137       state != "IN_PROCESS" &&
138       state != "QUEUED" &&
139       state != "RUNNING" &&
140       state != "PAUSED" &&
141       state != "FINISHED" &&
142       state != "FAILED" &&
143       state != "ERROR")
144   {
145     throw LauncherException("Bad state, this state does not exist: " + state);
146   }
147   _state = state;
148 }
149
150 std::string 
151 Launcher::Job::getState()
152 {
153   return _state;
154 }
155
156 // Get names or ids of hosts assigned to the job
157 std::string
158 Launcher::Job::getAssignedHostnames()
159 {
160   return _assigned_hostnames;
161 }
162
163 void 
164 Launcher::Job::setNumber(const int & number)
165 {
166   if (_number != -1)
167     std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
168   _number = number;
169 }
170
171 int
172 Launcher::Job::getNumber()
173 {
174   return _number;
175 }
176
177 void 
178 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
179 {
180   // Check machine_definition
181   std::string user_name = "";
182   if (resource_definition.UserName == "")
183   {
184     user_name = getenv("USER");
185     if (user_name == "")
186     {
187       std::string mess = "You must define a user name: into your resource description or with env variable USER";
188       throw LauncherException(mess);
189     }
190   }
191   else
192     user_name = resource_definition.UserName;
193
194   _resource_definition = resource_definition;
195   _resource_definition.UserName = user_name;
196 }
197
198 ParserResourcesType 
199 Launcher::Job::getResourceDefinition()
200 {
201   return _resource_definition;
202 }
203
204 void 
205 Launcher::Job::setJobFile(const std::string & job_file)
206 {
207   // Check job file
208   if (job_file == "")
209   {
210     std::string mess = "Empty Job File is forbidden !";
211     throw LauncherException(mess);
212   }
213
214   _job_file = job_file;
215   std::string::size_type p1 = _job_file.find_last_of("/");
216   std::string::size_type p2 = _job_file.find_last_of(".");
217   _job_file_name_complete = _job_file.substr(p1+1);
218   _job_file_name = _job_file.substr(p1+1,p2-p1-1);
219 }
220
221 std::string
222 Launcher::Job::getJobFile()
223 {
224   return _job_file;
225 }
226 void 
227 Launcher::Job::setEnvFile(const std::string & env_file)
228 {
229   _env_file = env_file;
230 }
231
232 std::string
233 Launcher::Job::getEnvFile()
234 {
235   return _env_file;
236 }
237
238 void 
239 Launcher::Job::setWorkDirectory(const std::string & work_directory)
240 {
241   _work_directory = work_directory;
242 }
243
244 void 
245 Launcher::Job::setLocalDirectory(const std::string & local_directory)
246 {
247   _local_directory = local_directory;
248 }
249
250 void 
251 Launcher::Job::setResultDirectory(const std::string & result_directory)
252 {
253   _result_directory = result_directory;
254 }
255
256 void 
257 Launcher::Job::add_in_file(const std::string & file)
258 {
259   std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
260   if (it == _in_files.end())
261     _in_files.push_back(file);
262   else
263     std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
264 }
265
266 void 
267 Launcher::Job::add_out_file(const std::string & file)
268 {
269   std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
270   if (it == _out_files.end())
271     _out_files.push_back(file);
272   else
273     std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
274 }
275
276 void 
277 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
278 {
279   checkMaximumDuration(maximum_duration);
280   _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
281   _maximum_duration = maximum_duration;
282 }
283
284 // For COORM
285 void
286 Launcher::Job::setLauncherFile(const std::string & launcher_file)
287 {
288         _launcher_file = launcher_file;
289 }
290 void
291 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
292 {
293         _launcher_args = launcher_args;
294 }
295
296 void 
297 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
298 {
299   checkResourceRequiredParams(resource_required_params);
300   _resource_required_params = resource_required_params;
301 }
302
303 void 
304 Launcher::Job::setQueue(const std::string & queue)
305 {
306   _queue = queue;
307 }
308
309 std::string 
310 Launcher::Job::getWorkDirectory()
311 {
312   return _work_directory;
313 }
314
315 std::string 
316 Launcher::Job::getLocalDirectory()
317 {
318   return _local_directory;
319 }
320
321 std::string
322 Launcher::Job::getResultDirectory()
323 {
324   return _result_directory;
325 }
326
327 const std::list<std::string> & 
328 Launcher::Job::get_in_files()
329 {
330   return _in_files;
331 }
332
333 const std::list<std::string> & 
334 Launcher::Job::get_out_files()
335 {
336   return _out_files;
337 }
338
339 std::string 
340 Launcher::Job::getMaximumDuration()
341 {
342   return _maximum_duration;
343 }
344
345 // For COORM
346 std::string
347 Launcher::Job::getLauncherFile()
348 {
349         return _launcher_file;
350 }
351 std::string
352 Launcher::Job::getLauncherArgs()
353 {
354         return _launcher_args;
355 }
356
357 resourceParams 
358 Launcher::Job::getResourceRequiredParams()
359 {
360   return _resource_required_params;
361 }
362
363 std::string 
364 Launcher::Job::getQueue()
365 {
366   return _queue;
367 }
368
369 void 
370 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
371 {
372   std::string result("");
373   std::string edt_value = maximum_duration;
374   std::size_t pos = edt_value.find(":");
375
376   if (edt_value != "") {
377     if (pos == edt_value.npos) {
378       throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
379     }
380     std::string begin_edt_value = edt_value.substr(0, pos);
381     std::string mid_edt_value = edt_value.substr(pos, 1);
382     std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
383   
384     long value;
385     std::istringstream iss(begin_edt_value);
386     if (!(iss >> value)) {
387       result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
388     }
389     else if (value < 0) {
390       result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
391     }
392     std::istringstream iss_2(end_edt_value);
393     if (!(iss_2 >> value)) {
394       result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
395     }
396     else if (value < 0) {
397       result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
398     }
399     if (mid_edt_value != ":") {
400       result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
401     }
402   }
403   if (result != "")
404     throw LauncherException(result);
405 }
406
407 void 
408 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
409 {
410   // nb_proc has be to > 0
411   if (resource_required_params.nb_proc <= 0)
412   {
413     std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
414     throw LauncherException(message);
415   }
416 }
417
418 long 
419 Launcher::Job::convertMaximumDuration(const std::string & edt)
420 {
421   long hh, mm, ret;
422
423   if( edt.size() == 0 )
424     return -1;
425
426   std::string::size_type pos = edt.find(":");
427   std::string h = edt.substr(0,pos);
428   std::string m = edt.substr(pos+1,edt.size()-pos+1);
429   std::istringstream issh(h);
430   issh >> hh;
431   std::istringstream issm(m);
432   issm >> mm;
433   ret = hh*60 + mm;
434   ret = ret * 60;
435
436   return ret;
437 }
438
439 std::string 
440 Launcher::Job::getLaunchDate()
441 {
442   time_t rawtime;
443   time(&rawtime);
444   std::string launch_date = ctime(&rawtime);
445   int i = 0 ;
446   for (;i < launch_date.size(); i++) 
447     if (launch_date[i] == '/' ||
448         launch_date[i] == '-' ||
449         launch_date[i] == ':' ||
450         launch_date[i] == ' ') 
451       launch_date[i] = '_';
452   launch_date.erase(--launch_date.end()); // Last caracter is a \n
453
454   return launch_date;
455 }
456
457 std::string
458 Launcher::Job::updateJobState()
459 {
460
461   if (_state != "FINISHED" &&
462       _state != "ERROR"    &&
463       _state != "FAILED")
464   {
465 #ifdef WITH_LIBBATCH
466     if (_batch_job_id.getReference() != "undefined")
467     {
468       // A batch manager has been affected to the job
469       Batch::JobInfo job_info = _batch_job_id.queryJob();
470       Batch::Parametre par = job_info.getParametre();
471       _state = par[Batch::STATE].str();
472       _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
473                             "" : par[Batch::ASSIGNEDHOSTNAMES].str();
474       LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
475     }
476 #endif
477   }
478   return _state;
479 }
480
481 #ifdef WITH_LIBBATCH
482 Batch::Job * 
483 Launcher::Job::getBatchJob()
484 {
485   update_job();
486   return _batch_job;
487 }
488
489 Batch::Parametre
490 Launcher::Job::common_job_params()
491 {
492   Batch::Parametre params;
493
494   params[Batch::NAME] = getJobName();
495   params[Batch::NBPROC] = _resource_required_params.nb_proc;
496   params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
497
498   // Memory in megabytes
499   if (_resource_required_params.mem_mb > 0)
500   {
501     params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
502   }
503
504   // We define a default directory based on user time
505   if (_work_directory == "")
506   {
507     const size_t BUFSIZE = 32;
508     char date[BUFSIZE];
509     time_t curtime = time(NULL);
510     strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
511     _work_directory = std::string("$HOME/Batch/workdir_");
512     _work_directory += date;
513   }
514   params[Batch::WORKDIR] = _work_directory;
515
516   // Parameters for COORM
517   params[Batch::LAUNCHER_FILE] = _launcher_file;
518   params[Batch::LAUNCHER_ARGS] = _launcher_args;
519
520   // If result_directory is not defined, we use HOME environnement
521   if (_result_directory == "")
522     _result_directory = getenv("HOME");
523
524   // _in_files
525   std::list<std::string> in_files(_in_files);
526   in_files.push_back(_job_file);
527   if (_env_file != "")
528           in_files.push_back(_env_file);
529   for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
530   {
531     std::string file = *it;
532
533     // local file -> If file is not an absolute path, we apply _local_directory
534     std::string local_file;
535     if (file.substr(0, 1) == std::string("/"))
536       local_file = file;
537     else
538 #ifndef WIN32
539       local_file = _local_directory + "/" + file;
540 #else
541           local_file = file;
542 #endif
543     
544     // remote file -> get only file name from in_files
545     size_t found = file.find_last_of("/");
546     std::string remote_file = _work_directory + "/" + file.substr(found+1);
547
548     params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
549   }
550    
551   // _out_files
552   for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
553   {
554     std::string file = *it;
555
556     // local file 
557     size_t found = file.find_last_of("/");
558     std::string local_file = _result_directory +  "/" + file.substr(found+1);
559
560     // remote file -> If file is not an absolute path, we apply _work_directory
561     std::string remote_file;
562     if (file.substr(0, 1) == std::string("/"))
563       remote_file = file;
564     else
565       remote_file = _work_directory + "/" + file;
566
567     params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
568   }
569
570   // Time
571   if (_maximum_duration_in_second != -1)
572     params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
573
574   // Queue
575   if (_queue != "")
576     params[Batch::QUEUE] = _queue;
577
578   // Specific parameters
579   std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
580   if (it != _specific_parameters.end())
581     params["LL_JOBTYPE"] = it->second;
582   return params;
583 }
584
585 void 
586 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
587 {
588   _batch_job_id = batch_manager_job_id;
589 }
590
591 Batch::JobId 
592 Launcher::Job::getBatchManagerJobId()
593 {
594   return _batch_job_id;
595 }
596 #endif
597
598 void
599 Launcher::Job::addToXmlDocument(xmlNodePtr root_node)
600 {
601   // Begin job
602   xmlNodePtr job_node = xmlNewChild(root_node, NULL, xmlCharStrdup("job"), NULL);
603   xmlNewProp(job_node, xmlCharStrdup("type"), xmlCharStrdup(getJobType().c_str()));
604   xmlNewProp(job_node, xmlCharStrdup("name"), xmlCharStrdup(getJobName().c_str()));
605
606   // Add user part
607   xmlNodePtr node = xmlNewChild(job_node, NULL, xmlCharStrdup("user_part"), NULL);
608
609   xmlNewChild(node, NULL, xmlCharStrdup("job_file"),         xmlCharStrdup(getJobFile().c_str()));
610   xmlNewChild(node, NULL, xmlCharStrdup("env_file"),         xmlCharStrdup(getEnvFile().c_str()));
611   xmlNewChild(node, NULL, xmlCharStrdup("work_directory"),   xmlCharStrdup(getWorkDirectory().c_str()));
612   xmlNewChild(node, NULL, xmlCharStrdup("local_directory"),  xmlCharStrdup(getLocalDirectory().c_str()));
613   xmlNewChild(node, NULL, xmlCharStrdup("result_directory"), xmlCharStrdup(getResultDirectory().c_str()));
614
615   // Parameters for COORM
616   xmlNewChild(node, NULL, xmlCharStrdup("launcher_file"), xmlCharStrdup(getLauncherFile().c_str()));
617
618   // Files
619   xmlNodePtr files_node = xmlNewChild(node, NULL, xmlCharStrdup("files"), NULL);
620   std::list<std::string> in_files  = get_in_files();
621   std::list<std::string> out_files = get_out_files();
622   for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
623     xmlNewChild(files_node, NULL, xmlCharStrdup("in_file"), xmlCharStrdup((*it).c_str()));
624   for(std::list<std::string>::iterator it = out_files.begin(); it != out_files.end(); it++)
625     xmlNewChild(files_node, NULL, xmlCharStrdup("out_file"), xmlCharStrdup((*it).c_str()));
626
627   // Resource part
628   resourceParams resource_params = getResourceRequiredParams();
629   xmlNodePtr res_node = xmlNewChild(node, NULL, xmlCharStrdup("resource_params"), NULL);
630   xmlNewChild(res_node, NULL, xmlCharStrdup("name"),   xmlCharStrdup(resource_params.name.c_str()));
631   xmlNewChild(res_node, NULL, xmlCharStrdup("hostname"),   xmlCharStrdup(resource_params.hostname.c_str()));
632   xmlNewChild(res_node, NULL, xmlCharStrdup("OS"),   xmlCharStrdup(resource_params.OS.c_str()));
633   std::ostringstream nb_proc_stream;
634   std::ostringstream nb_node_stream;
635   std::ostringstream nb_proc_per_node_stream;
636   std::ostringstream cpu_clock_stream;
637   std::ostringstream mem_mb_stream;
638   nb_proc_stream << resource_params.nb_proc;
639   nb_node_stream << resource_params.nb_node;
640   nb_proc_per_node_stream << resource_params.nb_proc_per_node;
641   cpu_clock_stream << resource_params.cpu_clock;
642   mem_mb_stream << resource_params.mem_mb;
643   xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc"),            xmlCharStrdup(nb_proc_stream.str().c_str()));
644   xmlNewChild(res_node, NULL, xmlCharStrdup("nb_node"),            xmlCharStrdup(nb_node_stream.str().c_str()));
645   xmlNewChild(res_node, NULL, xmlCharStrdup("nb_proc_per_node"),   xmlCharStrdup(nb_proc_per_node_stream.str().c_str()));
646   xmlNewChild(res_node, NULL, xmlCharStrdup("cpu_clock"),          xmlCharStrdup(cpu_clock_stream.str().c_str()));
647   xmlNewChild(res_node, NULL, xmlCharStrdup("mem_mb"),             xmlCharStrdup(mem_mb_stream.str().c_str()));
648
649   xmlNewChild(node, NULL, xmlCharStrdup("maximum_duration"), xmlCharStrdup(getMaximumDuration().c_str()));
650   xmlNewChild(node, NULL, xmlCharStrdup("queue"),            xmlCharStrdup(getQueue().c_str()));
651
652   // For COORM
653   xmlNewChild(node, NULL, xmlCharStrdup("launcher_args"), xmlCharStrdup(getLauncherArgs().c_str()));
654
655   // Specific parameters part
656   xmlNodePtr specific_parameters_node = xmlNewChild(node, NULL, xmlCharStrdup("specific_parameters"), NULL);
657   std::map<std::string, std::string> specific_parameters = getSpecificParameters();
658   for(std::map<std::string, std::string>::iterator it = specific_parameters.begin(); it != specific_parameters.end(); it++)
659   {
660     xmlNodePtr specific_parameter_node = xmlNewChild(specific_parameters_node, NULL, xmlCharStrdup("specific_parameter"), NULL);
661     xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("name"), xmlCharStrdup((it->first).c_str()));
662     xmlNewChild(specific_parameter_node, NULL, xmlCharStrdup("value"), xmlCharStrdup((it->second).c_str()));
663   }
664
665   // Run part
666   xmlNodePtr run_node = xmlNewChild(job_node, NULL, xmlCharStrdup("run_part"), NULL);
667   xmlNewChild(run_node, NULL, xmlCharStrdup("job_state"), xmlCharStrdup(getState().c_str()));
668   ParserResourcesType resource_definition = getResourceDefinition();
669   xmlNewChild(run_node, NULL, xmlCharStrdup("resource_choosed_name"), xmlCharStrdup(resource_definition.Name.c_str()));
670
671 #ifdef WITH_LIBBATCH
672   Batch::JobId job_id = getBatchManagerJobId();
673   xmlNewChild(run_node, NULL, xmlCharStrdup("job_reference"), xmlCharStrdup(job_id.getReference().c_str()));
674 #endif
675 }
676
677 void 
678 Launcher::Job::addSpecificParameter(const std::string & name,
679                                       const std::string & value)
680 {
681   _specific_parameters[name] = value;
682 }
683
684 const std::map<std::string, std::string> &
685 Launcher::Job::getSpecificParameters()
686 {
687   return _specific_parameters;
688 }
689
690 void
691 Launcher::Job::checkSpecificParameters()
692 {
693 }