1 // Copyright (C) 2009-2010 CEA/DEN, EDF R&D
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.
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"
29 _launch_date = getLaunchDate();
34 _job_file_name_complete = "";
36 _local_directory = "";
37 _result_directory = "";
38 _maximum_duration = "";
39 _maximum_duration_in_second = -1;
40 _resource_required_params.name = "";
41 _resource_required_params.hostname = "";
42 _resource_required_params.OS = "";
43 _resource_required_params.nb_proc = -1;
44 _resource_required_params.nb_node = -1;
45 _resource_required_params.nb_proc_per_node = -1;
46 _resource_required_params.cpu_clock = -1;
47 _resource_required_params.mem_mb = -1;
51 _batch_job = new Batch::Job();
57 LAUNCHER_MESSAGE("Deleting job number: " << _number);
59 if (_batch_job_id.getReference() != "undefined")
63 _batch_job_id.deleteJob();
65 catch (const Batch::EmulationException &ex)
67 LAUNCHER_INFOS("WARNING: exception when deleting the job: " << ex.message);
76 Launcher::Job::setState(const std::string & state)
78 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, FAILED
79 if (state != "CREATED" &&
82 state != "FINISHED" &&
85 throw LauncherException("Bad state, this state does not exist: " + state);
91 Launcher::Job::getState()
97 Launcher::Job::setNumber(const int & number)
100 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
105 Launcher::Job::getNumber()
111 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
113 // Check machine_definition
114 std::string user_name = "";
115 if (resource_definition.UserName == "")
117 user_name = getenv("USER");
120 std::string mess = "You must define a user name: into your resource description or with env variable USER";
121 throw LauncherException(mess);
125 user_name = resource_definition.UserName;
127 _resource_definition = resource_definition;
128 _resource_definition.UserName = user_name;
132 Launcher::Job::getResourceDefinition()
134 return _resource_definition;
138 Launcher::Job::setJobFile(const std::string & job_file)
143 std::string mess = "Empty Job File is forbidden !";
144 throw LauncherException(mess);
147 _job_file = job_file;
148 std::string::size_type p1 = _job_file.find_last_of("/");
149 std::string::size_type p2 = _job_file.find_last_of(".");
150 _job_file_name_complete = _job_file.substr(p1+1);
151 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
154 add_in_file(_job_file);
158 Launcher::Job::getJobFile()
163 Launcher::Job::setEnvFile(const std::string & env_file)
165 _env_file = env_file;
167 add_in_file(_env_file);
171 Launcher::Job::getEnvFile()
177 Launcher::Job::setWorkDirectory(const std::string & work_directory)
179 _work_directory = work_directory;
183 Launcher::Job::setLocalDirectory(const std::string & local_directory)
185 _local_directory = local_directory;
189 Launcher::Job::setResultDirectory(const std::string & result_directory)
191 _result_directory = result_directory;
195 Launcher::Job::add_in_file(const std::string & file)
197 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
198 if (it == _in_files.end())
199 _in_files.push_back(file);
201 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
205 Launcher::Job::add_out_file(const std::string & file)
207 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
208 if (it == _out_files.end())
209 _out_files.push_back(file);
211 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
215 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
217 checkMaximumDuration(maximum_duration);
218 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
219 _maximum_duration = maximum_duration;
223 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
225 checkResourceRequiredParams(resource_required_params);
226 _resource_required_params = resource_required_params;
230 Launcher::Job::setQueue(const std::string & queue)
236 Launcher::Job::getWorkDirectory()
238 return _work_directory;
242 Launcher::Job::getLocalDirectory()
244 return _local_directory;
248 Launcher::Job::getResultDirectory()
250 return _result_directory;
253 const std::list<std::string> &
254 Launcher::Job::get_in_files()
259 const std::list<std::string> &
260 Launcher::Job::get_out_files()
266 Launcher::Job::getMaximumDuration()
268 return _maximum_duration;
272 Launcher::Job::getResourceRequiredParams()
274 return _resource_required_params;
278 Launcher::Job::getQueue()
284 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
286 std::string result("");
287 std::string edt_value = maximum_duration;
288 std::size_t pos = edt_value.find(":");
290 if (edt_value != "") {
291 std::string begin_edt_value = edt_value.substr(0, pos);
292 std::string mid_edt_value = edt_value.substr(pos, 1);
293 std::string end_edt_value = edt_value.substr(pos + 1, edt_value.npos);
296 std::istringstream iss(begin_edt_value);
297 if (!(iss >> value)) {
298 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
300 else if (value < 0) {
301 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
303 std::istringstream iss_2(end_edt_value);
304 if (!(iss_2 >> value)) {
305 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
307 else if (value < 0) {
308 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
310 if (mid_edt_value != ":") {
311 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
315 throw LauncherException(result);
319 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
321 // nb_proc has be to > 0
322 if (resource_required_params.nb_proc <= 0)
324 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
325 throw LauncherException(message);
330 Launcher::Job::convertMaximumDuration(const std::string & edt)
334 if( edt.size() == 0 )
337 std::string::size_type pos = edt.find(":");
338 std::string h = edt.substr(0,pos);
339 std::string m = edt.substr(pos+1,edt.size()-pos+1);
340 std::istringstream issh(h);
342 std::istringstream issm(m);
351 Launcher::Job::getLaunchDate()
355 std::string launch_date = ctime(&rawtime);
357 for (;i < launch_date.size(); i++)
358 if (launch_date[i] == '/' ||
359 launch_date[i] == '-' ||
360 launch_date[i] == ':' ||
361 launch_date[i] == ' ')
362 launch_date[i] = '_';
363 launch_date.erase(--launch_date.end()); // Last caracter is a \n
369 Launcher::Job::updateJobState()
372 if (_batch_job_id.getReference() != "undefined")
374 // A batch manager has been affected to the job
375 Batch::JobInfo job_info = _batch_job_id.queryJob();
376 Batch::Parametre par = job_info.getParametre();
378 LAUNCHER_MESSAGE("State received is: " << par[Batch::STATE].str());
380 // TODO: Remove this if all tests pass with the new libBatch, otherwise fix the codes in libBatch
381 // Patch until new LIBBATCH version
382 // eSSH Client and ePBS Client and eSGE
383 /* if (par[STATE].str() == "Running" or par[STATE].str() == "E" or par[STATE].str() == "R" or par[STATE].str() == "r" or par[STATE].str() == "RUN")
385 else if (par[STATE].str() == "Stopped")
387 else if (par[STATE].str() == "Done" or par[STATE].str() == "U" or par[STATE].str() == "e" or par[STATE].str() == "DONE" or par[STATE].str() == "EXIT")
389 else if (par[STATE].str() == "Dead" or par[STATE].str() == "Eqw")
391 else if (par[STATE].str() == "Q" or par[STATE].str() == "qw" or par[STATE].str() == "PEN")
393 _state = par[Batch::STATE].str();
401 Launcher::Job::getBatchJob()
408 Launcher::Job::common_job_params()
410 Batch::Parametre params;
412 params[Batch::USER] = _resource_definition.UserName;
413 params[Batch::NBPROC] = _resource_required_params.nb_proc;
416 if (_resource_required_params.mem_mb > 0)
418 // Memory is in kilobytes
419 params[Batch::MAXRAMSIZE] = _resource_required_params.mem_mb * 1024;
422 // We define a default directory based on user time
423 if (_work_directory == "")
426 Batch::Date date = Batch::Date(time(0));
427 thedate = date.str();
428 int lend = thedate.size() ;
431 if ( thedate[i] == '/' || thedate[i] == '-' || thedate[i] == ':' ) {
436 _work_directory = std::string("$HOME/Batch/");
437 _work_directory += thedate;
439 params[Batch::WORKDIR] = _work_directory;
440 params[Batch::TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
442 // If result_directory is not defined, we use HOME environnement
443 if (_result_directory == "")
444 _result_directory = getenv("HOME");
447 for(std::list<std::string>::iterator it = _in_files.begin(); it != _in_files.end(); it++)
449 std::string file = *it;
451 // local file -> If file is not an absolute path, we apply _local_directory
452 std::string local_file;
453 if (file.substr(0, 1) == std::string("/"))
456 local_file = _local_directory + "/" + file;
458 // remote file -> get only file name from _in_files
459 size_t found = file.find_last_of("/");
460 std::string remote_file = _work_directory + "/" + file.substr(found+1);
462 params[Batch::INFILE] += Batch::Couple(local_file, remote_file);
466 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
468 std::string file = *it;
471 size_t found = file.find_last_of("/");
472 std::string local_file = _result_directory + "/" + file.substr(found+1);
474 // remote file -> If file is not an absolute path, we apply _work_directory
475 std::string remote_file;
476 if (file.substr(0, 1) == std::string("/"))
479 remote_file = _work_directory + "/" + file;
481 params[Batch::OUTFILE] += Batch::Couple(local_file, remote_file);
485 if (_maximum_duration_in_second != -1)
486 params[Batch::MAXWALLTIME] = _maximum_duration_in_second;
490 params[Batch::QUEUE] = _queue;
496 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
498 _batch_job_id = batch_manager_job_id;
502 Launcher::Job::getBatchManagerJobId()
504 return _batch_job_id;