1 // Copyright (C) 2009-2020 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);
81 Launcher::Job::stopJob()
83 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
86 if (_batch_job_id.getReference() != "undefined")
90 _batch_job_id.deleteJob();
92 catch (const Batch::GenericException &ex)
94 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
102 Launcher::Job::removeJob()
104 LAUNCHER_MESSAGE("Removing job number: " << _number);
106 if (_batch_job_id.getReference() != "undefined")
110 _batch_job_id.deleteJob();
112 catch (const Batch::GenericException &ex)
114 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
121 Launcher::Job::getJobType() const
127 Launcher::Job::setJobName(const std::string & job_name)
129 _job_name = job_name;
133 Launcher::Job::getJobName() const
139 Launcher::Job::setState(const std::string & state)
141 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
142 if (state != "CREATED" &&
143 state != "IN_PROCESS" &&
145 state != "RUNNING" &&
147 state != "FINISHED" &&
151 throw LauncherException("Bad state, this state does not exist: " + state);
157 Launcher::Job::getState() const
162 // Get names or ids of hosts assigned to the job
164 Launcher::Job::getAssignedHostnames()
166 return _assigned_hostnames;
170 Launcher::Job::setNumber(const int & number)
173 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
178 Launcher::Job::getNumber()
184 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
186 // Check machine_definition
187 std::string user_name = "";
188 if (resource_definition.UserName == "")
191 user_name = getenv("USER");
193 user_name = getenv("USERNAME");
196 user_name = getenv("LOGNAME");
199 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
200 throw LauncherException(mess);
204 user_name = resource_definition.UserName;
206 _resource_definition = resource_definition;
207 _resource_definition.UserName = user_name;
211 Launcher::Job::getResourceDefinition() const
213 return _resource_definition;
217 Launcher::Job::setJobFile(const std::string & job_file)
222 std::string mess = "Empty Job File is forbidden !";
223 throw LauncherException(mess);
226 _job_file = job_file;
227 std::string::size_type p1 = _job_file.find_last_of(SEPARATOR);
228 std::string::size_type p2 = _job_file.find_last_of(".");
229 _job_file_name_complete = _job_file.substr(p1+1);
230 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
234 Launcher::Job::getJobFile() const
239 Launcher::Job::setEnvFile(const std::string & env_file)
241 _env_file = env_file;
245 Launcher::Job::getEnvFile() const
251 Launcher::Job::setWorkDirectory(const std::string & work_directory)
253 _work_directory = work_directory;
257 Launcher::Job::setLocalDirectory(const std::string & local_directory)
259 _local_directory = local_directory;
263 Launcher::Job::setResultDirectory(const std::string & result_directory)
265 _result_directory = result_directory;
269 Launcher::Job::add_in_file(const std::string & file)
271 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
272 if (it == _in_files.end())
273 _in_files.push_back(file);
275 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
279 Launcher::Job::add_out_file(const std::string & file)
281 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
282 if (it == _out_files.end())
283 _out_files.push_back(file);
285 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
289 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
291 checkMaximumDuration(maximum_duration);
292 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
293 _maximum_duration = maximum_duration;
298 Launcher::Job::setLauncherFile(const std::string & launcher_file)
300 _launcher_file = launcher_file;
303 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
305 _launcher_args = launcher_args;
309 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
311 checkResourceRequiredParams(resource_required_params);
312 _resource_required_params = resource_required_params;
316 Launcher::Job::setQueue(const std::string & queue)
322 Launcher::Job::setPartition(const std::string & partition)
324 _partition = partition;
328 Launcher::Job::setExclusive(bool exclusive)
330 _exclusive = exclusive;
334 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
336 if (exclusiveStr == "true")
338 else if (exclusiveStr == "false")
341 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
345 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
347 _mem_per_cpu = mem_per_cpu;
351 Launcher::Job::setWCKey(const std::string & wckey)
357 Launcher::Job::setExtraParams(const std::string & extra_params)
359 _extra_params = extra_params;
363 Launcher::Job::setReference(const std::string & reference)
365 _reference = reference;
369 Launcher::Job::getWorkDirectory() const
371 return _work_directory;
375 Launcher::Job::getLocalDirectory() const
377 return _local_directory;
381 Launcher::Job::getResultDirectory() const
383 return _result_directory;
386 const std::list<std::string> &
387 Launcher::Job::get_in_files() const
392 const std::list<std::string> &
393 Launcher::Job::get_out_files() const
399 Launcher::Job::getMaximumDuration() const
401 return _maximum_duration;
406 Launcher::Job::getLauncherFile() const
408 return _launcher_file;
411 Launcher::Job::getLauncherArgs() const
413 return _launcher_args;
417 Launcher::Job::getResourceRequiredParams() const
419 return _resource_required_params;
423 Launcher::Job::getQueue() const
429 Launcher::Job::getPartition() const
435 Launcher::Job::getExclusive() const
441 Launcher::Job::getExclusiveStr() const
443 return _exclusive ? "true" : "false";
447 Launcher::Job::getMemPerCpu() const
453 Launcher::Job::getWCKey() const
459 Launcher::Job::getExtraParams() const
461 return _extra_params;
465 Launcher::Job::getReference() const
471 Launcher::Job::setPreCommand(const std::string & preCommand)
473 _pre_command = preCommand;
477 Launcher::Job::getPreCommand() const
483 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
485 std::string result("");
486 std::string edt_value = maximum_duration;
487 std::size_t pos = edt_value.find(":");
489 if (edt_value != "") {
490 if (pos == edt_value.npos) {
491 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
493 std::string begin_edt_value = edt_value.substr(0, pos);
494 std::string mid_edt_value = edt_value.substr(pos, 1);
495 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
498 std::istringstream iss(begin_edt_value);
499 if (!(iss >> value)) {
500 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
502 else if (value < 0) {
503 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
505 std::istringstream iss_2(end_edt_value);
506 if (!(iss_2 >> value)) {
507 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
509 else if (value < 0) {
510 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
512 if (mid_edt_value != ":") {
513 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
517 throw LauncherException(result);
521 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
523 // nb_proc has be to > 0
524 if (resource_required_params.nb_proc <= 0)
526 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
527 throw LauncherException(message);
532 Launcher::Job::convertMaximumDuration(const std::string & edt)
536 if( edt.size() == 0 )
539 std::string::size_type pos = edt.find(":");
540 std::string h = edt.substr(0,pos);
541 std::string m = edt.substr(pos+1,edt.size()-pos+1);
542 std::istringstream issh(h);
544 std::istringstream issm(m);
553 Launcher::Job::getLaunchDate() const
557 std::string launch_date = ctime(&rawtime);
559 for (;i < launch_date.size(); i++)
560 if (launch_date[i] == '/' ||
561 launch_date[i] == '-' ||
562 launch_date[i] == ':' ||
563 launch_date[i] == ' ')
564 launch_date[i] = '_';
565 launch_date.erase(--launch_date.end()); // Last character is a \n
571 Launcher::Job::updateJobState()
574 if (_state != "FINISHED" &&
579 if (_batch_job_id.getReference() != "undefined")
581 // A batch manager has been affected to the job
582 Batch::JobInfo job_info = _batch_job_id.queryJob();
583 Batch::Parametre par = job_info.getParametre();
584 _state = par[Batch::STATE].str();
585 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
586 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
587 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
596 Launcher::Job::getBatchJob()
603 Launcher::Job::common_job_params()
605 Batch::Parametre params;
607 params[Batch::NAME] = getJobName();
608 params[Batch::NBPROC] = _resource_required_params.nb_proc;
609 if(_resource_required_params.nb_proc_per_node > 0)
610 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
612 if(_resource_required_params.nb_node > 0)
613 params[Batch::NBNODE] = _resource_required_params.nb_node;
615 // Memory in megabytes
616 if (_resource_required_params.mem_mb > 0)
618 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
620 else if (_mem_per_cpu > 0)
622 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
625 // We define a default directory
626 if (_work_directory == "")
628 const size_t BUFSIZE = 32;
630 time_t curtime = time(NULL);
631 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
632 if(!_resource_definition.working_directory.empty())
634 std::string date_dir = std::string("/job_") + date;
635 std::ostringstream str_pid;
637 str_pid << _getpid();
639 str_pid << ::getpid();
641 std::string job_dir = date_dir + "-" + str_pid.str();
643 _work_directory = _resource_definition.working_directory + job_dir;
648 _work_directory = std::string("/$HOME/Batch/workdir_");
650 _work_directory = std::string("%USERPROFILE%\\Batch\\workdir_");
652 _work_directory += date;
655 params[Batch::WORKDIR] = _work_directory;
656 std::string libbatch_pre_command("");
657 if(!_pre_command.empty())
659 boost::filesystem::path pre_command_path(_pre_command);
660 libbatch_pre_command += "./" + pre_command_path.filename().string();
662 params[Batch::PREPROCESS] = libbatch_pre_command;
664 // Parameters for COORM
665 params[Batch::LAUNCHER_FILE] = _launcher_file;
666 params[Batch::LAUNCHER_ARGS] = _launcher_args;
668 // If result_directory is not defined, we use HOME environment
669 if (_result_directory == "")
671 _result_directory = getenv("HOME");
673 _result_directory = getenv("USERPROFILE");
676 std::list<std::string> in_files(_in_files);
677 in_files.push_back(_job_file);
679 in_files.push_back(_env_file);
680 if(!_pre_command.empty())
681 in_files.push_back(_pre_command);
682 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
684 std::string file = *it;
686 // local file -> If file is not an absolute path, we apply _local_directory
687 std::string local_file;
689 if (file.substr(0, 1) == std::string("/"))
691 // On Windows, absolute paths may begin with something like "C:"
692 if (file.substr(1, 1) == std::string(":"))
695 else if (file.substr(0, 1) == std::string("-")) // using rsync options
699 // '/./' is used by rsync to find the root of the relative path
700 // /a/b/./c/f -> _working_directory/c/f
701 local_file = _local_directory + "/./" + file;
703 local_file = _local_directory + SEPARATOR + file;
706 // remote file -> get only file name from in_files
707 std::string remote_file = _work_directory + "/";
709 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
713 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
715 std::string file = *it;
716 // remote file -> If file is not an absolute path, we apply _work_directory
717 std::string remote_file;
718 std::string local_file;
719 if (file.substr(0, 1) == std::string("/"))
722 size_t found = file.find_last_of("/");
723 local_file = file.substr(found+1);
725 else if (file.substr(0, 1) == std::string("-")) // using rsync options
732 // '/./' is used by rsync to find the root of the relative path
733 remote_file = _work_directory + "/./" + file;
737 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
741 if (_maximum_duration_in_second != -1)
742 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
746 params[Batch::QUEUE] = _queue;
749 if (_partition != "")
750 params[Batch::PARTITION] = _partition;
754 params[Batch::EXCLUSIVE] = true;
758 params[Batch::WCKEY] = _wckey;
761 if (_extra_params != "")
762 params[Batch::EXTRAPARAMS] = _extra_params;
764 // Specific parameters
765 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
766 if (it != _specific_parameters.end())
767 params["LL_JOBTYPE"] = it->second;
772 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
774 _batch_job_id = batch_manager_job_id;
778 Launcher::Job::getBatchManagerJobId() const
780 return _batch_job_id;
785 Launcher::Job::addSpecificParameter(const std::string & name,
786 const std::string & value)
788 _specific_parameters[name] = value;
791 const std::map<std::string, std::string> &
792 Launcher::Job::getSpecificParameters() const
794 return _specific_parameters;
798 Launcher::Job::checkSpecificParameters()