use of org.ow2.proactive.scripting.ScriptException in project scheduling by ow2-proactive.
the class ScriptExecutor method executeScripts.
/**
* Runs selection scripts and process the results
* returns node if it matches, null otherwise
*/
private Node executeScripts() {
boolean selectionScriptSpecified = selectionScriptList != null && selectionScriptList.size() > 0;
boolean nodeMatch = true;
ScriptException exception = null;
if (selectionScriptSpecified) {
// initializing parallel script execution
for (SelectionScript script : selectionScriptList) {
if (manager.isPassed(script, criteria.getBindings(), rmnode)) {
// already executed static script
logger.debug(rmnode.getNodeURL() + " : " + script.hashCode() + " skipping script execution");
continue;
}
logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " executing");
try {
ScriptResult<Boolean> scriptResult = rmnode.executeScript(script, criteria.getBindings());
// processing the results
if (!MOP.isReifiedObject(scriptResult) && scriptResult.getException() != null) {
// could not create script execution handler
// probably the node id down
logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception", scriptResult.getException());
logger.warn(rmnode.getNodeURL() + " : pinging the node");
rmnode.getNodeSource().pingNode(rmnode.getNode());
nodeMatch = false;
break;
} else {
try {
PAFuture.waitFor(scriptResult, PAResourceManagerProperties.RM_SELECT_SCRIPT_TIMEOUT.getValueAsLong());
} catch (ProActiveTimeoutException e) {
logger.warn("Timeout on " + rmnode.getNodeURL());
// do not produce an exception here
nodeMatch = false;
break;
}
// display the script result and output in the scheduler logs
if (scriptResult != null && logger.isInfoEnabled()) {
logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " result " + scriptResult.getResult());
if (scriptResult.getOutput() != null && scriptResult.getOutput().length() > 0) {
logger.info(rmnode.getNodeURL() + " : " + script.hashCode() + " output\n" + scriptResult.getOutput());
}
}
if (scriptResult != null && scriptResult.errorOccured()) {
nodeMatch = false;
exception = new ScriptException(scriptResult.getException());
logger.warn(rmnode.getNodeURL() + " : exception during the script execution", scriptResult.getException());
}
// selection manager at the same time. Returns whether node is selected.
if (!manager.processScriptResult(script, criteria.getBindings(), scriptResult, rmnode)) {
nodeMatch = false;
break;
}
}
} catch (Exception ex) {
// proactive or network exception occurred when script was executed
logger.warn(rmnode.getNodeURL() + " : " + script.hashCode() + " exception", ex);
nodeMatch = false;
exception = new ScriptException(ex);
break;
}
}
}
manager.scriptExecutionFinished(rmnode.getNodeURL());
if (selectionScriptSpecified && logger.isDebugEnabled()) {
if (nodeMatch) {
logger.debug(rmnode.getNodeURL() + " : selected");
} else {
logger.debug(rmnode.getNodeURL() + " : not selected");
}
}
// cleaning the node
try {
rmnode.clean();
} catch (Throwable t) {
logger.warn(rmnode.getNodeURL() + " : exception in cleaning", t);
logger.warn(rmnode.getNodeURL() + " : pinging the node");
try {
// 'pingNode' call can fail with exception if NodeSource was destroyed
rmnode.getNodeSource().pingNode(rmnode.getNode());
} catch (Throwable pingError) {
logger.warn(rmnode.getNodeURL() + " : nodeSource " + rmnode.getNodeSourceName() + " seems to be removed ", pingError);
}
return null;
}
if (exception != null) {
throw exception;
}
if (nodeMatch) {
return rmnode.getNode();
} else {
return null;
}
}
use of org.ow2.proactive.scripting.ScriptException in project scheduling by ow2-proactive.
the class SelectionManager method executeScript.
public <T> List<ScriptResult<T>> executeScript(final Script<T> script, final Collection<RMNode> nodes, final Map<String, Serializable> bindings) {
// TODO: add a specific timeout for script execution
final long timeout = PAResourceManagerProperties.RM_EXECUTE_SCRIPT_TIMEOUT.getValueAsLong();
final ArrayList<Callable<ScriptResult<T>>> scriptExecutors = new ArrayList<>(nodes.size());
// Execute the script on each selected node
for (final RMNode node : nodes) {
scriptExecutors.add(new Callable<ScriptResult<T>>() {
@Override
public ScriptResult<T> call() throws Exception {
// treated as ExecutionException
try {
ScriptResult<T> res = node.executeScript(script, bindings);
PAFuture.waitFor(res, timeout);
return res;
// return PAFuture.getFutureValue(res, timeout);
} finally {
// cleaning the node
try {
node.clean();
} catch (Throwable ex) {
logger.error("Cannot clean the node " + node.getNodeURL(), ex);
}
SelectionManager.this.rmcore.unlockNodes(Collections.singleton(node.getNodeURL()));
}
}
@Override
public String toString() {
return "executing script on " + node.getNodeURL();
}
});
}
// Invoke all Callables and get the list of futures
List<Future<ScriptResult<T>>> futures = null;
try {
futures = this.scriptExecutorThreadPool.invokeAll(scriptExecutors, timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
logger.warn("Interrupted while waiting, unable to execute all scripts", e);
Thread.currentThread().interrupt();
}
final List<ScriptResult<T>> results = new LinkedList<>();
int index = 0;
// waiting for the results
for (final Future<ScriptResult<T>> future : futures) {
final String description = scriptExecutors.get(index++).toString();
ScriptResult<T> result = null;
try {
result = future.get();
} catch (CancellationException e) {
result = new ScriptResult<>(new ScriptException("Cancelled due to timeout expiration when " + description, e));
} catch (InterruptedException e) {
result = new ScriptResult<>(new ScriptException("Cancelled due to interruption when " + description));
} catch (ExecutionException e) {
// Unwrap the root exception
Throwable rex = e.getCause();
result = new ScriptResult<>(new ScriptException("Exception occured in script call when " + description, rex));
}
results.add(result);
}
return results;
}
use of org.ow2.proactive.scripting.ScriptException in project scheduling by ow2-proactive.
the class AbstractIModeCommand method execute.
@Override
public void execute(ApplicationContext currentContext) throws CLIException {
currentContext.setProperty(IMODE, true);
ScriptEngine engine = currentContext.getEngine();
try {
// load supported functions
engine.eval(new InputStreamReader(script()));
} catch (ScriptException error) {
throw new CLIException(CLIException.REASON_OTHER, error);
}
while (!currentContext.getProperty(TERMINATE, Boolean.TYPE, false)) {
try {
String command = readLine(currentContext, "> ");
if (command == null) {
// EOF, exit interactive shell
break;
}
engine.eval(command);
} catch (ScriptException se) {
handleError(String.format("An error occurred while executing the script:"), se, currentContext);
}
}
}
use of org.ow2.proactive.scripting.ScriptException in project scheduling by ow2-proactive.
the class JavaClassScriptEngine method eval.
@Override
public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException {
try {
JavaExecutable javaExecutable = getExecutable(userExecutableClassName);
JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer();
PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true);
execInitializer.setOutputSink(output);
PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true);
execInitializer.setErrorSink(error);
Map<String, byte[]> propagatedVariables = null;
if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) {
propagatedVariables = SerializationUtil.serializeVariableMap(((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).getPropagatedVariables());
execInitializer.setPropagatedVariables(propagatedVariables);
} else {
execInitializer.setPropagatedVariables(Collections.<String, byte[]>emptyMap());
}
if (context.getAttribute(Script.ARGUMENTS_NAME) != null) {
execInitializer.setSerializedArguments((Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]);
} else {
execInitializer.setSerializedArguments(Collections.<String, byte[]>emptyMap());
}
if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) {
execInitializer.setThirdPartyCredentials((Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE));
} else {
execInitializer.setThirdPartyCredentials(Collections.<String, String>emptyMap());
}
if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) {
List<String> nodesURLs = (List<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME);
execInitializer.setNodesURL(nodesURLs);
} else {
execInitializer.setNodesURL(Collections.<String>emptyList());
}
javaExecutable.internalInit(execInitializer, context);
Serializable execute = javaExecutable.execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE));
if (propagatedVariables != null) {
((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).putAll(javaExecutable.getVariables());
}
output.close();
error.close();
return execute;
} catch (Throwable e) {
throw new ScriptException(new TaskException(getStackTraceAsString(e), e));
}
}
use of org.ow2.proactive.scripting.ScriptException in project scheduling by ow2-proactive.
the class Script method execute.
/**
* 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.
* @param outputSink where the script output is printed to.
* @param errorSink where the script error stream is printed to.
* @return a ScriptResult object.
*/
public ScriptResult<E> execute(Map<String, Object> aBindings, PrintStream outputSink, PrintStream errorSink) {
try {
fetchUrlIfNeeded();
} catch (Throwable t) {
String stack = Throwables.getStackTraceAsString(t);
if (t.getMessage() != null) {
stack = t.getMessage() + System.lineSeparator() + stack;
}
return new ScriptResult<>(new Exception(stack));
}
ScriptEngine engine = createScriptEngine();
if (engine == null)
return new ScriptResult<>(new Exception("No Script Engine Found for name or extension " + scriptEngineLookupName));
// SCHEDULING-1532: redirect script output to a buffer (keep the latest DEFAULT_OUTPUT_MAX_SIZE)
BoundedStringWriter outputBoundedWriter = new BoundedStringWriter(outputSink, DEFAULT_OUTPUT_MAX_SIZE);
BoundedStringWriter errorBoundedWriter = new BoundedStringWriter(errorSink, DEFAULT_OUTPUT_MAX_SIZE);
engine.getContext().setWriter(new PrintWriter(outputBoundedWriter));
engine.getContext().setErrorWriter(new PrintWriter(errorBoundedWriter));
Reader closedInput = new Reader() {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
throw new IOException("closed");
}
@Override
public void close() throws IOException {
}
};
engine.getContext().setReader(closedInput);
engine.getContext().setAttribute(ScriptEngine.FILENAME, scriptName, ScriptContext.ENGINE_SCOPE);
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);
Object evalResult = engine.eval(getReader());
engine.getContext().getErrorWriter().flush();
engine.getContext().getWriter().flush();
// Add output to the script result
ScriptResult<E> result = this.getResult(evalResult, bindings);
result.setOutput(outputBoundedWriter.toString());
return result;
} catch (javax.script.ScriptException e) {
// drop exception cause as it might not be serializable
ScriptException scriptException = new ScriptException(e.getMessage());
scriptException.setStackTrace(e.getStackTrace());
return new ScriptResult<>(scriptException);
} catch (Throwable t) {
String stack = Throwables.getStackTraceAsString(t);
if (t.getMessage() != null) {
stack = t.getMessage() + System.lineSeparator() + stack;
}
return new ScriptResult<>(new Exception(stack));
}
}
Aggregations