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::setReference(const std::string & reference)
331 _reference = reference;
335 Launcher::Job::getWorkDirectory() const
337 return _work_directory;
341 Launcher::Job::getLocalDirectory() const
343 return _local_directory;
347 Launcher::Job::getResultDirectory() const
349 return _result_directory;
352 const std::list<std::string> &
353 Launcher::Job::get_in_files() const
358 const std::list<std::string> &
359 Launcher::Job::get_out_files() const
365 Launcher::Job::getMaximumDuration() const
367 return _maximum_duration;
372 Launcher::Job::getLauncherFile() const
374 return _launcher_file;
377 Launcher::Job::getLauncherArgs() const
379 return _launcher_args;
383 Launcher::Job::getResourceRequiredParams() const
385 return _resource_required_params;
389 Launcher::Job::getQueue() const
395 Launcher::Job::getExclusive() const
401 Launcher::Job::getExclusiveStr() const
403 return _exclusive ? "true" : "false";
407 Launcher::Job::getMemPerCpu() const
413 Launcher::Job::getReference() const
419 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
421 std::string result("");
422 std::string edt_value = maximum_duration;
423 std::size_t pos = edt_value.find(":");
425 if (edt_value != "") {
426 if (pos == edt_value.npos) {
427 throw LauncherException("[Launcher::Job::checkMaximumDuration] Error on definition: " + edt_value);
429 std::string begin_edt_value = edt_value.substr(0, pos);
430 std::string mid_edt_value = edt_value.substr(pos, 1);
431 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
434 std::istringstream iss(begin_edt_value);
435 if (!(iss >> value)) {
436 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
438 else if (value < 0) {
439 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
441 std::istringstream iss_2(end_edt_value);
442 if (!(iss_2 >> value)) {
443 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
445 else if (value < 0) {
446 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
448 if (mid_edt_value != ":") {
449 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
453 throw LauncherException(result);
457 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
459 // nb_proc has be to > 0
460 if (resource_required_params.nb_proc <= 0)
462 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
463 throw LauncherException(message);
468 Launcher::Job::convertMaximumDuration(const std::string & edt)
472 if( edt.size() == 0 )
475 std::string::size_type pos = edt.find(":");
476 std::string h = edt.substr(0,pos);
477 std::string m = edt.substr(pos+1,edt.size()-pos+1);
478 std::istringstream issh(h);
480 std::istringstream issm(m);
489 Launcher::Job::getLaunchDate() const
493 std::string launch_date = ctime(&rawtime);
495 for (;i < launch_date.size(); i++)
496 if (launch_date[i] == '/' ||
497 launch_date[i] == '-' ||
498 launch_date[i] == ':' ||
499 launch_date[i] == ' ')
500 launch_date[i] = '_';
501 launch_date.erase(--launch_date.end()); // Last caracter is a \n
507 Launcher::Job::updateJobState()
510 if (_state != "FINISHED" &&
515 if (_batch_job_id.getReference() != "undefined")
517 // A batch manager has been affected to the job
518 Batch::JobInfo job_info = _batch_job_id.queryJob();
519 Batch::Parametre par = job_info.getParametre();
520 _state = par[Batch::STATE].str();
521 _assigned_hostnames = (par.find(Batch::ASSIGNEDHOSTNAMES) == par.end())?
522 "" : par[Batch::ASSIGNEDHOSTNAMES].str();
523 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
532 Launcher::Job::getBatchJob()
539 Launcher::Job::common_job_params()
541 Batch::Parametre params;
543 params[Batch::NAME] = getJobName();
544 params[Batch::NBPROC] = _resource_required_params.nb_proc;
545 params[Batch::NBPROCPERNODE] = _resource_required_params.nb_proc_per_node;
547 // Memory in megabytes
548 if (_resource_required_params.mem_mb > 0)
550 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb;
552 else if (_mem_per_cpu > 0)
554 params[Batch::MEMPERCPU] = (long)_mem_per_cpu;
557 // We define a default directory based on user time
558 if (_work_directory == "")
560 const size_t BUFSIZE = 32;
562 time_t curtime = time(NULL);
563 strftime(date, BUFSIZE, "%Y_%m_%d__%H_%M_%S", localtime(&curtime));
564 _work_directory = std::string("$HOME/Batch/workdir_");
565 _work_directory += date;
567 params[Batch::WORKDIR] = _work_directory;
569 // Parameters for COORM
570 params[Batch::LAUNCHER_FILE] = _launcher_file;
571 params[Batch::LAUNCHER_ARGS] = _launcher_args;
573 // If result_directory is not defined, we use HOME environnement
574 if (_result_directory == "")
575 _result_directory = getenv("HOME");
578 std::list<std::string> in_files(_in_files);
579 in_files.push_back(_job_file);
581 in_files.push_back(_env_file);
582 for(std::list<std::string>::iterator it = in_files.begin(); it != in_files.end(); it++)
584 std::string file = *it;
586 // local file -> If file is not an absolute path, we apply _local_directory
587 std::string local_file;
588 if (file.substr(0, 1) == std::string("/"))
592 local_file = _local_directory + "/" + file;
597 // remote file -> get only file name from in_files
598 size_t found = file.find_last_of("/");
599 std::string remote_file = _work_directory + "/" + file.substr(found+1);
601 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
605 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
607 std::string file = *it;
610 size_t found = file.find_last_of("/");
611 std::string local_file = _result_directory + "/" + file.substr(found+1);
613 // remote file -> If file is not an absolute path, we apply _work_directory
614 std::string remote_file;
615 if (file.substr(0, 1) == std::string("/"))
618 remote_file = _work_directory + "/" + file;
620 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
624 if (_maximum_duration_in_second != -1)
625 params[Batch::MAXWALLTIME] = _maximum_duration_in_second / 60;
629 params[Batch::QUEUE] = _queue;
633 params[Batch::EXCLUSIVE] = true;
635 // Specific parameters
636 std::map<std::string, std::string>::iterator it = _specific_parameters.find("LoalLevelerJobType");
637 if (it != _specific_parameters.end())
638 params["LL_JOBTYPE"] = it->second;
643 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
645 _batch_job_id = batch_manager_job_id;
649 Launcher::Job::getBatchManagerJobId() const
651 return _batch_job_id;
656 Launcher::Job::addSpecificParameter(const std::string & name,
657 const std::string & value)
659 _specific_parameters[name] = value;
662 const std::map<std::string, std::string> &
663 Launcher::Job::getSpecificParameters() const
665 return _specific_parameters;
669 Launcher::Job::checkSpecificParameters()