1 // Copyright (C) 2009 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
19 // Author: André RIBES - EDF R&D
21 #include "Launcher_Job.hxx"
22 #include "Launcher.hxx"
28 _launch_date = getLaunchDate();
33 _job_file_name_complete = "";
35 _local_directory = "";
36 _result_directory = "";
37 _maximum_duration = "";
38 _maximum_duration_in_second = -1;
39 _resource_required_params.name = "";
40 _resource_required_params.hostname = "";
41 _resource_required_params.OS = "";
42 _resource_required_params.nb_proc = -1;
43 _resource_required_params.nb_node = -1;
44 _resource_required_params.nb_proc_per_node = -1;
45 _resource_required_params.cpu_clock = -1;
46 _resource_required_params.mem_mb = -1;
50 _batch_job = new Batch::Job();
56 LAUNCHER_MESSAGE("Deleting job number: " << _number);
58 if (_batch_job_id.getReference() != "undefined")
62 _batch_job_id.deleteJob();
64 catch (const Batch::EmulationException &ex)
66 LAUNCHER_INFOS("WARNING: exception when deleting the job: " << ex.message);
75 Launcher::Job::setState(const std::string & state)
77 // State of a Job: CREATED, QUEUED, RUNNING, FINISHED, ERROR
78 if (state != "CREATED" and
80 state != "RUNNING" and
81 state != "FINISHED" and
84 throw LauncherException("Bad state, this state does not exist: " + state);
90 Launcher::Job::getState()
96 Launcher::Job::setNumber(const int & number)
99 std::cerr << "Launcher::Job::setNumber -- Job number was already defined, before: " << _number << " now: " << number << std::endl;
104 Launcher::Job::getNumber()
110 Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_definition)
112 // Check machine_definition
113 std::string user_name = "";
114 if (resource_definition.UserName == "")
116 user_name = getenv("USER");
119 std::string mess = "You must define a user name: into your resource description or with env variable USER";
120 throw LauncherException(mess);
124 user_name = resource_definition.UserName;
126 _resource_definition = resource_definition;
127 _resource_definition.UserName = user_name;
131 Launcher::Job::getResourceDefinition()
133 return _resource_definition;
137 Launcher::Job::setJobFile(const std::string & job_file)
142 std::string mess = "Empty Job File is forbidden !";
143 throw LauncherException(mess);
146 _job_file = job_file;
147 std::string::size_type p1 = _job_file.find_last_of("/");
148 std::string::size_type p2 = _job_file.find_last_of(".");
149 _job_file_name_complete = _job_file.substr(p1+1);
150 _job_file_name = _job_file.substr(p1+1,p2-p1-1);
153 add_in_file(_job_file);
157 Launcher::Job::getJobFile()
162 Launcher::Job::setEnvFile(const std::string & env_file)
164 _env_file = env_file;
166 add_in_file(_env_file);
170 Launcher::Job::getEnvFile()
176 Launcher::Job::setWorkDirectory(const std::string & work_directory)
178 _work_directory = work_directory;
182 Launcher::Job::setLocalDirectory(const std::string & local_directory)
184 _local_directory = local_directory;
188 Launcher::Job::setResultDirectory(const std::string & result_directory)
190 _result_directory = result_directory;
194 Launcher::Job::add_in_file(const std::string & file)
196 std::list<std::string>::iterator it = std::find(_in_files.begin(), _in_files.end(), file);
197 if (it == _in_files.end())
198 _in_files.push_back(file);
200 std::cerr << "Launcher::Job::add_in_file -- Warning file was already entered in in_files: " << file << std::endl;
204 Launcher::Job::add_out_file(const std::string & file)
206 std::list<std::string>::iterator it = std::find(_out_files.begin(), _out_files.end(), file);
207 if (it == _out_files.end())
208 _out_files.push_back(file);
210 std::cerr << "Launcher::Job::add_out_file -- Warning file was already entered in out_files: " << file << std::endl;
214 Launcher::Job::setMaximumDuration(const std::string & maximum_duration)
216 checkMaximumDuration(maximum_duration);
217 _maximum_duration_in_second = convertMaximumDuration(maximum_duration);
218 _maximum_duration = maximum_duration;
222 Launcher::Job::setResourceRequiredParams(const resourceParams & resource_required_params)
224 checkResourceRequiredParams(resource_required_params);
225 _resource_required_params = resource_required_params;
229 Launcher::Job::setQueue(const std::string & queue)
235 Launcher::Job::getWorkDirectory()
237 return _work_directory;
241 Launcher::Job::getLocalDirectory()
243 return _local_directory;
247 Launcher::Job::getResultDirectory()
249 return _result_directory;
252 const std::list<std::string> &
253 Launcher::Job::get_in_files()
258 const std::list<std::string> &
259 Launcher::Job::get_out_files()
265 Launcher::Job::getMaximumDuration()
267 return _maximum_duration;
271 Launcher::Job::getResourceRequiredParams()
273 return _resource_required_params;
277 Launcher::Job::getQueue()
283 Launcher::Job::checkMaximumDuration(const std::string & maximum_duration)
285 std::string result("");
286 std::string edt_value = maximum_duration;
287 if (edt_value != "") {
288 std::string begin_edt_value = edt_value.substr(0, 2);
289 std::string mid_edt_value = edt_value.substr(2, 1);
290 std::string end_edt_value = edt_value.substr(3);
293 std::istringstream iss(begin_edt_value);
294 if (!(iss >> value)) {
295 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
297 else if (value < 0) {
298 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
300 std::istringstream iss_2(end_edt_value);
301 if (!(iss_2 >> value)) {
302 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! : " + edt_value;
304 else if (value < 0) {
305 result = "[Launcher::Job::checkExpectedDuration] Error on definition time is negative ! : " + value;
307 if (mid_edt_value != ":") {
308 result = "[Launcher::Job::checkExpectedDuration] Error on definition ! :" + edt_value;
312 throw LauncherException(result);
316 Launcher::Job::checkResourceRequiredParams(const resourceParams & resource_required_params)
318 // nb_proc has be to > 0
319 if (resource_required_params.nb_proc <= 0)
321 std::string message("[Launcher::Job::checkResourceRequiredParams] proc number is not > 0 ! ");
322 throw LauncherException(message);
327 Launcher::Job::convertMaximumDuration(const std::string & edt)
331 if( edt.size() == 0 )
334 std::string::size_type pos = edt.find(":");
335 std::string h = edt.substr(0,pos);
336 std::string m = edt.substr(pos+1,edt.size()-pos+1);
337 std::istringstream issh(h);
339 std::istringstream issm(m);
348 Launcher::Job::getLaunchDate()
352 std::string launch_date = ctime(&rawtime);
354 for (;i < launch_date.size(); i++)
355 if (launch_date[i] == '/' or
356 launch_date[i] == '-' or
357 launch_date[i] == ':' or
358 launch_date[i] == ' ')
359 launch_date[i] = '_';
360 launch_date.erase(--launch_date.end()); // Last caracter is a \n
366 Launcher::Job::updateJobState()
369 if (_batch_job_id.getReference() != "undefined")
371 // A batch manager has been affected to the job
372 Batch::JobInfo job_info = _batch_job_id.queryJob();
373 Batch::Parametre par = job_info.getParametre();
375 LAUNCHER_MESSAGE("State received is: " << par[STATE].str());
377 // Patch until new LIBBATCH version
378 // eSSH Client and ePBS Client and eSGE
379 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")
381 else if (par[STATE].str() == "Stopped")
383 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")
385 else if (par[STATE].str() == "Dead" or par[STATE].str() == "Eqw")
387 else if (par[STATE].str() == "Q" or par[STATE].str() == "qw" or par[STATE].str() == "PEN")
396 Launcher::Job::getBatchJob()
403 Launcher::Job::common_job_params()
405 Batch::Parametre params;
407 params[USER] = _resource_definition.UserName;
408 params[NBPROC] = _resource_required_params.nb_proc;
411 if (_resource_required_params.mem_mb > 0)
413 // Memory is in kilobytes
414 params[MAXRAMSIZE] = _resource_required_params.mem_mb * 1024;
417 // We define a default directory based on user time
418 if (_work_directory == "")
421 Batch::Date date = Batch::Date(time(0));
422 thedate = date.str();
423 int lend = thedate.size() ;
426 if ( thedate[i] == '/' || thedate[i] == '-' || thedate[i] == ':' ) {
431 _work_directory = std::string("$HOME/Batch/");
432 _work_directory += thedate;
434 params[WORKDIR] = _work_directory;
435 params[TMPDIR] = _work_directory; // To Compatibility -- remove ??? TODO
437 // If result_directory is not defined, we use HOME environnement
438 if (_result_directory == "")
439 _result_directory = getenv("HOME");
442 for(std::list<std::string>::iterator it = _in_files.begin(); it != _in_files.end(); it++)
444 std::string file = *it;
446 // local file -> If file is not an absolute path, we apply _local_directory
447 std::string local_file;
448 if (file.substr(0, 1) == std::string("/"))
451 local_file = _local_directory + "/" + file;
453 // remote file -> get only file name from _in_files
454 size_t found = file.find_last_of("/");
455 std::string remote_file = _work_directory + "/" + file.substr(found+1);
457 params[INFILE] += Batch::Couple(local_file, remote_file);
461 for(std::list<std::string>::iterator it = _out_files.begin(); it != _out_files.end(); it++)
463 std::string file = *it;
466 size_t found = file.find_last_of("/");
467 std::string local_file = _result_directory + "/" + file.substr(found+1);
469 // remote file -> If file is not an absolute path, we apply _work_directory
470 std::string remote_file;
471 if (file.substr(0, 1) == std::string("/"))
474 remote_file = _work_directory + "/" + file;
476 params[OUTFILE] += Batch::Couple(local_file, remote_file);
480 if (_maximum_duration_in_second != -1)
481 params[MAXWALLTIME] = _maximum_duration_in_second;
485 params[QUEUE] = _queue;
491 Launcher::Job::setBatchManagerJobId(Batch::JobId batch_manager_job_id)
493 _batch_job_id = batch_manager_job_id;
497 Launcher::Job::getBatchManagerJobId()
499 return _batch_job_id;