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>
33 static const char SEPARATOR = '\\';
35 static const char SEPARATOR = '/';
42 _launch_date = getLaunchDate();
48 _job_file_name_complete = "";
51 _local_directory = "";
52 _result_directory = "";
53 _maximum_duration = "";
54 _maximum_duration_in_second = -1;
61 // Parameters for COORM
66 _batch_job = new Batch::Job();
72 LAUNCHER_MESSAGE("Deleting job number: " << _number);
80 Launcher::Job::stopJob()
82 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
85 if (_batch_job_id.getReference() != "undefined")
89 _batch_job_id.deleteJob();
91 catch (const Batch::GenericException &ex)
93 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
101 Launcher::Job::removeJob()
103 LAUNCHER_MESSAGE("Removing job number: " << _number);
105 if (_batch_job_id.getReference() != "undefined")
109 _batch_job_id.deleteJob();
111 catch (const Batch::GenericException &ex)
113 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
120 Launcher::Job::getJobType() const
126 Launcher::Job::setJobName(const std::string & job_name)
128 _job_name = job_name;
132 Launcher::Job::getJobName() const
138 Launcher::Job::setState(const std::string & state)
140 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
141 if (state != "CREATED" &&
142 state != "IN_PROCESS" &&
144 state != "RUNNING" &&
146 state != "FINISHED" &&
150 throw LauncherException("Bad state, this state does not exist: " + state);
156 Launcher::Job::getState() const
161 // Get names or ids of hosts assigned to the job
163 Launcher::Job::getAssignedHostnames()
165 return _assigned_hostnames;
169 Launcher::Job::setNumber(const int & number)
172 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
177 Launcher::Job::getNumber()
183 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
185 // Check machine_definition
186 std::string user_name = "";
187 if (resource_definition.UserName == "")
190 user_name = getenv("USER");
192 user_name = getenv("USERNAME");
195 user_name = getenv("LOGNAME");
198 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
199 throw LauncherException(mess);
203 user_name = resource_definition.UserName;
205 _resource_definition = resource_definition;
206 _resource_definition.UserName = user_name;
210 Launcher::Job::getResourceDefinition() const
212 return _resource_definition;
216 Launcher::Job::setJobFile(const std::string & job_file)
221 std::string mess = "Empty Job File is forbidden !";
222 throw LauncherException(mess);
225 _job_file = job_file;
226 std::string::size_type p1 = _job_file.find_last_of(SEPARATOR);
227 std::string::size_type p2 = _job_file.find_last_of(".");
228 _job_file_name_complete = _job_file.substr(p1+1);
229 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
233 Launcher::Job::getJobFile() const
238 Launcher::Job::setEnvFile(const std::string & env_file)
240 _env_file = env_file;
244 Launcher::Job::getEnvFile() const
250 Launcher::Job::setWorkDirectory(const std::string & work_directory)
252 _work_directory = work_directory;
256 Launcher::Job::setLocalDirectory(const std::string & local_directory)
258 _local_directory = local_directory;
262 Launcher::Job::setResultDirectory(const std::string & result_directory)
264 _result_directory = result_directory;
268 Launcher::Job::add_in_file(const std::string & file)
270 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
271 if (it == _in_files.end())
272 _in_files.push_back(file);
274 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
278 Launcher::Job::add_out_file(const std::string & file)
280 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
281 if (it == _out_files.end())
282 _out_files.push_back(file);
284 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
288 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
290 checkMaximumDuration(maximum_duration);
291 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
292 _maximum_duration = maximum_duration;
297 Launcher::Job::setLauncherFile(const std::string & launcher_file)
299 _launcher_file = launcher_file;
302 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
304 _launcher_args = launcher_args;
308 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
310 checkResourceRequiredParams(resource_required_params);
311 _resource_required_params = resource_required_params;
315 Launcher::Job::setQueue(const std::string & queue)
321 Launcher::Job::setPartition(const std::string & partition)
323 _partition = partition;
327 Launcher::Job::setExclusive(bool exclusive)
329 _exclusive = exclusive;
333 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
335 if (exclusiveStr == "true")
337 else if (exclusiveStr == "false")
340 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
344 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
346 _mem_per_cpu = mem_per_cpu;
350 Launcher::Job::setWCKey(const std::string & wckey)
356 Launcher::Job::setExtraParams(const std::string & extra_params)
358 _extra_params = extra_params;
362 Launcher::Job::setReference(const std::string & reference)
364 _reference = reference;
368 Launcher::Job::getWorkDirectory() const
370 return _work_directory;
374 Launcher::Job::getLocalDirectory() const
376 return _local_directory;
380 Launcher::Job::getResultDirectory() const
382 return _result_directory;
385 const std::list<std::string> &
386 Launcher::Job::get_in_files() const
391 const std::list<std::string> &
392 Launcher::Job::get_out_files() const
398 Launcher::Job::getMaximumDuration() const
400 return _maximum_duration;
405 Launcher::Job::getLauncherFile() const
407 return _launcher_file;
410 Launcher::Job::getLauncherArgs() const
412 return _launcher_args;
416 Launcher::Job::getResourceRequiredParams() const
418 return _resource_required_params;
422 Launcher::Job::getQueue() const
428 Launcher::Job::getPartition() const
434 Launcher::Job::getExclusive() const
440 Launcher::Job::getExclusiveStr() const
442 return _exclusive ? "true" : "false";
446 Launcher::Job::getMemPerCpu() const
452 Launcher::Job::getWCKey() const
458 Launcher::Job::getExtraParams() const
460 return _extra_params;
464 Launcher::Job::getReference() const
470 Launcher::Job::setPreCommand(const std::string & preCommand)
472 _pre_command = preCommand;
476 Launcher::Job::getPreCommand() const
482 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
484 std::string result("");
485 std::string edt_value = maximum_duration;
486 std::size_t pos = edt_value.find(":");
488 if (edt_value != "") {
489 if (pos == edt_value.npos) {
490 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
492 std::string begin_edt_value = edt_value.substr(0, pos);
493 std::string mid_edt_value = edt_value.substr(pos, 1);
494 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
497 std::istringstream iss(begin_edt_value);
498 if (!(iss >> value)) {
499 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
501 else if (value < 0) {
502 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
504 std::istringstream iss_2(end_edt_value);
505 if (!(iss_2 >> value)) {
506 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
508 else if (value < 0) {
509 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
511 if (mid_edt_value != ":") {
512 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
516 throw LauncherException(result);
520 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
522 // nb_proc has be to > 0
523 if (resource_required_params.nb_proc <= 0)
525 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
526 throw LauncherException(message);
531 Launcher::Job::convertMaximumDuration(const std::string & edt)
535 if( edt.size() == 0 )
538 std::string::size_type pos = edt.find(":");
539 std::string h = edt.substr(0,pos);
540 std::string m = edt.substr(pos+1,edt.size()-pos+1);
541 std::istringstream issh(h);
543 std::istringstream issm(m);
552 Launcher::Job::getLaunchDate() const
556 std::string launch_date = ctime(&rawtime);
558 for (;i < launch_date.size(); i++)
559 if (launch_date[i] == '/' ||
560 launch_date[i] == '-' ||
561 launch_date[i] == ':' ||
562 launch_date[i] == ' ')
563 launch_date[i] = '_';
564 launch_date.erase(--launch_date.end()); // Last character is a \n
570 Launcher::Job::updateJobState()
573 if (_state != "FINISHED" &&
578 if (_batch_job_id.getReference() != "undefined")
580 // A batch manager has been affected to the job
581 Batch::JobInfo job_info = _batch_job_id.queryJob();
582 Batch::Parametre par = job_info.getParametre();
583 _state = par[Batch::STATE].str();
584 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
585 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
586 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
595 Launcher::Job::getBatchJob()
602 Launcher::Job::common_job_params()
604 Batch::Parametre params;
606 params[Batch::NAME] = getJobName();
607 params[Batch::NBPROC] = _resource_required_params.nb_proc;
608 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
610 if(_resource_required_params.nb_node > 0)
611 params[Batch::NBNODE] = _resource_required_params.nb_node;
613 // Memory in megabytes
614 if (_resource_required_params.mem_mb > 0)
616 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
618 else if (_mem_per_cpu > 0)
620 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
623 // We define a default directory
624 if (_work_directory == "")
626 const size_t BUFSIZE = 32;
628 time_t curtime = time(NULL);
629 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
630 if(!_resource_definition.working_directory.empty())
632 std::string date_dir = std::string("/job_") + date;
633 std::ostringstream str_pid;
634 str_pid << ::getpid();
635 std::string job_dir = date_dir + "-" + str_pid.str();
637 _work_directory = _resource_definition.working_directory + job_dir;
642 _work_directory = std::string("/$HOME/Batch/workdir_");
644 _work_directory = std::string("%USERPROFILE%\\Batch\\workdir_");
646 _work_directory += date;
649 params[Batch::WORKDIR] = _work_directory;
650 std::string libbatch_pre_command("");
651 if(!_pre_command.empty())
653 boost::filesystem::path pre_command_path(_pre_command);
654 libbatch_pre_command += "./" + pre_command_path.filename().string();
656 params[Batch::PREPROCESS] = libbatch_pre_command;
658 // Parameters for COORM
659 params[Batch::LAUNCHER_FILE] = _launcher_file;
660 params[Batch::LAUNCHER_ARGS] = _launcher_args;
662 // If result_directory is not defined, we use HOME environment
663 if (_result_directory == "")
665 _result_directory = getenv("HOME");
667 _result_directory = getenv("USERPROFILE");
670 std::list<std::string> in_files(_in_files);
671 in_files.push_back(_job_file);
673 in_files.push_back(_env_file);
674 if(!_pre_command.empty())
675 in_files.push_back(_pre_command);
676 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
678 std::string file = *it;
680 // local file -> If file is not an absolute path, we apply _local_directory
681 std::string local_file;
683 if (file.substr(0, 1) == std::string("/"))
685 // On Windows, absolute paths may begin with something like "C:"
686 if (file.substr(1, 1) == std::string(":"))
689 else if (file.substr(0, 1) == std::string("-")) // using rsync options
693 // '/./' is used by rsync to find the root of the relative path
694 // /a/b/./c/f -> _working_directory/c/f
695 local_file = _local_directory + "/./" + file;
697 local_file = _local_directory + SEPARATOR + file;
700 // remote file -> get only file name from in_files
701 std::string remote_file = _work_directory + "/";
703 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
707 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
709 std::string file = *it;
710 // remote file -> If file is not an absolute path, we apply _work_directory
711 std::string remote_file;
712 std::string local_file;
713 if (file.substr(0, 1) == std::string("/"))
716 size_t found = file.find_last_of("/");
717 local_file = file.substr(found+1);
719 else if (file.substr(0, 1) == std::string("-")) // using rsync options
726 // '/./' is used by rsync to find the root of the relative path
727 remote_file = _work_directory + "/./" + file;
731 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
735 if (_maximum_duration_in_second != -1)
736 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
740 params[Batch::QUEUE] = _queue;
743 if (_partition != "")
744 params[Batch::PARTITION] = _partition;
748 params[Batch::EXCLUSIVE] = true;
752 params[Batch::WCKEY] = _wckey;
755 if (_extra_params != "")
756 params[Batch::EXTRAPARAMS] = _extra_params;
758 // Specific parameters
759 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
760 if (it != _specific_parameters.end())
761 params["LL_JOBTYPE"] = it->second;
766 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
768 _batch_job_id = batch_manager_job_id;
772 Launcher::Job::getBatchManagerJobId() const
774 return _batch_job_id;
779 Launcher::Job::addSpecificParameter(const std::string & name,
780 const std::string & value)
782 _specific_parameters[name] = value;
785 const std::map<std::string, std::string> &
786 Launcher::Job::getSpecificParameters() const
788 return _specific_parameters;
792 Launcher::Job::checkSpecificParameters()