1 // Copyright (C) 2009-2016 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 = "";
44 _local_directory = "";
45 _result_directory = "";
46 _maximum_duration = "";
47 _maximum_duration_in_second = -1;
53 // Parameters for COORM
58 _batch_job = new Batch::Job();
64 LAUNCHER_MESSAGE("Deleting job number: " << _number);
72 Launcher::Job::stopJob()
74 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
77 if (_batch_job_id.getReference() != "undefined")
81 _batch_job_id.deleteJob();
83 catch (const Batch::GenericException &ex)
85 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
93 Launcher::Job::removeJob()
95 LAUNCHER_MESSAGE("Removing job number: " << _number);
97 if (_batch_job_id.getReference() != "undefined")
101 _batch_job_id.deleteJob();
103 catch (const Batch::GenericException &ex)
105 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
112 Launcher::Job::getJobType() const
118 Launcher::Job::setJobName(const std::string & job_name)
120 _job_name = job_name;
124 Launcher::Job::getJobName() const
130 Launcher::Job::setState(const std::string & state)
132 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
133 if (state != "CREATED" &&
134 state != "IN_PROCESS" &&
136 state != "RUNNING" &&
138 state != "FINISHED" &&
142 throw LauncherException("Bad state, this state does not exist: " + state);
148 Launcher::Job::getState() const
153 // Get names or ids of hosts assigned to the job
155 Launcher::Job::getAssignedHostnames()
157 return _assigned_hostnames;
161 Launcher::Job::setNumber(const int & number)
164 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
169 Launcher::Job::getNumber()
175 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
177 // Check machine_definition
178 std::string user_name = "";
179 if (resource_definition.UserName == "")
181 user_name = getenv("USER");
183 user_name = getenv("LOGNAME");
186 std::string mess = "You must define a user name: into your resource description or with one of env variables USER/LOGNAME";
187 throw LauncherException(mess);
191 user_name = resource_definition.UserName;
193 _resource_definition = resource_definition;
194 _resource_definition.UserName = user_name;
198 Launcher::Job::getResourceDefinition() const
200 return _resource_definition;
204 Launcher::Job::setJobFile(const std::string & job_file)
209 std::string mess = "Empty Job File is forbidden !";
210 throw LauncherException(mess);
213 _job_file = job_file;
214 std::string::size_type p1 = _job_file.find_last_of("/");
215 std::string::size_type p2 = _job_file.find_last_of(".");
216 _job_file_name_complete = _job_file.substr(p1+1);
217 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
221 Launcher::Job::getJobFile() const
226 Launcher::Job::setEnvFile(const std::string & env_file)
228 _env_file = env_file;
232 Launcher::Job::getEnvFile() const
238 Launcher::Job::setWorkDirectory(const std::string & work_directory)
240 _work_directory = work_directory;
244 Launcher::Job::setLocalDirectory(const std::string & local_directory)
246 _local_directory = local_directory;
250 Launcher::Job::setResultDirectory(const std::string & result_directory)
252 _result_directory = result_directory;
256 Launcher::Job::add_in_file(const std::string & file)
258 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
259 if (it == _in_files.end())
260 _in_files.push_back(file);
262 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
266 Launcher::Job::add_out_file(const std::string & file)
268 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
269 if (it == _out_files.end())
270 _out_files.push_back(file);
272 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
276 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
278 checkMaximumDuration(maximum_duration);
279 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
280 _maximum_duration = maximum_duration;
285 Launcher::Job::setLauncherFile(const std::string & launcher_file)
287 _launcher_file = launcher_file;
290 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
292 _launcher_args = launcher_args;
296 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
298 checkResourceRequiredParams(resource_required_params);
299 _resource_required_params = resource_required_params;
303 Launcher::Job::setQueue(const std::string & queue)
309 Launcher::Job::setExclusive(bool exclusive)
311 _exclusive = exclusive;
315 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
317 if (exclusiveStr == "true")
319 else if (exclusiveStr == "false")
322 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
326 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
328 _mem_per_cpu = mem_per_cpu;
332 Launcher::Job::setWCKey(const std::string & wckey)
338 Launcher::Job::setExtraParams(const std::string & extra_params)
340 _extra_params = extra_params;
344 Launcher::Job::setReference(const std::string & reference)
346 _reference = reference;
350 Launcher::Job::getWorkDirectory() const
352 return _work_directory;
356 Launcher::Job::getLocalDirectory() const
358 return _local_directory;
362 Launcher::Job::getResultDirectory() const
364 return _result_directory;
367 const std::list<std::string> &
368 Launcher::Job::get_in_files() const
373 const std::list<std::string> &
374 Launcher::Job::get_out_files() const
380 Launcher::Job::getMaximumDuration() const
382 return _maximum_duration;
387 Launcher::Job::getLauncherFile() const
389 return _launcher_file;
392 Launcher::Job::getLauncherArgs() const
394 return _launcher_args;
398 Launcher::Job::getResourceRequiredParams() const
400 return _resource_required_params;
404 Launcher::Job::getQueue() const
410 Launcher::Job::getExclusive() const
416 Launcher::Job::getExclusiveStr() const
418 return _exclusive ? "true" : "false";
422 Launcher::Job::getMemPerCpu() const
428 Launcher::Job::getWCKey() const
434 Launcher::Job::getExtraParams() const
436 return _extra_params;
440 Launcher::Job::getReference() const
446 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
448 std::string result("");
449 std::string edt_value = maximum_duration;
450 std::size_t pos = edt_value.find(":");
452 if (edt_value != "") {
453 if (pos == edt_value.npos) {
454 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
456 std::string begin_edt_value = edt_value.substr(0, pos);
457 std::string mid_edt_value = edt_value.substr(pos, 1);
458 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
461 std::istringstream iss(begin_edt_value);
462 if (!(iss >> value)) {
463 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
465 else if (value < 0) {
466 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
468 std::istringstream iss_2(end_edt_value);
469 if (!(iss_2 >> value)) {
470 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
472 else if (value < 0) {
473 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
475 if (mid_edt_value != ":") {
476 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
480 throw LauncherException(result);
484 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
486 // nb_proc has be to > 0
487 if (resource_required_params.nb_proc <= 0)
489 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
490 throw LauncherException(message);
495 Launcher::Job::convertMaximumDuration(const std::string & edt)
499 if( edt.size() == 0 )
502 std::string::size_type pos = edt.find(":");
503 std::string h = edt.substr(0,pos);
504 std::string m = edt.substr(pos+1,edt.size()-pos+1);
505 std::istringstream issh(h);
507 std::istringstream issm(m);
516 Launcher::Job::getLaunchDate() const
520 std::string launch_date = ctime(&rawtime);
522 for (;i < launch_date.size(); i++)
523 if (launch_date[i] == '/' ||
524 launch_date[i] == '-' ||
525 launch_date[i] == ':' ||
526 launch_date[i] == ' ')
527 launch_date[i] = '_';
528 launch_date.erase(--launch_date.end()); // Last caracter is a \n
534 Launcher::Job::updateJobState()
537 if (_state != "FINISHED" &&
542 if (_batch_job_id.getReference() != "undefined")
544 // A batch manager has been affected to the job
545 Batch::JobInfo job_info = _batch_job_id.queryJob();
546 Batch::Parametre par = job_info.getParametre();
547 _state = par[Batch::STATE].str();
548 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
549 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
550 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
559 Launcher::Job::getBatchJob()
566 Launcher::Job::common_job_params()
568 Batch::Parametre params;
570 params[Batch::NAME] = getJobName();
571 params[Batch::NBPROC] = _resource_required_params.nb_proc;
572 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
574 // Memory in megabytes
575 if (_resource_required_params.mem_mb > 0)
577 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
579 else if (_mem_per_cpu > 0)
581 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
584 // We define a default directory
585 if (_work_directory == "")
587 const size_t BUFSIZE = 32;
589 time_t curtime = time(NULL);
590 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
591 if(!_resource_definition.working_directory.empty())
593 std::string job_dir = std::string("/job_") + date;
594 _work_directory = _resource_definition.working_directory + job_dir;
598 _work_directory = std::string("/$HOME/Batch/workdir_");
599 _work_directory += date;
602 params[Batch::WORKDIR] = _work_directory;
604 // Parameters for COORM
605 params[Batch::LAUNCHER_FILE] = _launcher_file;
606 params[Batch::LAUNCHER_ARGS] = _launcher_args;
608 // If result_directory is not defined, we use HOME environnement
609 if (_result_directory == "")
610 _result_directory = getenv("HOME");
613 std::list<std::string> in_files(_in_files);
614 in_files.push_back(_job_file);
616 in_files.push_back(_env_file);
617 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
619 std::string file = *it;
621 // local file -> If file is not an absolute path, we apply _local_directory
622 std::string local_file;
623 if (file.substr(0, 1) == std::string("/"))
627 local_file = _local_directory + "/" + file;
632 // remote file -> get only file name from in_files
633 size_t found = file.find_last_of("/");
634 std::string remote_file = _work_directory + "/" + file.substr(found+1);
636 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
640 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
642 std::string file = *it;
643 // remote file -> If file is not an absolute path, we apply _work_directory
644 std::string remote_file;
645 std::string local_file;
646 if (file.substr(0, 1) == std::string("/"))
649 size_t found = file.find_last_of("/");
650 local_file = file.substr(found+1);
654 remote_file = _work_directory + "/" + file;
658 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
662 if (_maximum_duration_in_second != -1)
663 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
667 params[Batch::QUEUE] = _queue;
671 params[Batch::EXCLUSIVE] = true;
675 params[Batch::WCKEY] = _wckey;
678 if (_extra_params != "")
679 params[Batch::EXTRAPARAMS] = _extra_params;
681 // Specific parameters
682 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
683 if (it != _specific_parameters.end())
684 params["LL_JOBTYPE"] = it->second;
689 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
691 _batch_job_id = batch_manager_job_id;
695 Launcher::Job::getBatchManagerJobId() const
697 return _batch_job_id;
702 Launcher::Job::addSpecificParameter(const std::string & name,
703 const std::string & value)
705 _specific_parameters[name] = value;
708 const std::map<std::string, std::string> &
709 Launcher::Job::getSpecificParameters() const
711 return _specific_parameters;
715 Launcher::Job::checkSpecificParameters()