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);
294 new_job->setYacsDriverOptions(wizard.yacs_driver_options);
296 case BL::CreateJobWizard::COMMAND:
298 new_job->setType(BL::Job::COMMAND);
299 new_job->setJobFile(wizard.command);
301 case BL::CreateJobWizard::COMMAND_SALOME:
303 new_job->setType(BL::Job::COMMAND_SALOME);
304 new_job->setJobFile(wizard.command);
306 case BL::CreateJobWizard::PYTHON_SALOME:
308 new_job->setType(BL::Job::PYTHON_SALOME);
309 new_job->setJobFile(wizard.python_salome_file);
312 throw BL::Exception("Unknown job type");
316 new_job->setEnvFile(wizard.env_file);
317 new_job->setPreCommand(wizard.pre_command);
318 BL::Job::BatchParam param;
321 if (wizard.coorm_batch_directory != "")
323 param.batch_directory = wizard.coorm_batch_directory;
325 else if (wizard.batch_directory != "")
327 param.batch_directory = wizard.batch_directory;
330 param.maximum_duration = wizard.maximum_duration;
331 param.mem_limit = wizard.mem_limit;
332 param.mem_req_type = wizard.mem_req_type;
333 param.nb_proc = wizard.nb_proc;
334 param.nb_node = wizard.nb_node;
335 param.exclusive = wizard.exclusive;
337 // Parameters for COORM
338 param.launcher_file = wizard.launcher_file;
339 param.launcher_args = wizard.launcher_args;
341 new_job->setBatchParameters(param);
342 BL::Job::FilesParam files_params;
343 files_params.result_directory = wizard.result_directory;
344 files_params.input_files_list = wizard.input_files_list;
345 files_params.output_files_list = wizard.output_files_list;
346 new_job->setFilesParameters(files_params);
347 new_job->setResource(wizard.resource_choosed);
348 new_job->setBatchQueue(wizard.batch_queue);
349 new_job->setBatchPartition(wizard.batch_partition);
350 new_job->setLoadLevelerJobType(wizard.ll_jobtype);
351 new_job->setWCKey(wizard.wckey);
352 new_job->setExtraParams(wizard.extra_params);
355 addJobToLauncher(wizard.job_name);
356 emit new_job_added(QString::fromUtf8(wizard.job_name.c_str()));
357 QStandardItemModel * model = _model_manager->getModel();
358 QList<QStandardItem *> item_list = model->findItems(QString::fromUtf8(wizard.job_name.c_str()));
359 QStandardItem * job_state_item = model->item(item_list.at(0)->row(), 2);
360 _main_gui->_jobs_table->selectRow(item_list.at(0)->row());
361 if (wizard.start_job)
362 start_job(wizard.job_name);
366 BL::JobsManager_QT::delete_job(QString job_name)
368 BL::JobsManager::removeJob(job_name.toUtf8().constData());
369 _model_manager->delete_job(job_name);
370 _main_gui->_job_tab->reset(job_name);
374 BL::JobsManager_QT::sendEvent(const std::string & action,
375 const std::string & event_name,
376 const std::string & job_name,
377 const std::string & data)
379 DEBTRACE("sendEvent BL::JobsManager_QT");
381 // Sending a QEvent to go back to main thread
382 BL::JobManagerEvent * event = new JobManagerEvent(action, event_name, job_name, data);
383 QApplication::postEvent(this, event);
387 BL::JobsManager_QT::event(QEvent * e)
389 QDockWidget::event(e);
390 JobManagerEvent * event = dynamic_cast<JobManagerEvent*>(e);
391 if (!event) return false;
393 DEBTRACE("BL::JobsManager_QT Receiving event : "
394 << event->action << " "
395 << event->event_name << " "
396 << event->job_name << " "
399 QString job_name = QString::fromUtf8(event->job_name.c_str());
400 if (event->action == "create_job")
402 if (event->event_name == "Ok")
404 write_normal_text("Job " + job_name + " created\n");
408 write_error_text("Error in creating job: " + job_name + "\n");
409 write_error_text("*** ");
410 write_error_text((event->data).c_str());
411 write_error_text(" ***\n");
414 else if (event->action == "start_job")
416 if (event->event_name == "Ok")
418 write_normal_text("Job " + job_name + " queued\n");
422 write_error_text("Error in starting job: " + job_name + "\n");
423 write_error_text("*** ");
424 write_error_text((event->data).c_str());
425 write_error_text(" ***\n");
427 emit job_state_changed(job_name);
429 else if (event->action == "refresh_job")
431 if (event->event_name == "Ok")
433 QString state((event->data).c_str());
434 state = state.toLower();
435 write_normal_text("Job " + job_name + " new state is " + state + "\n");
436 emit job_state_changed(job_name);
440 write_error_text("Error in refreshing job: " + job_name + "\n");
441 write_error_text("*** ");
442 write_error_text((event->data).c_str());
443 write_error_text(" ***\n");
446 else if (event->action == "delete_job")
448 if (event->event_name == "Ok")
450 write_normal_text("Job " + job_name + " deleted\n");
454 write_error_text("Warning delete job: " + job_name + " maybe not complete, exception catch in SALOME Launcher service\n");
455 write_error_text("*** ");
456 write_error_text((event->data).c_str());
457 write_error_text(" ***\n");
460 else if (event->action == "get_results_job")
462 if (event->event_name == "Ok")
464 write_normal_text("Results of Job " + job_name + " have been got.\n");
468 write_error_text("Warning for results of job: " + job_name + " maybe not complete, exception catch in SALOME Launcher service\n");
469 write_error_text("*** ");
470 write_error_text((event->data).c_str());
471 write_error_text(" ***\n");
474 else if (event->action == "stop_job")
476 if (event->event_name == "Ok")
478 write_normal_text("Job " + job_name + " is stopped\n");
482 write_error_text("Error when trying to stop job: " + job_name + "\n");
483 write_error_text("*** ");
484 write_error_text((event->data).c_str());
485 write_error_text(" ***\n");
488 else if (event->action == "get_assigned_hostnames")
490 if (event->event_name == "Ok")
492 vector<string> hostnames;
494 Tokenize(event->data, hostnames, "+");
496 vector<string>::iterator it;
498 write_normal_text("Job " + job_name + " assigned hostnames are :\n");
500 for (it = hostnames.begin(); it < hostnames.end(); it++)
502 vector<string> hostname;
503 Tokenize(*it, hostname, ".");
504 QString assigned_hostname(hostname[0].c_str());
505 write_normal_text("+ " + assigned_hostname + "\n");
510 // Do nothing in case the batch manager does not support this
513 else if (event->action == "save_jobs")
515 if (event->event_name == "Error")
517 write_error_text("Error in saving jobs: \n");
518 write_error_text("*** ");
519 write_error_text((event->data).c_str());
520 write_error_text(" ***\n");
524 QString str((event->data).c_str());
525 write_normal_text("Jobs saved in file " + str + "\n");
528 else if (event->action == "load_jobs")
530 if (event->event_name == "Error")
532 write_error_text("Error in loading jobs: \n");
533 write_error_text("*** ");
534 write_error_text((event->data).c_str());
535 write_error_text(" ***\n");
539 QString str((event->data).c_str());
540 write_normal_text("Jobs loaded from file " + str + "\n");
543 else if (event->action == "add_job")
545 if (event->event_name == "Ok")
547 write_normal_text("New job added " + job_name + "\n");
548 emit new_job_added(job_name);
551 else if (event->action == "to_remove_job")
553 if (event->event_name == "Ok")
554 _main_gui->delete_job_external(job_name);
558 QString str((event->action).c_str());
559 write_error_text("Unknown type of event received:" + str + "\n");
565 BL::JobsManager_QT::write_normal_text(const QString & text)
567 _log->setReadOnly(false);
568 QTextCursor cursor = _log->textCursor();
569 QTextCharFormat text_format;
570 text_format.setForeground(Qt::darkBlue);
571 cursor.insertText(text, text_format);
572 _log->setTextCursor(cursor);
573 _log->setReadOnly(true);
577 BL::JobsManager_QT::write_error_text(const QString & text)
579 _log->setReadOnly(false);
580 QTextCursor cursor = _log->textCursor();
581 QTextCharFormat text_format;
582 text_format.setForeground(Qt::red);
583 cursor.insertText(text, text_format);
584 _log->setTextCursor(cursor);
585 _log->setReadOnly(true);
588 // To tokenize a string
589 void Tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
591 // Skip delimiters at beginning.
592 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
593 // Find first "non-delimiter".
594 string::size_type pos = str.find_first_of(delimiters, lastPos);
596 while (string::npos != pos || string::npos != lastPos)
598 // Found a token, add it to the vector.
599 tokens.push_back(str.substr(lastPos, pos - lastPos));
600 // Skip delimiters. Note the "not_of"
601 lastPos = str.find_first_not_of(delimiters, pos);
602 // Find next "non-delimiter"
603 pos = str.find_first_of(delimiters, lastPos);