1 // Copyright (C) 2009-2019 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 = '\\';
36 static const char SEPARATOR = '/';
43 _launch_date = getLaunchDate();
49 _job_file_name_complete = "";
52 _local_directory = "";
53 _result_directory = "";
54 _maximum_duration = "";
55 _maximum_duration_in_second = -1;
62 // Parameters for COORM
67 _batch_job = new Batch::Job();
73 LAUNCHER_MESSAGE("Deleting job number: " << _number);
80 bool Launcher::Job::decrRef() const
82 bool ret=((--_cnt)==0);
88 void Launcher::Job::incrRef() const
94 Launcher::Job::stopJob()
96 LAUNCHER_MESSAGE("Stop resquested for 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 stopping the job: " << ex.message);
115 Launcher::Job::removeJob()
117 LAUNCHER_MESSAGE("Removing job number: " << _number);
119 if (_batch_job_id.getReference() != "undefined")
123 _batch_job_id.deleteJob();
125 catch (const Batch::GenericException &ex)
127 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
134 Launcher::Job::getJobType() const
140 Launcher::Job::setJobName(const std::string & job_name)
142 _job_name = job_name;
146 Launcher::Job::getJobName() const
152 Launcher::Job::setState(const std::string & state)
154 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
155 if (state != "CREATED" &&
156 state != "IN_PROCESS" &&
158 state != "RUNNING" &&
160 state != "FINISHED" &&
164 throw LauncherException("Bad state, this state does not exist: " + state);
170 Launcher::Job::getState() const
175 // Get names or ids of hosts assigned to the job
177 Launcher::Job::getAssignedHostnames()
179 return _assigned_hostnames;
183 Launcher::Job::setNumber(const int & number)
186 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
191 Launcher::Job::getNumber()
197 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
199 // Check machine_definition
200 std::string user_name = "";
201 if (resource_definition.UserName == "")
204 user_name = getenv("USER");
206 user_name = getenv("USERNAME");
209 user_name = getenv("LOGNAME");
212 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
213 throw LauncherException(mess);
217 user_name = resource_definition.UserName;
219 _resource_definition = resource_definition;
220 _resource_definition.UserName = user_name;
224 Launcher::Job::getResourceDefinition() const
226 return _resource_definition;
230 Launcher::Job::setJobFile(const std::string & job_file)
235 std::string mess = "Empty Job File is forbidden !";
236 throw LauncherException(mess);
239 _job_file = job_file;
240 std::string::size_type p1 = _job_file.find_last_of(SEPARATOR);
241 std::string::size_type p2 = _job_file.find_last_of(".");
242 _job_file_name_complete = _job_file.substr(p1+1);
243 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
247 Launcher::Job::getJobFile() const
252 Launcher::Job::setEnvFile(const std::string & env_file)
254 _env_file = env_file;
258 Launcher::Job::getEnvFile() const
264 Launcher::Job::setWorkDirectory(const std::string & work_directory)
266 _work_directory = work_directory;
270 Launcher::Job::setLocalDirectory(const std::string & local_directory)
272 _local_directory = local_directory;
276 Launcher::Job::setResultDirectory(const std::string & result_directory)
278 _result_directory = result_directory;
282 Launcher::Job::add_in_file(const std::string & file)
284 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
285 if (it == _in_files.end())
286 _in_files.push_back(file);
288 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
292 Launcher::Job::add_out_file(const std::string & file)
294 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
295 if (it == _out_files.end())
296 _out_files.push_back(file);
298 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
302 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
304 checkMaximumDuration(maximum_duration);
305 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
306 _maximum_duration = maximum_duration;
311 Launcher::Job::setLauncherFile(const std::string & launcher_file)
313 _launcher_file = launcher_file;
316 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
318 _launcher_args = launcher_args;
322 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
324 checkResourceRequiredParams(resource_required_params);
325 _resource_required_params = resource_required_params;
329 Launcher::Job::setQueue(const std::string & queue)
335 Launcher::Job::setPartition(const std::string & partition)
337 _partition = partition;
341 Launcher::Job::setExclusive(bool exclusive)
343 _exclusive = exclusive;
347 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
349 if (exclusiveStr == "true")
351 else if (exclusiveStr == "false")
354 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
358 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
360 _mem_per_cpu = mem_per_cpu;
364 Launcher::Job::setWCKey(const std::string & wckey)
370 Launcher::Job::setExtraParams(const std::string & extra_params)
372 _extra_params = extra_params;
376 Launcher::Job::setReference(const std::string & reference)
378 _reference = reference;
382 Launcher::Job::getWorkDirectory() const
384 return _work_directory;
388 Launcher::Job::getLocalDirectory() const
390 return _local_directory;
394 Launcher::Job::getResultDirectory() const
396 return _result_directory;
399 const std::list<std::string> &
400 Launcher::Job::get_in_files() const
405 const std::list<std::string> &
406 Launcher::Job::get_out_files() const
412 Launcher::Job::getMaximumDuration() const
414 return _maximum_duration;
419 Launcher::Job::getLauncherFile() const
421 return _launcher_file;
424 Launcher::Job::getLauncherArgs() const
426 return _launcher_args;
430 Launcher::Job::getResourceRequiredParams() const
432 return _resource_required_params;
436 Launcher::Job::getQueue() const
442 Launcher::Job::getPartition() const
448 Launcher::Job::getExclusive() const
454 Launcher::Job::getExclusiveStr() const
456 return _exclusive ? "true" : "false";
460 Launcher::Job::getMemPerCpu() const
466 Launcher::Job::getWCKey() const
472 Launcher::Job::getExtraParams() const
474 return _extra_params;
478 Launcher::Job::getReference() const
484 Launcher::Job::setPreCommand(const std::string & preCommand)
486 _pre_command = preCommand;
490 Launcher::Job::getPreCommand() const
496 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
498 std::string result("");
499 std::string edt_value = maximum_duration;
500 std::size_t pos = edt_value.find(":");
502 if (edt_value != "") {
503 if (pos == edt_value.npos) {
504 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
506 std::string begin_edt_value = edt_value.substr(0, pos);
507 std::string mid_edt_value = edt_value.substr(pos, 1);
508 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
511 std::istringstream iss(begin_edt_value);
512 if (!(iss >> value)) {
513 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
515 else if (value < 0) {
516 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
518 std::istringstream iss_2(end_edt_value);
519 if (!(iss_2 >> value)) {
520 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
522 else if (value < 0) {
523 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
525 if (mid_edt_value != ":") {
526 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
530 throw LauncherException(result);
534 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
536 // nb_proc has be to > 0
537 if (resource_required_params.nb_proc <= 0)
539 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
540 throw LauncherException(message);
545 Launcher::Job::convertMaximumDuration(const std::string & edt)
549 if( edt.size() == 0 )
552 std::string::size_type pos = edt.find(":");
553 std::string h = edt.substr(0,pos);
554 std::string m = edt.substr(pos+1,edt.size()-pos+1);
555 std::istringstream issh(h);
557 std::istringstream issm(m);
566 Launcher::Job::getLaunchDate() const
570 std::string launch_date = ctime(&rawtime);
572 for (;i < launch_date.size(); i++)
573 if (launch_date[i] == '/' ||
574 launch_date[i] == '-' ||
575 launch_date[i] == ':' ||
576 launch_date[i] == ' ')
577 launch_date[i] = '_';
578 launch_date.erase(--launch_date.end()); // Last character is a \n
584 Launcher::Job::updateJobState()
587 if (_state != "FINISHED" &&
592 if (_batch_job_id.getReference() != "undefined")
594 // A batch manager has been affected to the job
595 Batch::JobInfo job_info = _batch_job_id.queryJob();
596 Batch::Parametre par = job_info.getParametre();
597 _state = par[Batch::STATE].str();
598 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
599 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
600 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
609 Launcher::Job::getBatchJob()
616 Launcher::Job::common_job_params()
618 Batch::Parametre params;
620 params[Batch::NAME] = getJobName();
621 params[Batch::NBPROC] = _resource_required_params.nb_proc;
622 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
624 if(_resource_required_params.nb_node > 0)
625 params[Batch::NBNODE] = _resource_required_params.nb_node;
627 // Memory in megabytes
628 if (_resource_required_params.mem_mb > 0)
630 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
632 else if (_mem_per_cpu > 0)
634 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
637 // We define a default directory
638 if (_work_directory == "")
640 const size_t BUFSIZE = 32;
642 time_t curtime = time(NULL);
643 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
644 if(!_resource_definition.working_directory.empty())
646 std::string date_dir = std::string("/job_") + date;
647 std::ostringstream str_pid;
649 str_pid << _getpid();
651 str_pid << ::getpid();
653 std::string job_dir = date_dir + "-" + str_pid.str();
655 _work_directory = _resource_definition.working_directory + job_dir;
660 _work_directory = std::string("/$HOME/Batch/workdir_");
662 _work_directory = std::string("%USERPROFILE%\\Batch\\workdir_");
664 _work_directory += date;
667 params[Batch::WORKDIR] = _work_directory;
668 std::string libbatch_pre_command("");
669 if(!_pre_command.empty())
671 boost::filesystem::path pre_command_path(_pre_command);
672 libbatch_pre_command += "./" + pre_command_path.filename().string();
674 params[Batch::PREPROCESS] = libbatch_pre_command;
676 // Parameters for COORM
677 params[Batch::LAUNCHER_FILE] = _launcher_file;
678 params[Batch::LAUNCHER_ARGS] = _launcher_args;
680 // If result_directory is not defined, we use HOME environment
681 if (_result_directory == "")
683 _result_directory = getenv("HOME");
685 _result_directory = getenv("USERPROFILE");
688 std::list<std::string> in_files(_in_files);
689 in_files.push_back(_job_file);
691 in_files.push_back(_env_file);
692 if(!_pre_command.empty())
693 in_files.push_back(_pre_command);
694 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
696 std::string file = *it;
698 // local file -> If file is not an absolute path, we apply _local_directory
699 std::string local_file;
701 if (file.substr(0, 1) == std::string("/"))
703 // On Windows, absolute paths may begin with something like "C:"
704 if (file.substr(1, 1) == std::string(":"))
707 else if (file.substr(0, 1) == std::string("-")) // using rsync options
711 // '/./' is used by rsync to find the root of the relative path
712 // /a/b/./c/f -> _working_directory/c/f
713 local_file = _local_directory + "/./" + file;
715 local_file = _local_directory + SEPARATOR + file;
718 // remote file -> get only file name from in_files
719 std::string remote_file = _work_directory + "/";
721 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
725 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
727 std::string file = *it;
728 // remote file -> If file is not an absolute path, we apply _work_directory
729 std::string remote_file;
730 std::string local_file;
731 if (file.substr(0, 1) == std::string("/"))
734 size_t found = file.find_last_of("/");
735 local_file = file.substr(found+1);
737 else if (file.substr(0, 1) == std::string("-")) // using rsync options
744 // '/./' is used by rsync to find the root of the relative path
745 remote_file = _work_directory + "/./" + file;
749 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
753 if (_maximum_duration_in_second != -1)
754 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
758 params[Batch::QUEUE] = _queue;
761 if (_partition != "")
762 params[Batch::PARTITION] = _partition;
766 params[Batch::EXCLUSIVE] = true;
770 params[Batch::WCKEY] = _wckey;
773 if (_extra_params != "")
774 params[Batch::EXTRAPARAMS] = _extra_params;
776 // Specific parameters
777 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
778 if (it != _specific_parameters.end())
779 params["LL_JOBTYPE"] = it->second;
784 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
786 _batch_job_id = batch_manager_job_id;
790 Launcher::Job::getBatchManagerJobId() const
792 return _batch_job_id;
797 Launcher::Job::addSpecificParameter(const std::string & name,
798 const std::string & value)
800 _specific_parameters[name] = value;
803 const std::map<std::string, std::string> &
804 Launcher::Job::getSpecificParameters() const
806 return _specific_parameters;
810 Launcher::Job::checkSpecificParameters()