Commit 50d0b6e8 authored by cdelbe's avatar cdelbe
Browse files

PICKED FROM TRUNK 14968

Fix SCHEDULING-461 :
WARNING : Post-Script is now executed each time leaving a boolean called "success" inside the script that
is true if the task execution has finished successfully, false otherwise.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/proactive/scheduling/branches/Scheduling_2.0.x@15002 28e8926c-6b08-0410-baaa-805c5e19b8d6
parent a1454af8
......@@ -42,6 +42,8 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Serializable;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;
import javax.persistence.Column;
import javax.persistence.Lob;
......@@ -217,11 +219,33 @@ public abstract class Script<E> implements Serializable {
return parameters;
}
/**
* Add a binding to the script that will be handle by this handler.
*
* @param name the name of the variable
* @param value the value of the variable
*/
public void addBinding(String name, Object value) {
}
/**
* Execute the script and return the ScriptResult corresponding.
*
* @return a ScriptResult object.
*/
public ScriptResult<E> execute() {
return execute(null);
}
/**
* Execute the script and return the ScriptResult corresponding.
* This method can add an additional user bindings if needed.
*
* @param aBindings the additional user bindings to add if needed. Can be null or empty.
* @return a ScriptResult object.
*/
public ScriptResult<E> execute(Map<String, Object> aBindings) {
ScriptEngine engine = getEngine();
if (engine == null) {
......@@ -230,6 +254,12 @@ public abstract class Script<E> implements Serializable {
try {
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
//add additional bindings
if (aBindings != null) {
for (Entry<String, Object> e : aBindings.entrySet()) {
bindings.put(e.getKey(), e.getValue());
}
}
prepareBindings(bindings);
engine.eval(getReader());
......
......@@ -35,6 +35,8 @@
package org.ow2.proactive.scripting;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.objectweb.proactive.annotation.PublicAPI;
......@@ -50,6 +52,7 @@ public class ScriptHandler implements Serializable {
/** */
private static final long serialVersionUID = 20;
Map<String, Object> additionalBindings = null;
/**
* ProActive Constructor
......@@ -64,12 +67,25 @@ public class ScriptHandler implements Serializable {
*/
public ScriptResult handle(Script script) {
try {
return script.execute();
return script.execute(additionalBindings);
} catch (Throwable t) {
return new ScriptResult(t);
}
}
/**
* Add a binding to the script that will be handle by this handler.
*
* @param name the name of the variable
* @param value the value of the variable
*/
public void addBinding(String name, Object value) {
if (additionalBindings == null) {
additionalBindings = new HashMap<String, Object>();
}
additionalBindings.put(name, value);
}
/**
* no implemented
*/
......
......@@ -101,16 +101,27 @@ public class JavaTaskLauncher extends TaskLauncher {
callInternalInit(JavaExecutable.class, JavaExecutableInitializer.class, executableContainer
.createExecutableInitializer());
//launch task
Serializable userResult = currentExecutable.execute(results);
Throwable exception = null;
Serializable userResult = null;
try {
//launch task
userResult = currentExecutable.execute(results);
} catch (Throwable t) {
exception = t;
}
//copy output file
copyScratchDataToOutput();
//launch post script
if (post != null) {
this.executePostScript(PAActiveObject.getNode());
this.executePostScript(PAActiveObject.getNode(), exception == null);
}
//copy output file
copyScratchDataToOutput();
//throw exception if needed
if (exception != null) {
throw exception;
}
//return result
return new TaskResultImpl(taskId, userResult, this.getLogs());
......
......@@ -121,22 +121,33 @@ public class NativeTaskLauncher extends TaskLauncher {
//replace dataspace tags in command (if needed) by local scratch directory
replaceCommandDSTags();
//launch task
logger_dev.debug("Starting execution of task '" + taskId + "'");
Serializable userResult = currentExecutable.execute(results);
//execute post-script only if user task return code is 0
int retCode = Integer.parseInt(userResult.toString());
if (post != null && retCode == 0) {
this.executePostScript(PAActiveObject.getNode());
Throwable exception = null;
Serializable userResult = null;
try {
//launch task
logger_dev.debug("Starting execution of task '" + taskId + "'");
userResult = currentExecutable.execute(results);
} catch (Throwable t) {
exception = t;
}
//logBuffer is filled up
TaskResult result = new TaskResultImpl(taskId, userResult, this.getLogs());
//copy output file
copyScratchDataToOutput();
//launch post script
if (post != null) {
int retCode = Integer.parseInt(userResult.toString());
this.executePostScript(PAActiveObject.getNode(), retCode == 0 && exception == null);
}
//throw exception if needed
if (exception != null) {
throw exception;
}
//logBuffer is filled up
TaskResult result = new TaskResultImpl(taskId, userResult, this.getLogs());
//return result
return result;
} catch (Throwable ex) {
......
......@@ -98,6 +98,7 @@ import org.ow2.proactive.scripting.ScriptResult;
public abstract class TaskLauncher implements InitActive {
public static final Logger logger_dev = ProActiveLogger.getLogger(SchedulerDevLoggers.LAUNCHER);
public static final String EXECUTION_SUCCEED_BINDING_NAME = "success";
protected DataSpacesFileObject SCRATCH = null;
protected DataSpacesFileObject INPUT = null;
......@@ -430,15 +431,18 @@ public abstract class TaskLauncher implements InitActive {
/**
* Execute the postScript on the node n, or on the default node if n is null.
*
* @param Node the node on which to execute the post script
* @param executionSucceed a boolean describing the state of the task execution.(true if execution succeed, false if not)
* @throws ActiveObjectCreationException if the script handler cannot be created
* @throws NodeException if the script handler cannot be created
* @throws UserException if an error occurred during the execution of the script
*/
@SuppressWarnings("unchecked")
protected void executePostScript(Node n) throws ActiveObjectCreationException, NodeException,
UserException {
protected void executePostScript(Node n, boolean executionSucceed) throws ActiveObjectCreationException,
NodeException, UserException {
logger_dev.info("Executing post-script");
ScriptHandler handler = ScriptLoader.createHandler(n);
handler.addBinding(EXECUTION_SUCCEED_BINDING_NAME, executionSucceed);
ScriptResult<String> res = handler.handle(post);
if (res.errorOccured()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment