1 // Copyright (C) 2009-2017 CEA/DEN, EDF R&D, OPEN CASCADE
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, or (at your option) any later version.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // Author: André RIBES - EDF R&D
23 #include "Launcher_Job.hxx"
24 #include "Launcher.hxx"
25 #include <boost/filesystem.hpp>
28 #include <libbatch/Constants.hxx>
37 _launch_date = getLaunchDate();
43 _job_file_name_complete = "";
46 _local_directory = "";
47 _result_directory = "";
48 _maximum_duration = "";
49 _maximum_duration_in_second = -1;
55 // Parameters for COORM
60 _batch_job = new Batch::Job();
66 LAUNCHER_MESSAGE("Deleting job number: " << _number);
74 Launcher::Job::stopJob()
76 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
79 if (_batch_job_id.getReference() != "undefined")
83 _batch_job_id.deleteJob();
85 catch (const Batch::GenericException &ex)
87 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
95 Launcher::Job::removeJob()
97 LAUNCHER_MESSAGE("Removing job number: " << _number);
99 if (_batch_job_id.getReference() != "undefined")
103 _batch_job_id.deleteJob();
105 catch (const Batch::GenericException &ex)
107 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
114 Launcher::Job::getJobType() const
120 Launcher::Job::setJobName(const std::string & job_name)
122 _job_name = job_name;
126 Launcher::Job::getJobName() const
132 Launcher::Job::setState(const std::string & state)
134 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
135 if (state != "CREATED" &&
136 state != "IN_PROCESS" &&
138 state != "RUNNING" &&
140 state != "FINISHED" &&
144 throw LauncherException("Bad state, this state does not exist: " + state);
150 Launcher::Job::getState() const
155 // Get names or ids of hosts assigned to the job
157 Launcher::Job::getAssignedHostnames()
159 return _assigned_hostnames;
163 Launcher::Job::setNumber(const int & number)
166 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
171 Launcher::Job::getNumber()
177 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
179 // Check machine_definition
180 std::string user_name = "";
181 if (resource_definition.UserName == "")
183 user_name = getenv("USER");
185 user_name = getenv("LOGNAME");
188 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
189 throw LauncherException(mess);
193 user_name = resource_definition.UserName;
195 _resource_definition = resource_definition;
196 _resource_definition.UserName = user_name;
200 Launcher::Job::getResourceDefinition() const
202 return _resource_definition;
206 Launcher::Job::setJobFile(const std::string & job_file)
211 std::string mess = "Empty Job File is forbidden !";
212 throw LauncherException(mess);
215 _job_file = job_file;
216 std::string::size_type p1 = _job_file.find_last_of("/");
217 std::string::size_type p2 = _job_file.find_last_of(".");
218 _job_file_name_complete = _job_file.substr(p1+1);
219 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
223 Launcher::Job::getJobFile() const
228 Launcher::Job::setEnvFile(const std::string & env_file)
230 _env_file = env_file;
234 Launcher::Job::getEnvFile() const
240 Launcher::Job::setWorkDirectory(const std::string & work_directory)
242 _work_directory = work_directory;
246 Launcher::Job::setLocalDirectory(const std::string & local_directory)
248 _local_directory = local_directory;
252 Launcher::Job::setResultDirectory(const std::string & result_directory)
254 _result_directory = result_directory;
258 Launcher::Job::add_in_file(const std::string & file)
260 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
261 if (it == _in_files.end())
262 _in_files.push_back(file);
264 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
268 Launcher::Job::add_out_file(const std::string & file)
270 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
271 if (it == _out_files.end())
272 _out_files.push_back(file);
274 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
278 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
280 checkMaximumDuration(maximum_duration);
281 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
282 _maximum_duration = maximum_duration;
287 Launcher::Job::setLauncherFile(const std::string & launcher_file)
289 _launcher_file = launcher_file;
292 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
294 _launcher_args = launcher_args;
298 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
300 checkResourceRequiredParams(resource_required_params);
301 _resource_required_params = resource_required_params;
305 Launcher::Job::setQueue(const std::string & queue)
311 Launcher::Job::setExclusive(bool exclusive)
313 _exclusive = exclusive;
317 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
319 if (exclusiveStr == "true")
321 else if (exclusiveStr == "false")
324 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
328 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
330 _mem_per_cpu = mem_per_cpu;
334 Launcher::Job::setWCKey(const std::string & wckey)
340 Launcher::Job::setExtraParams(const std::string & extra_params)
342 _extra_params = extra_params;
346 Launcher::Job::setReference(const std::string & reference)
348 _reference = reference;
352 Launcher::Job::getWorkDirectory() const
354 return _work_directory;
358 Launcher::Job::getLocalDirectory() const
360 return _local_directory;
364 Launcher::Job::getResultDirectory() const
366 return _result_directory;
369 const std::list<std::string> &
370 Launcher::Job::get_in_files() const
375 const std::list<std::string> &
376 Launcher::Job::get_out_files() const
382 Launcher::Job::getMaximumDuration() const
384 return _maximum_duration;
389 Launcher::Job::getLauncherFile() const
391 return _launcher_file;
394 Launcher::Job::getLauncherArgs() const
396 return _launcher_args;
400 Launcher::Job::getResourceRequiredParams() const
402 return _resource_required_params;
406 Launcher::Job::getQueue() const
412 Launcher::Job::getExclusive() const
418 Launcher::Job::getExclusiveStr() const
420 return _exclusive ? "true" : "false";
424 Launcher::Job::getMemPerCpu() const
430 Launcher::Job::getWCKey() const
436 Launcher::Job::getExtraParams() const
438 return _extra_params;
442 Launcher::Job::getReference() const
448 Launcher::Job::setPreCommand(const std::string & preCommand)
450 _pre_command = preCommand;
454 Launcher::Job::getPreCommand() const
460 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
462 std::string result("");
463 std::string edt_value = maximum_duration;
464 std::size_t pos = edt_value.find(":");
466 if (edt_value != "") {
467 if (pos == edt_value.npos) {
468 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
470 std::string begin_edt_value = edt_value.substr(0, pos);
471 std::string mid_edt_value = edt_value.substr(pos, 1);
472 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
475 std::istringstream iss(begin_edt_value);
476 if (!(iss >> value)) {
477 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
479 else if (value < 0) {
480 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
482 std::istringstream iss_2(end_edt_value);
483 if (!(iss_2 >> value)) {
484 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
486 else if (value < 0) {
487 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
489 if (mid_edt_value != ":") {
490 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
494 throw LauncherException(result);
498 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
500 // nb_proc has be to > 0
501 if (resource_required_params.nb_proc <= 0)
503 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
504 throw LauncherException(message);
509 Launcher::Job::convertMaximumDuration(const std::string & edt)
513 if( edt.size() == 0 )
516 std::string::size_type pos = edt.find(":");
517 std::string h = edt.substr(0,pos);
518 std::string m = edt.substr(pos+1,edt.size()-pos+1);
519 std::istringstream issh(h);
521 std::istringstream issm(m);
530 Launcher::Job::getLaunchDate() const
534 std::string launch_date = ctime(&rawtime);
536 for (;i < launch_date.size(); i++)
537 if (launch_date[i] == '/' ||
538 launch_date[i] == '-' ||
539 launch_date[i] == ':' ||
540 launch_date[i] == ' ')
541 launch_date[i] = '_';
542 launch_date.erase(--launch_date.end()); // Last caracter is a \n
548 Launcher::Job::updateJobState()
551 if (_state != "FINISHED" &&
556 if (_batch_job_id.getReference() != "undefined")
558 // A batch manager has been affected to the job
559 Batch::JobInfo job_info = _batch_job_id.queryJob();
560 Batch::Parametre par = job_info.getParametre();
561 _state = par[Batch::STATE].str();
562 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
563 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
564 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
573 Launcher::Job::getBatchJob()
580 Launcher::Job::common_job_params()
582 Batch::Parametre params;
584 params[Batch::NAME] = getJobName();
585 params[Batch::NBPROC] = _resource_required_params.nb_proc;
586 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
588 // Memory in megabytes
589 if (_resource_required_params.mem_mb > 0)
591 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
593 else if (_mem_per_cpu > 0)
595 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
598 // We define a default directory
599 if (_work_directory == "")
601 const size_t BUFSIZE = 32;
603 time_t curtime = time(NULL);
604 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
605 if(!_resource_definition.working_directory.empty())
607 std::string date_dir = std::string("/job_") + date;
608 std::ostringstream str_pid;
609 str_pid << ::getpid();
610 std::string job_dir = date_dir + "-" + str_pid.str();
612 _work_directory = _resource_definition.working_directory + job_dir;
616 _work_directory = std::string("/$HOME/Batch/workdir_");
617 _work_directory += date;
620 params[Batch::WORKDIR] = _work_directory;
621 std::string libbatch_pre_command("");
622 if(!_pre_command.empty())
624 boost::filesystem::path pre_command_path(_pre_command);
625 libbatch_pre_command += "./" + pre_command_path.filename().string();
627 params[Batch::PREPROCESS] = libbatch_pre_command;
629 // Parameters for COORM
630 params[Batch::LAUNCHER_FILE] = _launcher_file;
631 params[Batch::LAUNCHER_ARGS] = _launcher_args;
633 // If result_directory is not defined, we use HOME environnement
634 if (_result_directory == "")
635 _result_directory = getenv("HOME");
638 std::list<std::string> in_files(_in_files);
639 in_files.push_back(_job_file);
641 in_files.push_back(_env_file);
642 if(!_pre_command.empty())
643 in_files.push_back(_pre_command);
644 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
646 std::string file = *it;
648 // local file -> If file is not an absolute path, we apply _local_directory
649 std::string local_file;
650 if (file.substr(0, 1) == std::string("/"))
654 local_file = _local_directory + "/" + file;
659 // remote file -> get only file name from in_files
660 size_t found = file.find_last_of("/");
661 std::string remote_file = _work_directory + "/" + file.substr(found+1);
663 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
667 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
669 std::string file = *it;
670 // remote file -> If file is not an absolute path, we apply _work_directory
671 std::string remote_file;
672 std::string local_file;
673 if (file.substr(0, 1) == std::string("/"))
676 size_t found = file.find_last_of("/");
677 local_file = file.substr(found+1);
681 remote_file = _work_directory + "/" + file;
685 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
689 if (_maximum_duration_in_second != -1)
690 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
694 params[Batch::QUEUE] = _queue;
698 params[Batch::EXCLUSIVE] = true;
702 params[Batch::WCKEY] = _wckey;
705 if (_extra_params != "")
706 params[Batch::EXTRAPARAMS] = _extra_params;
708 // Specific parameters
709 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
710 if (it != _specific_parameters.end())
711 params["LL_JOBTYPE"] = it->second;
716 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
718 _batch_job_id = batch_manager_job_id;
722 Launcher::Job::getBatchManagerJobId() const
724 return _batch_job_id;
729 Launcher::Job::addSpecificParameter(const std::string & name,
730 const std::string & value)
732 _specific_parameters[name] = value;
735 const std::map<std::string, std::string> &
736 Launcher::Job::getSpecificParameters() const
738 return _specific_parameters;
742 Launcher::Job::checkSpecificParameters()