1 // Copyright (C) 2009-2016 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, 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 #include "BL_JobsManager_QT.hxx"
21 #include "BL_GenericGui.hxx"
23 #include <QApplication>
24 #include <QFileDialog>
26 #include <QHBoxLayout>
28 #include <QPushButton>
29 #include <QScrollArea>
30 #include <QStandardItemModel>
33 #include <QVBoxLayout>
38 // To tokenize a string
39 static void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters = " ");
41 BL::JobManagerEvent::JobManagerEvent(const std::string & action_i,
42 const std::string & event_name_i,
43 const std::string & job_name_i,
44 const std::string & data_i) : QEvent(QEvent::User)
47 event_name = event_name_i;
48 job_name = job_name_i;
52 BL::JobManagerEvent::~JobManagerEvent() {}
54 BL::JobsManager_QT::JobsManager_QT(QWidget * parent, BL::GenericGui * main_gui, BL::SALOMEServices * salome_services) :
55 QDockWidget(parent), BL::JobsManager(salome_services)
57 DEBTRACE("Creating BL::JobsManager_QT");
60 _model_manager = NULL;
64 QWidget * main_widget = new QWidget(this);
66 _load_jobs = new QPushButton("Load Jobs");
67 _save_jobs = new QPushButton("Save Jobs");
68 connect(_load_jobs, SIGNAL(clicked()), this, SLOT(load_jobs_button()));
69 connect(_save_jobs, SIGNAL(clicked()), this, SLOT(save_jobs_button()));
71 _auto_refresh_jobs = new QPushButton("Auto Refresh: no");
72 _timer = new QTimer(this);
74 connect(_timer, SIGNAL(timeout()), this, SLOT(RefreshJobs()));
76 // Menu for auto refresh
77 QMenu * refresh_menu = new QMenu(this);
78 refresh_menu->addAction("No", this, SLOT(no_auto_refresh()));
79 refresh_menu->addAction("10 seconds", this, SLOT(ten_seconds_refresh()));
80 refresh_menu->addAction("30 seconds", this, SLOT(thirty_seconds_refresh()));
81 refresh_menu->addAction("1 minute", this, SLOT(one_minute_refresh()));
82 refresh_menu->addAction("5 minutes", this, SLOT(five_minutes_refresh()));
83 refresh_menu->addAction("30 minutes", this, SLOT(thirty_minutes_refresh()));
84 refresh_menu->addAction("1 hour", this, SLOT(one_hour_refresh()));
85 _auto_refresh_jobs->setMenu(refresh_menu);
87 QHBoxLayout * button_layout = new QHBoxLayout();
88 button_layout->addWidget(_load_jobs);
89 button_layout->addWidget(_save_jobs);
90 button_layout->addWidget(_auto_refresh_jobs);
92 QGroupBox * message_box = new QGroupBox("Messages");
93 _log = new QTextEdit(this);
94 _log->setReadOnly(true);
95 QVBoxLayout * message_box_layout = new QVBoxLayout(message_box);
96 message_box_layout->addWidget(_log);
97 message_box->setLayout(message_box_layout);
99 QVBoxLayout * mainLayout = new QVBoxLayout();
100 mainLayout->addLayout(button_layout);
101 mainLayout->addWidget(message_box);
102 main_widget->setLayout(mainLayout);
104 QScrollArea * scroll_widget = new QScrollArea(this);
105 scroll_widget->setWidget(main_widget);
106 scroll_widget->setWidgetResizable(true);
107 setWidget(scroll_widget);
108 setWindowTitle("Job Manager");
109 setObjectName("jmJobManagerDock");
112 BL::JobsManager_QT::~JobsManager_QT()
114 DEBTRACE("Destroying BL::JobsManager_QT");
118 BL::JobsManager_QT::set_model_manager(BL::QModelManager * model_manager)
120 _model_manager = model_manager;
124 BL::JobsManager_QT::load_jobs_button()
126 DEBTRACE("load_jobs");
127 QString jobs_file = QFileDialog::getOpenFileName(this,
128 tr("Choose an xml jobs file"), "",
129 tr("xml (*.xml);;All Files (*)"));
132 write_normal_text("Load jobs action cancelled\n");
135 load_jobs(jobs_file.toUtf8().constData());
139 BL::JobsManager_QT::save_jobs_button()
141 DEBTRACE("save_jobs");
142 QFileDialog dialog(this, "Save jobs file");
144 filters << "XML files (*.xml)"
146 dialog.setFileMode(QFileDialog::AnyFile);
147 dialog.setNameFilters(filters);
148 dialog.selectNameFilter("(*.xml)");
149 dialog.setDefaultSuffix("xml");
150 dialog.setConfirmOverwrite(true);
151 dialog.setAcceptMode(QFileDialog::AcceptSave);
152 QString jobs_file("");
153 QStringList fileNames;
155 if (bool ret = dialog.exec())
157 DEBTRACE(ret << " " << dialog.confirmOverwrite());
158 fileNames = dialog.selectedFiles();
159 if (!fileNames.isEmpty())
160 jobs_file= fileNames.first();
164 write_normal_text("Save jobs action cancelled\n");
167 save_jobs(jobs_file.toUtf8().constData());
171 BL::JobsManager_QT::RefreshJobs()
177 BL::JobsManager_QT::no_auto_refresh()
179 _auto_refresh_jobs->setText("Auto Refresh: no");
184 BL::JobsManager_QT::ten_seconds_refresh()
186 _auto_refresh_jobs->setText("Auto Refresh: 10s");
188 _timer->start(10 * 1000);
192 BL::JobsManager_QT::thirty_seconds_refresh()
194 _auto_refresh_jobs->setText("Auto Refresh: 30s");
196 _timer->start(30 * 1000);
200 BL::JobsManager_QT::one_minute_refresh()
202 _auto_refresh_jobs->setText("Auto Refresh: 1min");
204 _timer->start(1 * 60 * 1000);
208 BL::JobsManager_QT::five_minutes_refresh()
210 _auto_refresh_jobs->setText("Auto Refresh: 5min");
212 _timer->start(5 * 60 * 1000);
216 BL::JobsManager_QT::thirty_minutes_refresh()
218 _auto_refresh_jobs->setText("Auto Refresh: 30min");
220 _timer->start(30 * 60 * 1000);
224 BL::JobsManager_QT::one_hour_refresh()
226 _auto_refresh_jobs->setText("Auto Refresh: 1hour");
228 _timer->start(1 * 60 * 60 * 1000);
232 BL::JobsManager_QT::restart_job(const std::string & name)
234 DEBTRACE("Restart job with name: " << name);
235 BL::CreateJobWizard wizard(this, _salome_services);
238 wizard.job_name = name;
239 wizard.start_job = true;
240 _main_gui->delete_job_internal();
241 create_job_with_wizard(wizard);
245 BL::JobsManager_QT::edit_clone_job(const std::string & name)
247 BL::CreateJobWizard wizard(this, _salome_services);
251 // Check if the job has the same name
252 if (name == wizard.job_name)
254 DEBTRACE("Job " << name << " has been edited");
255 _main_gui->delete_job_internal();
258 if (wizard.job_name != "")
260 create_job_with_wizard(wizard);
264 DEBTRACE("User cancel Create Job Wizard");
269 BL::JobsManager_QT::create_job()
271 BL::CreateJobWizard wizard(this, _salome_services);
273 if (wizard.job_name != "")
275 create_job_with_wizard(wizard);
279 DEBTRACE("User cancel Create Job Wizard");
284 BL::JobsManager_QT::create_job_with_wizard(BL::CreateJobWizard & wizard)
286 BL::Job * new_job = createJob(wizard.job_name);
287 switch (wizard.job_type)
289 case BL::CreateJobWizard::YACS:
291 new_job->setType(BL::Job::YACS_SCHEMA);
292 new_job->setJobFile(wizard.yacs_file);
293 new_job->setDumpYACSState(wizard.dump_yacs_state);
295 case BL::CreateJobWizard::COMMAND:
297 new_job->setType(BL::Job::COMMAND);
298 new_job->setJobFile(wizard.command);
300 case BL::CreateJobWizard::PYTHON_SALOME:
302 new_job->setType(BL::Job::PYTHON_SALOME);
303 new_job->setJobFile(wizard.python_salome_file);
306 throw BL::Exception("Unknown job type");
310 new_job->setEnvFile(wizard.env_file);
311 BL::Job::BatchParam param;
314 if (wizard.coorm_batch_directory != "")
316 param.batch_directory = wizard.coorm_batch_directory;
318 else if (wizard.batch_directory != "")
320 param.batch_directory = wizard.batch_directory;
323 param.maximum_duration = wizard.maximum_duration;
324 param.mem_limit = wizard.mem_limit;
325 param.mem_req_type = wizard.mem_req_type;
326 param.nb_proc = wizard.nb_proc;
327 param.exclusive = wizard.exclusive;
329 // Parameters for COORM
330 param.launcher_file = wizard.launcher_file;
331 param.launcher_args = wizard.launcher_args;
333 new_job->setBatchParameters(param);
334 BL::Job::FilesParam files_params;
335 files_params.result_directory = wizard.result_directory;
336 files_params.input_files_list = wizard.input_files_list;
337 files_params.output_files_list = wizard.output_files_list;
338 new_job->setFilesParameters(files_params);
339 new_job->setResource(wizard.resource_choosed);
340 new_job->setBatchQueue(wizard.batch_queue);
341 new_job->setLoadLevelerJobType(wizard.ll_jobtype);
342 new_job->setWCKey(wizard.wckey);
343 new_job->setExtraParams(wizard.extra_params);
346 addJobToLauncher(wizard.job_name);
347 emit new_job_added(QString::fromUtf8(wizard.job_name.c_str()));
348 QStandardItemModel * model = _model_manager->getModel();
349 QList<QStandardItem *> item_list = model->findItems(QString::fromUtf8(wizard.job_name.c_str()));
350 QStandardItem * job_state_item = model->item(item_list.at(0)->row(), 2);
351 _main_gui->_jobs_table->selectRow(item_list.at(0)->row());
352 if (wizard.start_job)
353 start_job(wizard.job_name);
357 BL::JobsManager_QT::delete_job(QString job_name)
359 BL::JobsManager::removeJob(job_name.toUtf8().constData());
360 _model_manager->delete_job(job_name);
361 _main_gui->_job_tab->reset(job_name);
365 BL::JobsManager_QT::sendEvent(const std::string & action,
366 const std::string & event_name,
367 const std::string & job_name,
368 const std::string & data)
370 DEBTRACE("sendEvent BL::JobsManager_QT");
372 // Sending a QEvent to go back to main thread
373 BL::JobManagerEvent * event = new JobManagerEvent(action, event_name, job_name, data);
374 QApplication::postEvent(this, event);
378 BL::JobsManager_QT::event(QEvent * e)
380 QDockWidget::event(e);
381 JobManagerEvent * event = dynamic_cast<JobManagerEvent*>(e);
382 if (!event) return false;
384 DEBTRACE("BL::JobsManager_QT Receiving event : "
385 << event->action << " "
386 << event->event_name << " "
387 << event->job_name << " "
390 QString job_name = QString::fromUtf8(event->job_name.c_str());
391 if (event->action == "create_job")
393 if (event->event_name == "Ok")
395 write_normal_text("Job " + job_name + " created\n");
399 write_error_text("Error in creating job: " + job_name + "\n");
400 write_error_text("*** ");
401 write_error_text((event->data).c_str());
402 write_error_text(" ***\n");
405 else if (event->action == "start_job")
407 if (event->event_name == "Ok")
409 write_normal_text("Job " + job_name + " queued\n");
413 write_error_text("Error in starting job: " + job_name + "\n");
414 write_error_text("*** ");
415 write_error_text((event->data).c_str());
416 write_error_text(" ***\n");
418 emit job_state_changed(job_name);
420 else if (event->action == "refresh_job")
422 if (event->event_name == "Ok")
424 QString state((event->data).c_str());
425 state = state.toLower();
426 write_normal_text("Job " + job_name + " new state is " + state + "\n");
427 emit job_state_changed(job_name);
431 write_error_text("Error in refreshing job: " + job_name + "\n");
432 write_error_text("*** ");
433 write_error_text((event->data).c_str());
434 write_error_text(" ***\n");
437 else if (event->action == "delete_job")
439 if (event->event_name == "Ok")
441 write_normal_text("Job " + job_name + " deleted\n");
445 write_error_text("Warning delete job: " + job_name + " maybe not complete, exception catch in SALOME Launcher service\n");
446 write_error_text("*** ");
447 write_error_text((event->data).c_str());
448 write_error_text(" ***\n");
451 else if (event->action == "get_results_job")
453 if (event->event_name == "Ok")
455 write_normal_text("Results of Job " + job_name + " are get\n");
459 write_error_text("Warning for results of job: " + job_name + " maybe not complete, exception catch in SALOME Launcher service\n");
460 write_error_text("*** ");
461 write_error_text((event->data).c_str());
462 write_error_text(" ***\n");
465 else if (event->action == "stop_job")
467 if (event->event_name == "Ok")
469 write_normal_text("Job " + job_name + " is stopped\n");
473 write_error_text("Error when trying to stop job: " + job_name + "\n");
474 write_error_text("*** ");
475 write_error_text((event->data).c_str());
476 write_error_text(" ***\n");
479 else if (event->action == "get_assigned_hostnames")
481 if (event->event_name == "Ok")
483 vector<string> hostnames;
485 Tokenize(event->data, hostnames, "+");
487 vector<string>::iterator it;
489 write_normal_text("Job " + job_name + " assigned hostnames are :\n");
491 for (it = hostnames.begin(); it < hostnames.end(); it++)
493 vector<string> hostname;
494 Tokenize(*it, hostname, ".");
495 QString assigned_hostname(hostname[0].c_str());
496 write_normal_text("+ " + assigned_hostname + "\n");
501 // Do nothing in case the batch manager does not support this
504 else if (event->action == "save_jobs")
506 if (event->event_name == "Error")
508 write_error_text("Error in saving jobs: \n");
509 write_error_text("*** ");
510 write_error_text((event->data).c_str());
511 write_error_text(" ***\n");
515 QString str((event->data).c_str());
516 write_normal_text("Jobs saved in file " + str + "\n");
519 else if (event->action == "load_jobs")
521 if (event->event_name == "Error")
523 write_error_text("Error in loading jobs: \n");
524 write_error_text("*** ");
525 write_error_text((event->data).c_str());
526 write_error_text(" ***\n");
530 QString str((event->data).c_str());
531 write_normal_text("Jobs loaded from file " + str + "\n");
534 else if (event->action == "add_job")
536 if (event->event_name == "Ok")
538 write_normal_text("New job added " + job_name + "\n");
539 emit new_job_added(job_name);
542 else if (event->action == "to_remove_job")
544 if (event->event_name == "Ok")
545 _main_gui->delete_job_external(job_name);
549 QString str((event->action).c_str());
550 write_error_text("Unknown type of event received:" + str + "\n");
556 BL::JobsManager_QT::write_normal_text(const QString & text)
558 _log->setReadOnly(false);
559 QTextCursor cursor = _log->textCursor();
560 QTextCharFormat text_format;
561 text_format.setForeground(Qt::darkBlue);
562 cursor.insertText(text, text_format);
563 _log->setTextCursor(cursor);
564 _log->setReadOnly(true);
568 BL::JobsManager_QT::write_error_text(const QString & text)
570 _log->setReadOnly(false);
571 QTextCursor cursor = _log->textCursor();
572 QTextCharFormat text_format;
573 text_format.setForeground(Qt::red);
574 cursor.insertText(text, text_format);
575 _log->setTextCursor(cursor);
576 _log->setReadOnly(true);
579 // To tokenize a string
580 void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
582 // Skip delimiters at beginning.
583 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
584 // Find first "non-delimiter".
585 string::size_type pos = str.find_first_of(delimiters, lastPos);
587 while (string::npos != pos || string::npos != lastPos)
589 // Found a token, add it to the vector.
590 tokens.push_back(str.substr(lastPos, pos - lastPos));
591 // Skip delimiters. Note the "not_of"
592 lastPos = str.find_first_not_of(delimiters, pos);
593 // Find next "non-delimiter"
594 pos = str.find_first_of(delimiters, lastPos);