From 9167f3bd8fac1258949377bd347d42feb6c968a9 Mon Sep 17 00:00:00 2001
From: Stan Hammon <stan.hammon@kitware.com>
Date: Tue, 8 Mar 2022 09:38:24 -0700
Subject: [PATCH] Added logic to prevent job submission while another job is
 still running

---
 plugin/pqACE3PExportBehavior.cxx | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/plugin/pqACE3PExportBehavior.cxx b/plugin/pqACE3PExportBehavior.cxx
index b21eee64..a98c32c8 100644
--- a/plugin/pqACE3PExportBehavior.cxx
+++ b/plugin/pqACE3PExportBehavior.cxx
@@ -13,6 +13,7 @@
 #include "plugin/pqACE3PJobsBehavior.h"
 #include "plugin/pqACE3PSaveBehavior.h"
 #include "smtk/newt/qtNewtInterface.h"
+#include "smtk/simulation/ace3p/JobsManifest.h"
 #include "smtk/simulation/ace3p/operations/Export.h"
 #include "smtk/simulation/ace3p/qt/qtNewtJobSubmitter.h"
 #include "smtk/simulation/ace3p/qt/qtProgressDialog.h"
@@ -95,6 +96,33 @@ void pqACE3PExportReaction::onTriggered()
 //-----------------------------------------------------------------------------
 void pqACE3PExportBehavior::exportProject()
 {
+  auto project = smtk::simulation::ace3p::qtProjectRuntime::instance()->ace3pProject();
+
+  // ensure that there are no other jobs currently running
+  std::shared_ptr<smtk::simulation::ace3p::JobsManifest> manifest = project->jobsManifest();
+  for (int i = 0; i < manifest->size(); i++)
+  {
+    std::string status;
+    manifest->getField(i, "status", status);
+    if (status == "created" || status == "queued" || status == "running")
+    {
+      QMessageBox confirmDialog(pqCoreUtilities::mainWidget());
+      confirmDialog.setWindowTitle("Sumbit Job?");
+      confirmDialog.setText("There is already a running job. If you submit this job, you need to "
+                            "make sure that this new job will not save to the same results "
+                            "directory as any other job that is currently running.\n\n"
+                            "Are you sure you want to submit this analysis job?");
+      /*auto noButton = */ confirmDialog.addButton("Cancel", QMessageBox::RejectRole);
+      auto yesButton = confirmDialog.addButton("Submit", QMessageBox::AcceptRole);
+      confirmDialog.setDefaultButton(yesButton);
+      auto reply = confirmDialog.exec();
+      if (reply != QDialog::Accepted)
+      {
+        return;
+      }
+    }
+  }
+
   // Get the operation manager from the builtin server
   pqServer* server = pqACE3PAutoStart::builtinServer();
   pqSMTKWrapper* wrapper = pqSMTKBehavior::instance()->resourceManagerForServer(server);
@@ -104,7 +132,6 @@ void pqACE3PExportBehavior::exportProject()
   auto exportOp = opManager->create<smtk::simulation::ace3p::Export>();
   InternalCheckMacro(exportOp != nullptr, "Internal Error: Export operation not created.");
 
-  auto project = smtk::simulation::ace3p::qtProjectRuntime::instance()->ace3pProject();
   if (!this->cleanProject(project))
   {
     qWarning() << "Project is in modified state. Cannot export.";
@@ -419,7 +446,7 @@ bool pqACE3PExportBehavior::cleanProject(smtk::project::ProjectPtr project)
   QMessageBox confirmDialog(pqCoreUtilities::mainWidget());
   confirmDialog.setWindowTitle("Save Project?");
   confirmDialog.setText("You must save current changes before exporting.");
-  auto noButton = confirmDialog.addButton("Cancel", QMessageBox::RejectRole);
+  /*auto noButton = */ confirmDialog.addButton("Cancel", QMessageBox::RejectRole);
   auto yesButton = confirmDialog.addButton("Save Project", QMessageBox::AcceptRole);
   confirmDialog.setDefaultButton(yesButton);
   auto reply = confirmDialog.exec();
-- 
GitLab