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"
27 #include <libbatch/Constants.hxx>
36 _launch_date = getLaunchDate();
42 _job_file_name_complete = "";
45 _local_directory = "";
46 _result_directory = "";
47 _maximum_duration = "";
48 _maximum_duration_in_second = -1;
54 // Parameters for COORM
59 _batch_job = new Batch::Job();
65 LAUNCHER_MESSAGE("Deleting job number: " << _number);
73 Launcher::Job::stopJob()
75 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
78 if (_batch_job_id.getReference() != "undefined")
82 _batch_job_id.deleteJob();
84 catch (const Batch::GenericException &ex)
86 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
94 Launcher::Job::removeJob()
96 LAUNCHER_MESSAGE("Removing job number: " << _number);
98 if (_batch_job_id.getReference() != "undefined")
102 _batch_job_id.deleteJob();
104 catch (const Batch::GenericException &ex)
106 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
113 Launcher::Job::getJobType() const
119 Launcher::Job::setJobName(const std::string & job_name)
121 _job_name = job_name;
125 Launcher::Job::getJobName() const
131 Launcher::Job::setState(const std::string & state)
133 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
134 if (state != "CREATED" &&
135 state != "IN_PROCESS" &&
137 state != "RUNNING" &&
139 state != "FINISHED" &&
143 throw LauncherException("Bad state, this state does not exist: " + state);
149 Launcher::Job::getState() const
154 // Get names or ids of hosts assigned to the job
156 Launcher::Job::getAssignedHostnames()
158 return _assigned_hostnames;
162 Launcher::Job::setNumber(const int & number)
165 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
170 Launcher::Job::getNumber()
176 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
178 // Check machine_definition
179 std::string user_name = "";
180 if (resource_definition.UserName == "")
182 user_name = getenv("USER");
184 user_name = getenv("LOGNAME");
187 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
188 throw LauncherException(mess);
192 user_name = resource_definition.UserName;
194 _resource_definition = resource_definition;
195 _resource_definition.UserName = user_name;
199 Launcher::Job::getResourceDefinition() const
201 return _resource_definition;
205 Launcher::Job::setJobFile(const std::string & job_file)
210 std::string mess = "Empty Job File is forbidden !";
211 throw LauncherException(mess);
214 _job_file = job_file;
215 std::string::size_type p1 = _job_file.find_last_of("/");
216 std::string::size_type p2 = _job_file.find_last_of(".");
217 _job_file_name_complete = _job_file.substr(p1+1);
218 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
222 Launcher::Job::getJobFile() const
227 Launcher::Job::setEnvFile(const std::string & env_file)
229 _env_file = env_file;
233 Launcher::Job::getEnvFile() const
239 Launcher::Job::setWorkDirectory(const std::string & work_directory)
241 _work_directory = work_directory;
245 Launcher::Job::setLocalDirectory(const std::string & local_directory)
247 _local_directory = local_directory;
251 Launcher::Job::setResultDirectory(const std::string & result_directory)
253 _result_directory = result_directory;
257 Launcher::Job::add_in_file(const std::string & file)
259 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
260 if (it == _in_files.end())
261 _in_files.push_back(file);
263 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
267 Launcher::Job::add_out_file(const std::string & file)
269 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
270 if (it == _out_files.end())
271 _out_files.push_back(file);
273 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
277 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
279 checkMaximumDuration(maximum_duration);
280 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
281 _maximum_duration = maximum_duration;
286 Launcher::Job::setLauncherFile(const std::string & launcher_file)
288 _launcher_file = launcher_file;
291 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
293 _launcher_args = launcher_args;
297 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
299 checkResourceRequiredParams(resource_required_params);
300 _resource_required_params = resource_required_params;
304 Launcher::Job::setQueue(const std::string & queue)
310 Launcher::Job::setExclusive(bool exclusive)
312 _exclusive = exclusive;
316 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
318 if (exclusiveStr == "true")
320 else if (exclusiveStr == "false")
323 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
327 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
329 _mem_per_cpu = mem_per_cpu;
333 Launcher::Job::setWCKey(const std::string & wckey)
339 Launcher::Job::setExtraParams(const std::string & extra_params)
341 _extra_params = extra_params;
345 Launcher::Job::setReference(const std::string & reference)
347 _reference = reference;
351 Launcher::Job::getWorkDirectory() const
353 return _work_directory;
357 Launcher::Job::getLocalDirectory() const
359 return _local_directory;
363 Launcher::Job::getResultDirectory() const
365 return _result_directory;
368 const std::list<std::string> &
369 Launcher::Job::get_in_files() const
374 const std::list<std::string> &
375 Launcher::Job::get_out_files() const
381 Launcher::Job::getMaximumDuration() const
383 return _maximum_duration;
388 Launcher::Job::getLauncherFile() const
390 return _launcher_file;
393 Launcher::Job::getLauncherArgs() const
395 return _launcher_args;
399 Launcher::Job::getResourceRequiredParams() const
401 return _resource_required_params;
405 Launcher::Job::getQueue() const
411 Launcher::Job::getExclusive() const
417 Launcher::Job::getExclusiveStr() const
419 return _exclusive ? "true" : "false";
423 Launcher::Job::getMemPerCpu() const
429 Launcher::Job::getWCKey() const
435 Launcher::Job::getExtraParams() const
437 return _extra_params;
441 Launcher::Job::getReference() const
447 Launcher::Job::setPreCommand(const std::string & preCommand)
449 _pre_command = preCommand;
453 Launcher::Job::getPreCommand() const
459 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
461 std::string result("");
462 std::string edt_value = maximum_duration;
463 std::size_t pos = edt_value.find(":");
465 if (edt_value != "") {
466 if (pos == edt_value.npos) {
467 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
469 std::string begin_edt_value = edt_value.substr(0, pos);
470 std::string mid_edt_value = edt_value.substr(pos, 1);
471 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
474 std::istringstream iss(begin_edt_value);
475 if (!(iss >> value)) {
476 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
478 else if (value < 0) {
479 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
481 std::istringstream iss_2(end_edt_value);
482 if (!(iss_2 >> value)) {
483 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
485 else if (value < 0) {
486 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
488 if (mid_edt_value != ":") {
489 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
493 throw LauncherException(result);
497 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
499 // nb_proc has be to > 0
500 if (resource_required_params.nb_proc <= 0)
502 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
503 throw LauncherException(message);
508 Launcher::Job::convertMaximumDuration(const std::string & edt)
512 if( edt.size() == 0 )
515 std::string::size_type pos = edt.find(":");
516 std::string h = edt.substr(0,pos);
517 std::string m = edt.substr(pos+1,edt.size()-pos+1);
518 std::istringstream issh(h);
520 std::istringstream issm(m);
529 Launcher::Job::getLaunchDate() const
533 std::string launch_date = ctime(&rawtime);
535 for (;i < launch_date.size(); i++)
536 if (launch_date[i] == '/' ||
537 launch_date[i] == '-' ||
538 launch_date[i] == ':' ||
539 launch_date[i] == ' ')
540 launch_date[i] = '_';
541 launch_date.erase(--launch_date.end()); // Last caracter is a \n
547 Launcher::Job::updateJobState()
550 if (_state != "FINISHED" &&
555 if (_batch_job_id.getReference() != "undefined")
557 // A batch manager has been affected to the job
558 Batch::JobInfo job_info = _batch_job_id.queryJob();
559 Batch::Parametre par = job_info.getParametre();
560 _state = par[Batch::STATE].str();
561 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
562 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
563 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
572 Launcher::Job::getBatchJob()
579 Launcher::Job::common_job_params()
581 Batch::Parametre params;
583 params[Batch::NAME] = getJobName();
584 params[Batch::NBPROC] = _resource_required_params.nb_proc;
585 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
587 // Memory in megabytes
588 if (_resource_required_params.mem_mb > 0)
590 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
592 else if (_mem_per_cpu > 0)
594 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
597 // We define a default directory
598 if (_work_directory == "")
600 const size_t BUFSIZE = 32;
602 time_t curtime = time(NULL);
603 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
604 if(!_resource_definition.working_directory.empty())
606 std::string date_dir = std::string("/job_") + date;
607 std::ostringstream str_pid;
608 str_pid << ::getpid();
609 std::string job_dir = date_dir + "-" + str_pid.str();
611 _work_directory = _resource_definition.working_directory + job_dir;
615 _work_directory = std::string("/$HOME/Batch/workdir_");
616 _work_directory += date;
619 params[Batch::WORKDIR] = _work_directory;
620 params[Batch::PREPROCESS] = _pre_command;
622 // Parameters for COORM
623 params[Batch::LAUNCHER_FILE] = _launcher_file;
624 params[Batch::LAUNCHER_ARGS] = _launcher_args;
626 // If result_directory is not defined, we use HOME environnement
627 if (_result_directory == "")
628 _result_directory = getenv("HOME");
631 std::list<std::string> in_files(_in_files);
632 in_files.push_back(_job_file);
634 in_files.push_back(_env_file);
635 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
637 std::string file = *it;
639 // local file -> If file is not an absolute path, we apply _local_directory
640 std::string local_file;
641 if (file.substr(0, 1) == std::string("/"))
645 local_file = _local_directory + "/" + file;
650 // remote file -> get only file name from in_files
651 size_t found = file.find_last_of("/");
652 std::string remote_file = _work_directory + "/" + file.substr(found+1);
654 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
658 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
660 std::string file = *it;
661 // remote file -> If file is not an absolute path, we apply _work_directory
662 std::string remote_file;
663 std::string local_file;
664 if (file.substr(0, 1) == std::string("/"))
667 size_t found = file.find_last_of("/");
668 local_file = file.substr(found+1);
672 remote_file = _work_directory + "/" + file;
676 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
680 if (_maximum_duration_in_second != -1)
681 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
685 params[Batch::QUEUE] = _queue;
689 params[Batch::EXCLUSIVE] = true;
693 params[Batch::WCKEY] = _wckey;
696 if (_extra_params != "")
697 params[Batch::EXTRAPARAMS] = _extra_params;
699 // Specific parameters
700 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
701 if (it != _specific_parameters.end())
702 params["LL_JOBTYPE"] = it->second;
707 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
709 _batch_job_id = batch_manager_job_id;
713 Launcher::Job::getBatchManagerJobId() const
715 return _batch_job_id;
720 Launcher::Job::addSpecificParameter(const std::string & name,
721 const std::string & value)
723 _specific_parameters[name] = value;
726 const std::map<std::string, std::string> &
727 Launcher::Job::getSpecificParameters() const
729 return _specific_parameters;
733 Launcher::Job::checkSpecificParameters()