--- /dev/null
+// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+/*
+ * Batch_Utils.cxx
+ *
+ * Created on: 30 jan. 2012
+ * Author : Renaud BARATE - EDF R&D
+ */
+
+#include <cstdio>
+
+#include <Batch_config.h>
+#include "Batch_Utils.hxx"
+
+#ifdef MSVC
+#define popen _popen
+#define pclose _pclose
+#endif
+
+using namespace std;
+namespace Batch {
+
+int Utils::getCommandOutput(const string & command, string & output)
+{
+ // Reinitialize output
+ output = "";
+
+ // Call command
+ FILE * fp = popen(command.c_str(), "r");
+ if (fp == NULL) {
+ return -1;
+ }
+
+ // Read the output and store it
+ char buf[1024];
+ while (fgets(buf, sizeof(buf), fp) != NULL) {
+ output += buf;
+ }
+
+ // close and get status
+ int status = pclose(fp);
+ return status;
+}
+
+}
--- /dev/null
+// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+/*
+ * Batch_Utils.hxx
+ *
+ * Created on: 30 jan. 2012
+ * Author : Renaud BARATE - EDF R&D
+ */
+
+#ifndef BATCH_UTILS_HXX_
+#define BATCH_UTILS_HXX_
+
+#include <string>
+
+namespace Batch {
+
+class Utils {
+public:
+
+ /**
+ * Call a command with the system shell and stores its output in parameter "output".
+ * Returns the return code of the command.
+ */
+ static int getCommandOutput(const std::string & command, std::string & output);
+
+private:
+
+ // No instanciation possible as this class provides only static methods
+ Utils() { }
+
+};
+
+}
+
+#endif /* BATCH_UTILS_HXX_ */
Core/Batch_StringType
Core/Batch_TypeMismatchException
Core/Batch_Versatile
+ Core/Batch_Utils
)
APPEND_CLASSES_TO_SRC_FILES(${CLASS_LIST})
*/
#include <cstdlib>
-#include <iostream>
#include <fstream>
#include <Batch_NotYetImplementedException.hxx>
#include <Batch_Constants.hxx>
+#include <Batch_Utils.hxx>
-#include "Batch_FactBatchManager_eSlurm.hxx"
#include "Batch_BatchManager_eSlurm.hxx"
#include "Batch_JobInfo_eSlurm.hxx"
// Method to submit a job to the batch manager
const JobId BatchManager_eSlurm::submitJob(const Job & job)
{
- int status;
Parametre params = job.getParametre();
const string workDir = params[WORKDIR];
// build command file to submit the job and copy it on the server
string cmdFile = buildCommandFile(job);
- // define name of log file (local)
- string logFile = generateTemporaryFileName("slurm-submitlog");
-
// define command to submit batch
string subCommand = string("cd ") + workDir + "; sbatch " + cmdFile;
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
- cerr << command.c_str() << endl;
- status = system(command.c_str());
- if (status)
- {
- ifstream error_message(logFile.c_str());
- string mess;
- string temp;
- while(getline(error_message, temp))
- mess += temp;
- error_message.close();
- throw EmulationException("Error of connection on remote host, error was: " + mess);
- }
-
- // read id of submitted job in log file
- string jobref;
- ifstream idfile(logFile.c_str());
- string line;
- while (idfile && line.compare(0, 20, "Submitted batch job ") != 0)
- getline(idfile, line);
- idfile.close();
- if (line.compare(0, 20, "Submitted batch job ") == 0)
- jobref = line.substr(20);
- if (jobref.size() == 0)
+ command += " 2>&1";
+ cout << command.c_str() << endl;
+
+ // submit job
+ string output;
+ int status = Utils::getCommandOutput(command, output);
+ cout << output;
+ if (status != 0) throw EmulationException("Can't submit job, error was: " + output);
+
+ // find id of submitted job in output
+ string search = "Submitted batch job ";
+ string::size_type pos = output.find(search);
+ if (pos == string::npos)
throw EmulationException("Error in the submission of the job on the remote host");
+ pos += search.size();
+ string::size_type endl_pos = output.find('\n', pos);
+ string::size_type count = (endl_pos == string::npos)? string::npos : endl_pos - pos;
+ string jobref = output.substr(pos, count);
JobId id(this, jobref);
return id;
JobInfo BatchManager_eSlurm::queryJob(const JobId & jobid)
{
- // define name of log file (local)
- string logFile = generateTemporaryFileName("slurm-querylog-" + jobid.getReference());
-
// define command to query batch
string subCommand = "squeue -o %t -j " + jobid.getReference();
string command = _protocol.getExecCommand(subCommand, _hostname, _username);
- command += " > ";
- command += logFile;
cerr << command.c_str() << endl;
- system(command.c_str());
+ string output;
+ Utils::getCommandOutput(command, output);
// We don't test the return code here because with jobs finished since a long time Slurm
// returns an error and a message like "slurm_load_jobs error: Invalid job id specified".
// So we consider that the job is finished when we get an error.
- JobInfo_eSlurm jobinfo = JobInfo_eSlurm(jobid.getReference(), logFile);
+ JobInfo_eSlurm jobinfo = JobInfo_eSlurm(jobid.getReference(), output);
return jobinfo;
}
* Author : Renaud BARATE - EDF R&D
*/
-#include <iostream>
-#include <fstream>
#include <sstream>
#include <Batch_RunTimeException.hxx>
namespace Batch {
- JobInfo_eSlurm::JobInfo_eSlurm(const std::string & id, const std::string & logFile)
+ JobInfo_eSlurm::JobInfo_eSlurm(const std::string & id, const std::string & queryOutput)
: JobInfo()
{
_param[ID] = id;
- // read log file
- ifstream log(logFile.c_str());
- string line;
-
- // status should be on the second line
- for (int i=0 ; i<2 ; i++)
- getline(log, line);
- log.close();
+ // read query output, status should be on the second line
+ istringstream iss(queryOutput);
string status;
- istringstream iss(line);
- iss >> status;
+ for (int i=0 ; i<2 ; i++)
+ getline(iss, status);
if (status.size() == 0) {
// On some batch managers, the job is deleted as soon as it is finished,
{
public:
- JobInfo_eSlurm(const std::string & id, const std::string & logFile);
+ JobInfo_eSlurm(const std::string & id, const std::string & queryOutput);
virtual ~JobInfo_eSlurm();
};