1 // Copyright (C) 2009-2014 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
22 #include "Launcher_Job.hxx"
23 #include "Launcher.hxx"
26 #include <libbatch/Constants.hxx>
35 _launch_date = getLaunchDate();
41 _job_file_name_complete = "";
43 _local_directory = "";
44 _result_directory = "";
45 _maximum_duration = "";
46 _maximum_duration_in_second = -1;
52 // Parameters for COORM
57 _batch_job = new Batch::Job();
63 LAUNCHER_MESSAGE("Deleting job number: " << _number);
71 Launcher::Job::stopJob()
73 LAUNCHER_MESSAGE("Stop resquested for job number: " << _number);
76 if (_batch_job_id.getReference() != "undefined")
80 _batch_job_id.deleteJob();
82 catch (const Batch::GenericException &ex)
84 LAUNCHER_INFOS("WARNING: exception when stopping the job: " << ex.message);
92 Launcher::Job::removeJob()
94 LAUNCHER_MESSAGE("Removing job number: " << _number);
96 if (_batch_job_id.getReference() != "undefined")
100 _batch_job_id.deleteJob();
102 catch (const Batch::GenericException &ex)
104 LAUNCHER_INFOS("WARNING: exception when removing the job: " << ex.message);
111 Launcher::Job::getJobType() const
117 Launcher::Job::setJobName(const std::string & job_name)
119 _job_name = job_name;
123 Launcher::Job::getJobName() const
129 Launcher::Job::setState(const std::string & state)
131 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
132 if (state != "CREATED" &&
133 state != "IN_PROCESS" &&
135 state != "RUNNING" &&
137 state != "FINISHED" &&
141 throw LauncherException("Bad state, this state does not exist: " + state);
147 Launcher::Job::getState() const
152 // Get names or ids of hosts assigned to the job
154 Launcher::Job::getAssignedHostnames()
156 return _assigned_hostnames;
160 Launcher::Job::setNumber(const int & number)
163 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
168 Launcher::Job::getNumber()
174 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
176 // Check machine_definition
177 std::string user_name = "";
178 if (resource_definition.UserName == "")
180 user_name = getenv("USER");
183 std::string mess = "You must define a user name: into your resource description or with env variable USER";
184 throw LauncherException(mess);
188 user_name = resource_definition.UserName;
190 _resource_definition = resource_definition;
191 _resource_definition.UserName = user_name;
195 Launcher::Job::getResourceDefinition() const
197 return _resource_definition;
201 Launcher::Job::setJobFile(const std::string & job_file)
206 std::string mess = "Empty Job File is forbidden !";
207 throw LauncherException(mess);
210 _job_file = job_file;
211 std::string::size_type p1 = _job_file.find_last_of("/");
212 std::string::size_type p2 = _job_file.find_last_of(".");
213 _job_file_name_complete = _job_file.substr(p1+1);
214 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
218 Launcher::Job::getJobFile() const
223 Launcher::Job::setEnvFile(const std::string & env_file)
225 _env_file = env_file;
229 Launcher::Job::getEnvFile() const
235 Launcher::Job::setWorkDirectory(const std::string & work_directory)
237 _work_directory = work_directory;
241 Launcher::Job::setLocalDirectory(const std::string & local_directory)
243 _local_directory = local_directory;
247 Launcher::Job::setResultDirectory(const std::string & result_directory)
249 _result_directory = result_directory;
253 Launcher::Job::add_in_file(const std::string & file)
255 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
256 if (it == _in_files.end())
257 _in_files.push_back(file);
259 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
263 Launcher::Job::add_out_file(const std::string & file)
265 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
266 if (it == _out_files.end())
267 _out_files.push_back(file);
269 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
273 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
275 checkMaximumDuration(maximum_duration);
276 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
277 _maximum_duration = maximum_duration;
282 Launcher::Job::setLauncherFile(const std::string & launcher_file)
284 _launcher_file = launcher_file;
287 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
289 _launcher_args = launcher_args;
293 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
295 checkResourceRequiredParams(resource_required_params);
296 _resource_required_params = resource_required_params;
300 Launcher::Job::setQueue(const std::string & queue)
306 Launcher::Job::setExclusive(bool exclusive)
308 _exclusive = exclusive;
312 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
314 if (exclusiveStr == "true")
316 else if (exclusiveStr == "false")
319 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
323 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
325 _mem_per_cpu = mem_per_cpu;
329 Launcher::Job::setWCKey(const std::string & wckey)
335 Launcher::Job::setExtraParams(const std::string & extra_params)
337 _extra_params = extra_params;
341 Launcher::Job::setReference(const std::string & reference)
343 _reference = reference;
347 Launcher::Job::getWorkDirectory() const
349 return _work_directory;
353 Launcher::Job::getLocalDirectory() const
355 return _local_directory;
359 Launcher::Job::getResultDirectory() const
361 return _result_directory;
364 const std::list<std::string> &
365 Launcher::Job::get_in_files() const
370 const std::list<std::string> &
371 Launcher::Job::get_out_files() const
377 Launcher::Job::getMaximumDuration() const
379 return _maximum_duration;
384 Launcher::Job::getLauncherFile() const
386 return _launcher_file;
389 Launcher::Job::getLauncherArgs() const
391 return _launcher_args;
395 Launcher::Job::getResourceRequiredParams() const
397 return _resource_required_params;
401 Launcher::Job::getQueue() const
407 Launcher::Job::getExclusive() const
413 Launcher::Job::getExclusiveStr() const
415 return _exclusive ? "true" : "false";
419 Launcher::Job::getMemPerCpu() const
425 Launcher::Job::getWCKey() const
431 Launcher::Job::getExtraParams() const
433 return _extra_params;
437 Launcher::Job::getReference() const
443 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
445 std::string result("");
446 std::string edt_value = maximum_duration;
447 std::size_t pos = edt_value.find(":");
449 if (edt_value != "") {
450 if (pos == edt_value.npos) {
451 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
453 std::string begin_edt_value = edt_value.substr(0, pos);
454 std::string mid_edt_value = edt_value.substr(pos, 1);
455 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
458 std::istringstream iss(begin_edt_value);
459 if (!(iss >> value)) {
460 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
462 else if (value < 0) {
463 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
465 std::istringstream iss_2(end_edt_value);
466 if (!(iss_2 >> value)) {
467 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
469 else if (value < 0) {
470 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
472 if (mid_edt_value != ":") {
473 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
477 throw LauncherException(result);
481 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
483 // nb_proc has be to > 0
484 if (resource_required_params.nb_proc <= 0)
486 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
487 throw LauncherException(message);
492 Launcher::Job::convertMaximumDuration(const std::string & edt)
496 if( edt.size() == 0 )
499 std::string::size_type pos = edt.find(":");
500 std::string h = edt.substr(0,pos);
501 std::string m = edt.substr(pos+1,edt.size()-pos+1);
502 std::istringstream issh(h);
504 std::istringstream issm(m);
513 Launcher::Job::getLaunchDate() const
517 std::string launch_date = ctime(&rawtime);
519 for (;i < launch_date.size(); i++)
520 if (launch_date[i] == '/' ||
521 launch_date[i] == '-' ||
522 launch_date[i] == ':' ||
523 launch_date[i] == ' ')
524 launch_date[i] = '_';
525 launch_date.erase(--launch_date.end()); // Last caracter is a \n
531 Launcher::Job::updateJobState()
534 if (_state != "FINISHED" &&
539 if (_batch_job_id.getReference() != "undefined")
541 // A batch manager has been affected to the job
542 Batch::JobInfo job_info = _batch_job_id.queryJob();
543 Batch::Parametre par = job_info.getParametre();
544 _state = par[Batch::STATE].str();
545 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
546 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
547 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
556 Launcher::Job::getBatchJob()
563 Launcher::Job::common_job_params()
565 Batch::Parametre params;
567 params[Batch::NAME] = getJobName();
568 params[Batch::NBPROC] = _resource_required_params.nb_proc;
569 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
571 // Memory in megabytes
572 if (_resource_required_params.mem_mb > 0)
574 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
576 else if (_mem_per_cpu > 0)
578 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
581 // We define a default directory based on user time
582 if (_work_directory == "")
584 const size_t BUFSIZE = 32;
586 time_t curtime = time(NULL);
587 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
588 _work_directory = std::string("$HOME/Batch/workdir_");
589 _work_directory += date;
591 params[Batch::WORKDIR] = _work_directory;
593 // Parameters for COORM
594 params[Batch::LAUNCHER_FILE] = _launcher_file;
595 params[Batch::LAUNCHER_ARGS] = _launcher_args;
597 // If result_directory is not defined, we use HOME environnement
598 if (_result_directory == "")
599 _result_directory = getenv("HOME");
602 std::list<std::string> in_files(_in_files);
603 in_files.push_back(_job_file);
605 in_files.push_back(_env_file);
606 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
608 std::string file = *it;
610 // local file -> If file is not an absolute path, we apply _local_directory
611 std::string local_file;
612 if (file.substr(0, 1) == std::string("/"))
616 local_file = _local_directory + "/" + file;
621 // remote file -> get only file name from in_files
622 size_t found = file.find_last_of("/");
623 std::string remote_file = _work_directory + "/" + file.substr(found+1);
625 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
629 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
631 std::string file = *it;
634 size_t found = file.find_last_of("/");
635 std::string local_file = _result_directory + "/" + file.substr(found+1);
637 // remote file -> If file is not an absolute path, we apply _work_directory
638 std::string remote_file;
639 if (file.substr(0, 1) == std::string("/"))
642 remote_file = _work_directory + "/" + file;
644 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
648 if (_maximum_duration_in_second != -1)
649 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
653 params[Batch::QUEUE] = _queue;
657 params[Batch::EXCLUSIVE] = true;
661 params[Batch::WCKEY] = _wckey;
664 if (_extra_params != "")
665 params[Batch::EXTRAPARAMS] = _extra_params;
667 // Specific parameters
668 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
669 if (it != _specific_parameters.end())
670 params["LL_JOBTYPE"] = it->second;
675 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
677 _batch_job_id = batch_manager_job_id;
681 Launcher::Job::getBatchManagerJobId() const
683 return _batch_job_id;
688 Launcher::Job::addSpecificParameter(const std::string & name,
689 const std::string & value)
691 _specific_parameters[name] = value;
694 const std::map<std::string, std::string> &
695 Launcher::Job::getSpecificParameters() const
697 return _specific_parameters;
701 Launcher::Job::checkSpecificParameters()