1 // Copyright (C) 2009-2015 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");
184 std::string mess = "You must define a user name: into your resource description or with env variable USER";
185 throw LauncherException(mess);
189 user_name = resource_definition.UserName;
191 _resource_definition = resource_definition;
192 _resource_definition.UserName = user_name;
196 Launcher::Job::getResourceDefinition() const
198 return _resource_definition;
202 Launcher::Job::setJobFile(const std::string & job_file)
207 std::string mess = "Empty Job File is forbidden !";
208 throw LauncherException(mess);
211 _job_file = job_file;
212 std::string::size_type p1 = _job_file.find_last_of("/");
213 std::string::size_type p2 = _job_file.find_last_of(".");
214 _job_file_name_complete = _job_file.substr(p1+1);
215 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
219 Launcher::Job::getJobFile() const
224 Launcher::Job::setEnvFile(const std::string & env_file)
226 _env_file = env_file;
230 Launcher::Job::getEnvFile() const
236 Launcher::Job::setWorkDirectory(const std::string & work_directory)
238 _work_directory = work_directory;
242 Launcher::Job::setLocalDirectory(const std::string & local_directory)
244 _local_directory = local_directory;
248 Launcher::Job::setResultDirectory(const std::string & result_directory)
250 _result_directory = result_directory;
254 Launcher::Job::add_in_file(const std::string & file)
256 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
257 if (it == _in_files.end())
258 _in_files.push_back(file);
260 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
264 Launcher::Job::add_out_file(const std::string & file)
266 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
267 if (it == _out_files.end())
268 _out_files.push_back(file);
270 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
274 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
276 checkMaximumDuration(maximum_duration);
277 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
278 _maximum_duration = maximum_duration;
283 Launcher::Job::setLauncherFile(const std::string & launcher_file)
285 _launcher_file = launcher_file;
288 Launcher::Job::setLauncherArgs(const std::string & launcher_args)
290 _launcher_args = launcher_args;
294 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
296 checkResourceRequiredParams(resource_required_params);
297 _resource_required_params = resource_required_params;
301 Launcher::Job::setQueue(const std::string & queue)
307 Launcher::Job::setExclusive(bool exclusive)
309 _exclusive = exclusive;
313 Launcher::Job::setExclusiveStr(const std::string & exclusiveStr)
315 if (exclusiveStr == "true")
317 else if (exclusiveStr == "false")
320 throw LauncherException(std::string("Invalid boolean value for exclusive: ") + exclusiveStr);
324 Launcher::Job::setMemPerCpu(unsigned long mem_per_cpu)
326 _mem_per_cpu = mem_per_cpu;
330 Launcher::Job::setWCKey(const std::string & wckey)
336 Launcher::Job::setExtraParams(const std::string & extra_params)
338 _extra_params = extra_params;
342 Launcher::Job::setReference(const std::string & reference)
344 _reference = reference;
348 Launcher::Job::getWorkDirectory() const
350 return _work_directory;
354 Launcher::Job::getLocalDirectory() const
356 return _local_directory;
360 Launcher::Job::getResultDirectory() const
362 return _result_directory;
365 const std::list<std::string> &
366 Launcher::Job::get_in_files() const
371 const std::list<std::string> &
372 Launcher::Job::get_out_files() const
378 Launcher::Job::getMaximumDuration() const
380 return _maximum_duration;
385 Launcher::Job::getLauncherFile() const
387 return _launcher_file;
390 Launcher::Job::getLauncherArgs() const
392 return _launcher_args;
396 Launcher::Job::getResourceRequiredParams() const
398 return _resource_required_params;
402 Launcher::Job::getQueue() const
408 Launcher::Job::getExclusive() const
414 Launcher::Job::getExclusiveStr() const
416 return _exclusive ? "true" : "false";
420 Launcher::Job::getMemPerCpu() const
426 Launcher::Job::getWCKey() const
432 Launcher::Job::getExtraParams() const
434 return _extra_params;
438 Launcher::Job::getReference() const
444 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
446 std::string result("");
447 std::string edt_value = maximum_duration;
448 std::size_t pos = edt_value.find(":");
450 if (edt_value != "") {
451 if (pos == edt_value.npos) {
452 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
454 std::string begin_edt_value = edt_value.substr(0, pos);
455 std::string mid_edt_value = edt_value.substr(pos, 1);
456 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
459 std::istringstream iss(begin_edt_value);
460 if (!(iss >> value)) {
461 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
463 else if (value < 0) {
464 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
466 std::istringstream iss_2(end_edt_value);
467 if (!(iss_2 >> value)) {
468 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
470 else if (value < 0) {
471 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
473 if (mid_edt_value != ":") {
474 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
478 throw LauncherException(result);
482 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
484 // nb_proc has be to > 0
485 if (resource_required_params.nb_proc <= 0)
487 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
488 throw LauncherException(message);
493 Launcher::Job::convertMaximumDuration(const std::string & edt)
497 if( edt.size() == 0 )
500 std::string::size_type pos = edt.find(":");
501 std::string h = edt.substr(0,pos);
502 std::string m = edt.substr(pos+1,edt.size()-pos+1);
503 std::istringstream issh(h);
505 std::istringstream issm(m);
514 Launcher::Job::getLaunchDate() const
518 std::string launch_date = ctime(&rawtime);
520 for (;i < launch_date.size(); i++)
521 if (launch_date[i] == '/' ||
522 launch_date[i] == '-' ||
523 launch_date[i] == ':' ||
524 launch_date[i] == ' ')
525 launch_date[i] = '_';
526 launch_date.erase(--launch_date.end()); // Last caracter is a \n
532 Launcher::Job::updateJobState()
535 if (_state != "FINISHED" &&
540 if (_batch_job_id.getReference() != "undefined")
542 // A batch manager has been affected to the job
543 Batch::JobInfo job_info = _batch_job_id.queryJob();
544 Batch::Parametre par = job_info.getParametre();
545 _state = par[Batch::STATE].str();
546 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
547 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
548 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
557 Launcher::Job::getBatchJob()
564 Launcher::Job::common_job_params()
566 Batch::Parametre params;
568 params[Batch::NAME] = getJobName();
569 params[Batch::NBPROC] = _resource_required_params.nb_proc;
570 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
572 // Memory in megabytes
573 if (_resource_required_params.mem_mb > 0)
575 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
577 else if (_mem_per_cpu > 0)
579 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
582 // We define a default directory
583 if (_work_directory == "")
585 const size_t BUFSIZE = 32;
587 time_t curtime = time(NULL);
588 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
589 if(!_resource_definition.working_directory.empty())
591 std::string job_dir = std::string("/job_") + date;
592 _work_directory = _resource_definition.working_directory + job_dir;
596 _work_directory = std::string("/$HOME/Batch/workdir_");
597 _work_directory += date;
600 params[Batch::WORKDIR] = _work_directory;
602 // Parameters for COORM
603 params[Batch::LAUNCHER_FILE] = _launcher_file;
604 params[Batch::LAUNCHER_ARGS] = _launcher_args;
606 // If result_directory is not defined, we use HOME environnement
607 if (_result_directory == "")
608 _result_directory = getenv("HOME");
611 std::list<std::string> in_files(_in_files);
612 in_files.push_back(_job_file);
614 in_files.push_back(_env_file);
615 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
617 std::string file = *it;
619 // local file -> If file is not an absolute path, we apply _local_directory
620 std::string local_file;
621 if (file.substr(0, 1) == std::string("/"))
625 local_file = _local_directory + "/" + file;
630 // remote file -> get only file name from in_files
631 size_t found = file.find_last_of("/");
632 std::string remote_file = _work_directory + "/" + file.substr(found+1);
634 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
638 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
640 std::string file = *it;
641 // remote file -> If file is not an absolute path, we apply _work_directory
642 std::string remote_file;
643 std::string local_file;
644 if (file.substr(0, 1) == std::string("/"))
647 size_t found = file.find_last_of("/");
648 local_file = file.substr(found+1);
652 remote_file = _work_directory + "/" + file;
656 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
660 if (_maximum_duration_in_second != -1)
661 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
665 params[Batch::QUEUE] = _queue;
669 params[Batch::EXCLUSIVE] = true;
673 params[Batch::WCKEY] = _wckey;
676 if (_extra_params != "")
677 params[Batch::EXTRAPARAMS] = _extra_params;
679 // Specific parameters
680 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
681 if (it != _specific_parameters.end())
682 params["LL_JOBTYPE"] = it->second;
687 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
689 _batch_job_id = batch_manager_job_id;
693 Launcher::Job::getBatchManagerJobId() const
695 return _batch_job_id;
700 Launcher::Job::addSpecificParameter(const std::string & name,
701 const std::string & value)
703 _specific_parameters[name] = value;
706 const std::map<std::string, std::string> &
707 Launcher::Job::getSpecificParameters() const
709 return _specific_parameters;
713 Launcher::Job::checkSpecificParameters()