R log as output in case of error

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineSmartExecutor@122703 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Gianpaolo Coro 2016-02-01 22:07:16 +00:00
parent a3c03e56dd
commit 0eec164e5c
1 changed files with 147 additions and 109 deletions

View File

@ -1,6 +1,8 @@
package org.gcube.dataanalysis.executor.rscripts.generic;
import java.io.File;
import java.io.FileWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -40,8 +42,10 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
@Override
protected void process() throws Exception {
boolean deletefiles = false; // for test only
String Rlog = null;
// init status
status = 0;
try {
// instantiate the R Script executor
scriptmanager = new LocalRScriptsManager();
// download and unzip the script in a controlled folder
@ -68,19 +72,20 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
for (String input : inputvariables) {
String value = config.getParam(input);
String defaultValue = inputs.get(i).getDefaultValue();
//inputParameters.put(defaultValue, value);
defaultValue = defaultValue.replace("(", "\\(").replace(")", "\\)").replace("[", "\\[").replace("]", "\\]").replace("|", "\\|").replace(".", "\\.").replace("?", "\\?").replace("*", "\\*").replace("+", "\\+").replace("{", "\\{").replace("}", "\\}");
// inputParameters.put(defaultValue, value);
String punct = "[ \";]*";
String regexp = punct+input+punct+"(<-|=)"+punct+defaultValue+punct+".*";
String regexp = punct + input + punct + "(<-|=)" + punct + defaultValue + punct + ".*";
boolean string = true;
try{
try {
Double.parseDouble(value);
string = false;
}catch(Exception ee){
} catch (Exception ee) {
}
String line = input+"="+value;
if (string)
line = input+"=\""+value+"\"";
String line = input + "=" + value;
if (string && !value.equals("T") && !value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false") && !value.equalsIgnoreCase("F"))
line = input + "=\"" + value + "\"";
inputParameters.put(regexp, line);
i++;
@ -120,7 +125,7 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
AnalysisLogger.getLogger().debug("Executing the script...");
// execute the script in multi-user mode
scriptmanager.executeRScript(config, scriptName, null, environmentalParameters, outputParameters, null, null, inputParameters, scriptMustReturnAFile, uploadScriptOnTheInfrastructureWorkspace, deletefiles, scriptSubfolder);
Rlog = scriptmanager.executeRScript(config, scriptName, null, environmentalParameters, outputParameters, null, null, inputParameters, scriptMustReturnAFile, uploadScriptOnTheInfrastructureWorkspace, deletefiles, scriptSubfolder);
AnalysisLogger.getLogger().debug("..execution finished!");
// get the output: one file should have been produced for each output
AnalysisLogger.getLogger().debug("Getting output");
@ -148,9 +153,43 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
AnalysisLogger.getLogger().debug(e);
AnalysisLogger.getLogger().debug("Could not delete sandbox folder " + folder.getAbsolutePath());
}
} catch (Exception e) {
if (Rlog != null) {
String httpurl = generateRemoteLogFile(Rlog);
String message = "Logs of the script can be found at "+httpurl;
e = new Exception(message);
}
throw e;
} finally {
AnalysisLogger.getLogger().debug("Computation finished.");
status = 100;
}
}
protected String generateRemoteLogFile(String Rlog) throws Exception {
String uuid = "" + UUID.randomUUID();
AnalysisLogger.getLogger().debug("Writing the logs of the execution");
File logfile = new File(config.getPersistencePath(), "RLOG" + uuid + ".txt");
FileWriter fw = new FileWriter(logfile);
fw.write(Rlog);
fw.close();
AnalysisLogger.getLogger().debug("Written in " + logfile);
String url = StorageUtils.uploadFilesOnStorage(config.getGcubeScope(), config.getParam("Username"), logfile.getParent(), "/ScriptLogs/" + uuid + "/", logfile.getName());
AnalysisLogger.getLogger().debug("Uploaded on storage: " + url);
String httpurl = "";
// String httpurl = url.replace("smp:", "http:");
if (config.getGcubeScope().startsWith("/gcube"))
httpurl = "http://data-d.d4science.org/uri-resolver/smp?smp-uri=" + url + "&fileName=" + logfile.getName() ;
else
httpurl = "http://data.d4science.org/uri-resolver/smp?smp-uri=" + url + "&fileName=" + logfile.getName() ;
AnalysisLogger.getLogger().debug("Deleting log file and returning " + httpurl);
logfile.delete();
// httpurl = URLEncoder.encode(httpurl, "UTF-8");
return httpurl;
}
protected String analyseContent(String filecontent, String scriptfolder) throws Exception {
AnalysisLogger.getLogger().debug("Analysing file content");
@ -161,10 +200,10 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
if (row != null) {
row = row.replace("\"", "");
File rowFile = new File(row.trim());
AnalysisLogger.getLogger().debug("Checking row file-> "+rowFile.getAbsolutePath());
if (!rowFile.exists()){
AnalysisLogger.getLogger().debug("Checking row file-> " + rowFile.getAbsolutePath());
if (!rowFile.exists()) {
rowFile = new File(scriptfolder, row.trim());
AnalysisLogger.getLogger().debug("File does not exist - checking complete row file-> "+rowFile.getAbsolutePath());
AnalysisLogger.getLogger().debug("File does not exist - checking complete row file-> " + rowFile.getAbsolutePath());
}
if (rowFile.exists()) {
@ -174,8 +213,7 @@ public abstract class GenericRScript extends StandardLocalExternalAlgorithm {
files.add(preparedFile);
break;
}
else
} else
AnalysisLogger.getLogger().debug("Checking row file does not exist - treating as a String");
}
}