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;
56 // Parameters for COORM
61 _batch_job = new Batch::Job();
67 LAUNCHER_MESSAGE("Deleting job number: " << _number);
75 Launcher::Job::stopJob()
77 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
80 if (_batch_job_id.getReference() != "undefined")
84 _batch_job_id.deleteJob();
86 catch (const Batch::GenericException &ex)
88 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
96 Launcher::Job::removeJob()
98 LAUNCHER_MESSAGE("Removing job number: " << _number);
100 if (_batch_job_id.getReference() != "undefined")
104 _batch_job_id.deleteJob();
106 catch (const Batch::GenericException &ex)
108 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
115 Launcher::Job::getJobType() const
121 Launcher::Job::setJobName(const std::string & job_name)
123 _job_name = job_name;
127 Launcher::Job::getJobName() const
133 Launcher::Job::setState(const std::string & state)
135 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
136 if (state != "CREATED" &&
137 state != "IN_PROCESS" &&
139 state != "RUNNING" &&
141 state != "FINISHED" &&
145 throw LauncherException("Bad state, this state does not exist: " + state);
151 Launcher::Job::getState() const
156 // Get names or ids of hosts assigned to the job
158 Launcher::Job::getAssignedHostnames()
160 return _assigned_hostnames;
164 Launcher::Job::setNumber(const int & number)
167 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
172 Launcher::Job::getNumber()
178 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
180 // Check machine_definition
181 std::string user_name = "";
182 if (resource_definition.UserName == "")
184 user_name = getenv("USER");
186 user_name = getenv("LOGNAME");
189 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
190 throw LauncherException(mess);
194 user_name = resource_definition.UserName;
196 _resource_definition = resource_definition;
197 _resource_definition.UserName = user_name;
201 Launcher::Job::getResourceDefinition() const
203 return _resource_definition;
207 Launcher::Job::setJobFile(const std::string & job_file)
212 std::string mess = "Empty Job File is forbidden !";
213 throw LauncherException(mess);
216 _job_file = job_file;
217 std::string::size_type p1 = _job_file.find_last_of("/");
218 std::string::size_type p2 = _job_file.find_last_of(".");
219 _job_file_name_complete = _job_file.substr(p1+1);
220 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
224 Launcher::Job::getJobFile() const
229 Launcher::Job::setEnvFile(const std::string & env_file)
231 _env_file = env_file;
235 Launcher::Job::getEnvFile() const
241 Launcher::Job::setWorkDirectory(const std::string & work_directory)
243 _work_directory = work_directory;
247 Launcher::Job::setLocalDirectory(const std::string & local_directory)
249 _local_directory = local_directory;
253 Launcher::Job::setResultDirectory(const std::string & result_directory)
255 _result_directory = result_directory;
259 Launcher::Job::add_in_file(const std::string & file)
261 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
262 if (it == _in_files.end())
263 _in_files.push_back(file);
265 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
269 Launcher::Job::add_out_file(const std::string & file)
271 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
272 if (it == _out_files.end())
273 _out_files.push_back(file);
275 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
279 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
281 checkMaximumDuration(maximum_duration);
282 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
283 _maximum_duration = maximum_duration;
288 Launcher::Job::setLauncherFile(const std::string & launcher_file)
290 _launcher_file = launcher_file;
293 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
295 _launcher_args = launcher_args;
299 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
301 checkResourceRequiredParams(resource_required_params);
302 _resource_required_params = resource_required_params;
306 Launcher::Job::setQueue(const std::string & queue)
312 Launcher::Job::setPartition(const std::string & partition)
314 _partition = partition;
318 Launcher::Job::setExclusive(bool exclusive)
320 _exclusive = exclusive;
324 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
326 if (exclusiveStr == "true")
328 else if (exclusiveStr == "false")
331 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
335 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
337 _mem_per_cpu = mem_per_cpu;
341 Launcher::Job::setWCKey(const std::string & wckey)
347 Launcher::Job::setExtraParams(const std::string & extra_params)
349 _extra_params = extra_params;
353 Launcher::Job::setReference(const std::string & reference)
355 _reference = reference;
359 Launcher::Job::getWorkDirectory() const
361 return _work_directory;
365 Launcher::Job::getLocalDirectory() const
367 return _local_directory;
371 Launcher::Job::getResultDirectory() const
373 return _result_directory;
376 const std::list<std::string> &
377 Launcher::Job::get_in_files() const
382 const std::list<std::string> &
383 Launcher::Job::get_out_files() const
389 Launcher::Job::getMaximumDuration() const
391 return _maximum_duration;
396 Launcher::Job::getLauncherFile() const
398 return _launcher_file;
401 Launcher::Job::getLauncherArgs() const
403 return _launcher_args;
407 Launcher::Job::getResourceRequiredParams() const
409 return _resource_required_params;
413 Launcher::Job::getQueue() const
419 Launcher::Job::getPartition() const
425 Launcher::Job::getExclusive() const
431 Launcher::Job::getExclusiveStr() const
433 return _exclusive ? "true" : "false";
437 Launcher::Job::getMemPerCpu() const
443 Launcher::Job::getWCKey() const
449 Launcher::Job::getExtraParams() const
451 return _extra_params;
455 Launcher::Job::getReference() const
461 Launcher::Job::setPreCommand(const std::string & preCommand)
463 _pre_command = preCommand;
467 Launcher::Job::getPreCommand() const
473 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
475 std::string result("");
476 std::string edt_value = maximum_duration;
477 std::size_t pos = edt_value.find(":");
479 if (edt_value != "") {
480 if (pos == edt_value.npos) {
481 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
483 std::string begin_edt_value = edt_value.substr(0, pos);
484 std::string mid_edt_value = edt_value.substr(pos, 1);
485 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
488 std::istringstream iss(begin_edt_value);
489 if (!(iss >> value)) {
490 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
492 else if (value < 0) {
493 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
495 std::istringstream iss_2(end_edt_value);
496 if (!(iss_2 >> value)) {
497 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
499 else if (value < 0) {
500 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
502 if (mid_edt_value != ":") {
503 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
507 throw LauncherException(result);
511 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
513 // nb_proc has be to > 0
514 if (resource_required_params.nb_proc <= 0)
516 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
517 throw LauncherException(message);
522 Launcher::Job::convertMaximumDuration(const std::string & edt)
526 if( edt.size() == 0 )
529 std::string::size_type pos = edt.find(":");
530 std::string h = edt.substr(0,pos);
531 std::string m = edt.substr(pos+1,edt.size()-pos+1);
532 std::istringstream issh(h);
534 std::istringstream issm(m);
543 Launcher::Job::getLaunchDate() const
547 std::string launch_date = ctime(&rawtime);
549 for (;i < launch_date.size(); i++)
550 if (launch_date[i] == '/' ||
551 launch_date[i] == '-' ||
552 launch_date[i] == ':' ||
553 launch_date[i] == ' ')
554 launch_date[i] = '_';
555 launch_date.erase(--launch_date.end()); // Last character is a \n
561 Launcher::Job::updateJobState()
564 if (_state != "FINISHED" &&
569 if (_batch_job_id.getReference() != "undefined")
571 // A batch manager has been affected to the job
572 Batch::JobInfo job_info = _batch_job_id.queryJob();
573 Batch::Parametre par = job_info.getParametre();
574 _state = par[Batch::STATE].str();
575 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
576 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
577 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
586 Launcher::Job::getBatchJob()
593 Launcher::Job::common_job_params()
595 Batch::Parametre params;
597 params[Batch::NAME] = getJobName();
598 params[Batch::NBPROC] = _resource_required_params.nb_proc;
599 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
601 if(_resource_required_params.nb_node > 0)
602 params[Batch::NBNODE] = _resource_required_params.nb_node;
604 // Memory in megabytes
605 if (_resource_required_params.mem_mb > 0)
607 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
609 else if (_mem_per_cpu > 0)
611 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
614 // We define a default directory
615 if (_work_directory == "")
617 const size_t BUFSIZE = 32;
619 time_t curtime = time(NULL);
620 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
621 if(!_resource_definition.working_directory.empty())
623 std::string date_dir = std::string("/job_") + date;
624 std::ostringstream str_pid;
625 str_pid << ::getpid();
626 std::string job_dir = date_dir + "-" + str_pid.str();
628 _work_directory = _resource_definition.working_directory + job_dir;
632 _work_directory = std::string("/$HOME/Batch/workdir_");
633 _work_directory += date;
636 params[Batch::WORKDIR] = _work_directory;
637 std::string libbatch_pre_command("");
638 if(!_pre_command.empty())
640 boost::filesystem::path pre_command_path(_pre_command);
641 libbatch_pre_command += "./" + pre_command_path.filename().string();
643 params[Batch::PREPROCESS] = libbatch_pre_command;
645 // Parameters for COORM
646 params[Batch::LAUNCHER_FILE] = _launcher_file;
647 params[Batch::LAUNCHER_ARGS] = _launcher_args;
649 // If result_directory is not defined, we use HOME environnement
650 if (_result_directory == "")
651 _result_directory = getenv("HOME");
654 std::list<std::string> in_files(_in_files);
655 in_files.push_back(_job_file);
657 in_files.push_back(_env_file);
658 if(!_pre_command.empty())
659 in_files.push_back(_pre_command);
660 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
662 std::string file = *it;
664 // local file -> If file is not an absolute path, we apply _local_directory
665 std::string local_file;
666 if (file.substr(0, 1) == std::string("/"))
668 else if (file.substr(0, 1) == std::string("-")) // using rsync options
672 // '/./' is used by rsync to find the root of the relative path
673 // /a/b/./c/f -> _working_directory/c/f
674 local_file = _local_directory + "/./" + file;
679 // remote file -> get only file name from in_files
680 std::string remote_file = _work_directory + "/";
682 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
686 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
688 std::string file = *it;
689 // remote file -> If file is not an absolute path, we apply _work_directory
690 std::string remote_file;
691 std::string local_file;
692 if (file.substr(0, 1) == std::string("/"))
695 size_t found = file.find_last_of("/");
696 local_file = file.substr(found+1);
698 else if (file.substr(0, 1) == std::string("-")) // using rsync options
705 // '/./' is used by rsync to find the root of the relative path
706 remote_file = _work_directory + "/./" + file;
710 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
714 if (_maximum_duration_in_second != -1)
715 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
719 params[Batch::QUEUE] = _queue;
722 if (_partition != "")
723 params[Batch::PARTITION] = _partition;
727 params[Batch::EXCLUSIVE] = true;
731 params[Batch::WCKEY] = _wckey;
734 if (_extra_params != "")
735 params[Batch::EXTRAPARAMS] = _extra_params;
737 // Specific parameters
738 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
739 if (it != _specific_parameters.end())
740 params["LL_JOBTYPE"] = it->second;
745 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
747 _batch_job_id = batch_manager_job_id;
751 Launcher::Job::getBatchManagerJobId() const
753 return _batch_job_id;
758 Launcher::Job::addSpecificParameter(const std::string & name,
759 const std::string & value)
761 _specific_parameters[name] = value;
764 const std::map<std::string, std::string> &
765 Launcher::Job::getSpecificParameters() const
767 return _specific_parameters;
771 Launcher::Job::checkSpecificParameters()