1 // Copyright (C) 2009-2013 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 #include "BL_JobsManager_QT.hxx"
21 #include "BL_GenericGui.hxx"
26 // To tokenize a string
27 static void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ");
29 BL::JobManagerEvent::JobManagerEvent(const std::string & action_i,
30 const std::string & event_name_i,
31 const std::string & job_name_i,
32 const std::string & data_i) : QEvent(QEvent::User)
35 event_name = event_name_i;
36 job_name = job_name_i;
40 BL::JobManagerEvent::~JobManagerEvent() {}
42 BL::JobsManager_QT::JobsManager_QT(QWidget * parent, BL::GenericGui * main_gui, BL::SALOMEServices * salome_services) :
43 QDockWidget(parent), BL::JobsManager(salome_services)
45 DEBTRACE("Creating BL::JobsManager_QT");
48 _model_manager = NULL;
52 QWidget * main_widget = new QWidget(this);
54 _load_jobs = new QPushButton("Load Jobs");
55 _save_jobs = new QPushButton("Save Jobs");
56 connect(_load_jobs, SIGNAL(clicked()), this, SLOT(load_jobs_button()));
57 connect(_save_jobs, SIGNAL(clicked()), this, SLOT(save_jobs_button()));
59 _auto_refresh_jobs = new QPushButton("Auto Refresh: no");
60 _timer = new QTimer(this);
62 connect(_timer, SIGNAL(timeout()), this, SLOT(RefreshJobs()));
64 // Menu for auto refresh
65 QMenu * refresh_menu = new QMenu(this);
66 refresh_menu->addAction("No", this, SLOT(no_auto_refresh()));
67 refresh_menu->addAction("10 seconds", this, SLOT(ten_seconds_refresh()));
68 refresh_menu->addAction("30 seconds", this, SLOT(thirty_seconds_refresh()));
69 refresh_menu->addAction("1 minute", this, SLOT(one_minute_refresh()));
70 refresh_menu->addAction("5 minutes", this, SLOT(five_minutes_refresh()));
71 refresh_menu->addAction("30 minutes", this, SLOT(thirty_minutes_refresh()));
72 refresh_menu->addAction("1 hour", this, SLOT(one_hour_refresh()));
73 _auto_refresh_jobs->setMenu(refresh_menu);
75 QHBoxLayout * button_layout = new QHBoxLayout();
76 button_layout->addWidget(_load_jobs);
77 button_layout->addWidget(_save_jobs);
78 button_layout->addWidget(_auto_refresh_jobs);
80 QGroupBox * message_box = new QGroupBox("Messages");
81 _log = new QTextEdit(this);
82 _log->setReadOnly(true);
83 QVBoxLayout * message_box_layout = new QVBoxLayout(message_box);
84 message_box_layout->addWidget(_log);
85 message_box->setLayout(message_box_layout);
87 QVBoxLayout * mainLayout = new QVBoxLayout();
88 mainLayout->addLayout(button_layout);
89 mainLayout->addWidget(message_box);
90 main_widget->setLayout(mainLayout);
92 QScrollArea * scroll_widget = new QScrollArea(this);
93 scroll_widget->setWidget(main_widget);
94 scroll_widget->setWidgetResizable(true);
95 setWidget(scroll_widget);
96 setWindowTitle("Job Manager");
99 BL::JobsManager_QT::~JobsManager_QT()
101 DEBTRACE("Destroying BL::JobsManager_QT");
105 BL::JobsManager_QT::set_model_manager(BL::QModelManager * model_manager)
107 _model_manager = model_manager;
111 BL::JobsManager_QT::load_jobs_button()
113 DEBTRACE("load_jobs");
114 QString jobs_file = QFileDialog::getOpenFileName(this,
115 tr("Choose an xml jobs file"), "",
116 tr("xml (*.xml);;All Files (*)"));
119 write_normal_text("Load jobs action cancelled\n");
122 load_jobs(jobs_file.toStdString());
126 BL::JobsManager_QT::save_jobs_button()
128 DEBTRACE("save_jobs");
129 QFileDialog dialog(this, "Save jobs file");
131 filters << "XML files (*.xml)"
133 dialog.setFileMode(QFileDialog::AnyFile);
134 dialog.setFilters(filters);
135 dialog.selectFilter("(*.xml)");
136 dialog.setDefaultSuffix("xml");
137 dialog.setConfirmOverwrite(true);
138 dialog.setAcceptMode(QFileDialog::AcceptSave);
139 QString jobs_file("");
140 QStringList fileNames;
142 if (bool ret = dialog.exec())
144 DEBTRACE(ret << " " << dialog.confirmOverwrite());
145 fileNames = dialog.selectedFiles();
146 if (!fileNames.isEmpty())
147 jobs_file= fileNames.first();
151 write_normal_text("Save jobs action cancelled\n");
154 save_jobs(jobs_file.toStdString());
158 BL::JobsManager_QT::RefreshJobs()
164 BL::JobsManager_QT::no_auto_refresh()
166 _auto_refresh_jobs->setText("Auto Refresh: no");
171 BL::JobsManager_QT::ten_seconds_refresh()
173 _auto_refresh_jobs->setText("Auto Refresh: 10s");
175 _timer->start(10 * 1000);
179 BL::JobsManager_QT::thirty_seconds_refresh()
181 _auto_refresh_jobs->setText("Auto Refresh: 30s");
183 _timer->start(30 * 1000);
187 BL::JobsManager_QT::one_minute_refresh()
189 _auto_refresh_jobs->setText("Auto Refresh: 1min");
191 _timer->start(1 * 60 * 1000);
195 BL::JobsManager_QT::five_minutes_refresh()
197 _auto_refresh_jobs->setText("Auto Refresh: 5min");
199 _timer->start(5 * 60 * 1000);
203 BL::JobsManager_QT::thirty_minutes_refresh()
205 _auto_refresh_jobs->setText("Auto Refresh: 30min");
207 _timer->start(30 * 60 * 1000);
211 BL::JobsManager_QT::one_hour_refresh()
213 _auto_refresh_jobs->setText("Auto Refresh: 1hour");
215 _timer->start(1 * 60 * 60 * 1000);
219 BL::JobsManager_QT::restart_job(const std::string & name)
221 DEBTRACE("Restart job with name: " << name);
222 BL::CreateJobWizard wizard(this, _salome_services);
225 wizard.job_name = name;
226 wizard.start_job = true;
227 _main_gui->delete_job_internal();
228 create_job_with_wizard(wizard);
232 BL::JobsManager_QT::edit_clone_job(const std::string & name)
234 BL::CreateJobWizard wizard(this, _salome_services);
238 // Check if the job has the same name
239 if (name == wizard.job_name)
241 DEBTRACE("Job " << name << " has been edited");
242 _main_gui->delete_job_internal();
245 if (wizard.job_name != "")
247 create_job_with_wizard(wizard);
251 DEBTRACE("User cancel Create Job Wizard");
256 BL::JobsManager_QT::create_job()
258 BL::CreateJobWizard wizard(this, _salome_services);
260 if (wizard.job_name != "")
262 create_job_with_wizard(wizard);
266 DEBTRACE("User cancel Create Job Wizard");
271 BL::JobsManager_QT::create_job_with_wizard(BL::CreateJobWizard & wizard)
273 BL::Job * new_job = createJob(wizard.job_name);
274 if (wizard.yacs_file != "")
277 new_job->setType(BL::Job::YACS_SCHEMA);
278 new_job->setJobFile(wizard.yacs_file);
279 new_job->setDumpYACSState(wizard.dump_yacs_state);
281 else if (wizard.command != "")
284 new_job->setType(BL::Job::COMMAND);
285 new_job->setJobFile(wizard.command);
287 else if (wizard.python_salome_file != "")
290 new_job->setType(BL::Job::PYTHON_SALOME);
291 new_job->setJobFile(wizard.python_salome_file);
295 new_job->setEnvFile(wizard.env_file);
296 BL::Job::BatchParam param;
299 if (wizard.coorm_batch_directory != "")
301 param.batch_directory = wizard.coorm_batch_directory;
303 else if (wizard.batch_directory != "")
305 param.batch_directory = wizard.batch_directory;
308 param.maximum_duration = wizard.maximum_duration;
309 param.expected_memory = wizard.expected_memory;
310 param.nb_proc = wizard.nb_proc;
311 param.exclusive = wizard.exclusive;
313 // Parameters for COORM
314 param.launcher_file = wizard.launcher_file;
315 param.launcher_args = wizard.launcher_args;
317 new_job->setBatchParameters(param);
318 BL::Job::FilesParam files_params;
319 files_params.result_directory = wizard.result_directory;
320 files_params.input_files_list = wizard.input_files_list;
321 files_params.output_files_list = wizard.output_files_list;
322 new_job->setFilesParameters(files_params);
323 new_job->setResource(wizard.resource_choosed);
324 new_job->setBatchQueue(wizard.batch_queue);
325 new_job->setLoadLevelerJobType(wizard.ll_jobtype);
328 addJobToLauncher(wizard.job_name);
329 emit new_job_added(QString::fromStdString(wizard.job_name));
330 QStandardItemModel * model = _model_manager->getModel();
331 QList<QStandardItem *> item_list = model->findItems(QString::fromStdString(wizard.job_name));
332 QStandardItem * job_state_item = model->item(item_list.at(0)->row(), 2);
333 _main_gui->_jobs_table->selectRow(item_list.at(0)->row());
334 if (wizard.start_job)
335 start_job(wizard.job_name);
339 BL::JobsManager_QT::delete_job(QString job_name)
341 BL::JobsManager::removeJob(job_name.toStdString());
342 _model_manager->delete_job(job_name);
343 _main_gui->_job_tab->reset(job_name);
347 BL::JobsManager_QT::sendEvent(const std::string & action,
348 const std::string & event_name,
349 const std::string & job_name,
350 const std::string & data)
352 DEBTRACE("sendEvent BL::JobsManager_QT");
354 // Sending a QEvent to go back to main thread
355 BL::JobManagerEvent * event = new JobManagerEvent(action, event_name, job_name, data);
356 QApplication::postEvent(this, event);
360 BL::JobsManager_QT::event(QEvent * e)
362 QDockWidget::event(e);
363 JobManagerEvent * event = dynamic_cast<JobManagerEvent*>(e);
364 if (!event) return false;
366 DEBTRACE("BL::JobsManager_QT Receiving event : "
367 << event->action << " "
368 << event->event_name << " "
369 << event->job_name << " "
372 if (event->action == "create_job")
374 if (event->event_name == "Ok")
376 QString str((event->job_name).c_str());
377 write_normal_text("Job " + str + " created\n");
381 QString str((event->job_name).c_str());
382 write_error_text("Error in creating job: " + str + "\n");
383 write_error_text("*** ");
384 write_error_text((event->data).c_str());
385 write_error_text(" ***\n");
388 else if (event->action == "start_job")
390 if (event->event_name == "Ok")
392 QString str((event->job_name).c_str());
393 write_normal_text("Job " + str + " queued\n");
397 QString str((event->job_name).c_str());
398 write_error_text("Error in starting job: " + str + "\n");
399 write_error_text("*** ");
400 write_error_text((event->data).c_str());
401 write_error_text(" ***\n");
403 emit job_state_changed(QString((event->job_name).c_str()));
405 else if (event->action == "refresh_job")
407 if (event->event_name == "Ok")
409 QString name((event->job_name).c_str());
410 QString state((event->data).c_str());
411 state = state.toLower();
412 write_normal_text("Job " + name + " new state is " + state + "\n");
413 emit job_state_changed(QString((event->job_name).c_str()));
417 QString str((event->job_name).c_str());
418 write_error_text("Error in refreshing job: " + str + "\n");
419 write_error_text("*** ");
420 write_error_text((event->data).c_str());
421 write_error_text(" ***\n");
424 else if (event->action == "delete_job")
426 if (event->event_name == "Ok")
428 QString str((event->job_name).c_str());
429 write_normal_text("Job " + str + " deleted\n");
433 QString str((event->job_name).c_str());
434 write_error_text("Warning delete job: " + str + " maybe not complete, exception catch in SALOME Launcher service\n");
435 write_error_text("*** ");
436 write_error_text((event->data).c_str());
437 write_error_text(" ***\n");
440 else if (event->action == "get_results_job")
442 if (event->event_name == "Ok")
444 QString str((event->job_name).c_str());
445 write_normal_text("Results of Job " + str + " are get\n");
449 QString str((event->job_name).c_str());
450 write_error_text("Warning for results of job: " + str + " maybe not complete, exception catch in SALOME Launcher service\n");
451 write_error_text("*** ");
452 write_error_text((event->data).c_str());
453 write_error_text(" ***\n");
456 else if (event->action == "stop_job")
458 if (event->event_name == "Ok")
460 QString str((event->job_name).c_str());
461 write_normal_text("Job " + str + " is stopped\n");
465 QString str((event->job_name).c_str());
466 write_error_text("Error when trying to stop job: " + str + "\n");
467 write_error_text("*** ");
468 write_error_text((event->data).c_str());
469 write_error_text(" ***\n");
472 else if (event->action == "get_assigned_hostnames")
474 if (event->event_name == "Ok")
476 QString str((event->job_name).c_str());
478 vector<string> hostnames;
480 Tokenize(event->data, hostnames, "+");
482 vector<string>::iterator it;
484 write_normal_text("Job " + str + " assigned hostnames are :\n");
486 for (it = hostnames.begin(); it < hostnames.end(); it++)
488 vector<string> hostname;
489 Tokenize(*it, hostname, ".");
490 QString assigned_hostname(hostname[0].c_str());
491 write_normal_text("+ " + assigned_hostname + "\n");
496 // Do nothing in case the batch manager does not support this
499 else if (event->action == "save_jobs")
501 if (event->event_name == "Error")
503 write_error_text("Error in saving jobs: \n");
504 write_error_text("*** ");
505 write_error_text((event->data).c_str());
506 write_error_text(" ***\n");
510 QString str((event->data).c_str());
511 write_normal_text("Jobs saved in file " + str + "\n");
514 else if (event->action == "load_jobs")
516 if (event->event_name == "Error")
518 write_error_text("Error in loading jobs: \n");
519 write_error_text("*** ");
520 write_error_text((event->data).c_str());
521 write_error_text(" ***\n");
525 QString str((event->data).c_str());
526 write_normal_text("Jobs loaded from file " + str + "\n");
529 else if (event->action == "add_job")
531 if (event->event_name == "Ok")
533 QString str((event->job_name).c_str());
534 write_normal_text("New job added " + str + "\n");
535 emit new_job_added(str);
538 else if (event->action == "to_remove_job")
540 if (event->event_name == "Ok")
541 _main_gui->delete_job_external((event->job_name).c_str());
545 QString str((event->action).c_str());
546 write_error_text("Unknown type of event received:" + str + "\n");
552 BL::JobsManager_QT::write_normal_text(const QString & text)
554 _log->setReadOnly(false);
555 QTextCursor cursor = _log->textCursor();
556 QTextCharFormat text_format;
557 text_format.setForeground(Qt::darkBlue);
558 cursor.insertText(text, text_format);
559 _log->setTextCursor(cursor);
560 _log->setReadOnly(true);
564 BL::JobsManager_QT::write_error_text(const QString & text)
566 _log->setReadOnly(false);
567 QTextCursor cursor = _log->textCursor();
568 QTextCharFormat text_format;
569 text_format.setForeground(Qt::red);
570 cursor.insertText(text, text_format);
571 _log->setTextCursor(cursor);
572 _log->setReadOnly(true);
575 // To tokenize a string
576 void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
578 // Skip delimiters at beginning.
579 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
580 // Find first "non-delimiter".
581 string::size_type pos = str.find_first_of(delimiters, lastPos);
583 while (string::npos != pos || string::npos != lastPos)
585 // Found a token, add it to the vector.
586 tokens.push_back(str.substr(lastPos, pos - lastPos));
587 // Skip delimiters. Note the "not_of"
588 lastPos = str.find_first_not_of(delimiters, pos);
589 // Find next "non-delimiter"
590 pos = str.find_first_of(delimiters, lastPos);