Search in sources :

Example 76 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class InProcessTaskExecutor method execute.

/**
 * Executes a task.
 *
 * @param taskContext           Task context to execute
 * @param output                Standard output sink.
 * @param error                 Error sink.
 * @param scriptHandler
 * @param thirdPartyCredentials
 * @param variables             Environment variables.
 * @return The result of the executed script.
 * @throws Throwable
 */
private Serializable execute(TaskContext taskContext, PrintStream output, PrintStream error, ScriptHandler scriptHandler, Map<String, String> thirdPartyCredentials, VariablesMap variables) throws Throwable {
    if (taskContext.getPreScript() != null) {
        Script<?> script = taskContext.getPreScript();
        forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
        ScriptResult preScriptResult = scriptHandler.handle(script, output, error);
        if (preScriptResult.errorOccured()) {
            throw new TaskException("Failed to execute pre script: " + preScriptResult.getException().getMessage(), preScriptResult.getException());
        }
    }
    Script<Serializable> script = ((ScriptExecutableContainer) taskContext.getExecutableContainer()).getScript();
    forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
    ScriptResult<Serializable> scriptResult = scriptHandler.handle(script, output, error);
    if (scriptResult.errorOccured()) {
        throw new TaskException("Failed to execute task: " + scriptResult.getException().getMessage(), scriptResult.getException());
    }
    if (taskContext.getPostScript() != null) {
        forkedTaskVariablesManager.replaceScriptParameters(taskContext.getPostScript(), thirdPartyCredentials, variables, error);
        ScriptResult postScriptResult = scriptHandler.handle(taskContext.getPostScript(), output, error);
        if (postScriptResult.errorOccured()) {
            throw new TaskException("Failed to execute post script: " + postScriptResult.getException().getMessage(), postScriptResult.getException());
        }
    }
    return scriptResult.getResult();
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) Serializable(java.io.Serializable) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)

Example 77 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class ForkedJvmTaskExecutionCommandCreator method createForkedJvmTaskExecutionCommand.

/**
 * Creates a command to start a task inside a java virtual machine.
 *
 * @param taskContext                   TaskContext object describing the task.
 * @param forkEnvironmentScriptResult   Result from a running fork environment script. If it is
 *                                      of instance {@link ForkEnvironmentScriptResult}, the script return
 *                                      variables will be used for the construction of the command.
 * @param serializedContextAbsolutePath The serialized TaskContext object which will be read by the virtual
 *                                      machine to run the task.
 * @return A List, empty if the TaskContext is null, otherwise filled with a command.
 * @throws Exception If the {@link TaskContextVariableExtractor} could not extract all variables from the
 *                   TaskContext.
 */
public List<String> createForkedJvmTaskExecutionCommand(TaskContext taskContext, ScriptResult forkEnvironmentScriptResult, String serializedContextAbsolutePath) throws Exception {
    if (taskContext == null) {
        return new ArrayList<>(0);
    }
    Map<String, Serializable> variables = taskContextVariableExtractor.getAllVariables(taskContext);
    String javaHome = System.getProperty("java.home");
    ArrayList<String> jvmArguments = new ArrayList<>(1);
    ForkEnvironment forkEnvironment = null;
    if (taskContext.getInitializer() != null) {
        forkEnvironment = taskContext.getInitializer().getForkEnvironment();
    }
    // set the task fork property so that script engines have a mean to know
    // if they are running in a forked task or not
    jvmArguments.add(PASchedulerProperties.TASK_FORK.getCmdLine() + "true");
    configureLogging(jvmArguments, variables);
    StringBuilder classpath = new StringBuilder("." + File.pathSeparatorChar);
    if (!System.getProperty("java.class.path", "").contains("node.jar")) {
        // in case the class path of the node is not built with the node.jar, we
        // build the classpath with wildcards to avoid command too long errors on windows
        classpath.append(getStandardClassPathEntries(variables));
    }
    for (String classpathEntry : OneJar.getClasspath()) {
        classpath.append(File.pathSeparatorChar).append(classpathEntry);
    }
    if (forkEnvironment != null) {
        for (String jvmArgument : forkEnvironment.getJVMArguments()) {
            jvmArguments.add(VariableSubstitutor.filterAndUpdate(jvmArgument, variables));
        }
        for (String classpathEntry : forkEnvironment.getAdditionalClasspath()) {
            classpath.append(File.pathSeparatorChar).append(VariableSubstitutor.filterAndUpdate(classpathEntry, variables));
        }
        if (!Strings.isNullOrEmpty(forkEnvironment.getJavaHome())) {
            javaHome = VariableSubstitutor.filterAndUpdate(forkEnvironment.getJavaHome(), variables);
        }
    }
    List<String> prefixes = javaPrefixCommandExtractor.extractJavaPrefixCommandToCommandListFromScriptResult(forkEnvironmentScriptResult);
    List<String> javaCommand = new ArrayList<>(prefixes.size() + 3 + jvmArguments.size() + 2);
    javaCommand.addAll(prefixes);
    javaCommand.add(javaHome + javaHomePostfixJavaExecutable);
    javaCommand.add("-cp");
    javaCommand.add(classpath.toString());
    javaCommand.addAll(jvmArguments);
    javaCommand.add(ExecuteForkedTaskInsideNewJvm.class.getName());
    javaCommand.add(serializedContextAbsolutePath);
    return javaCommand;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Example 78 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class JavaPrefixCommandExtractor method extractJavaPrefixCommandToCommandListFromScriptResult.

/**
 * Extracts a java fork prefix command from a script result.
 *
 * @param scriptResult ScriptResult object from which the fork environment command is extracted.
 * @return Java prefix command, extracted out of the fork environment script variables.
 */
public List<String> extractJavaPrefixCommandToCommandListFromScriptResult(ScriptResult scriptResult) {
    List<String> javaPrefixCommand = new ArrayList<>();
    if (scriptResult != null && scriptResult.getResult() instanceof ForkEnvironmentScriptResult) {
        ForkEnvironmentScriptResult forkEnvResult = (ForkEnvironmentScriptResult) scriptResult.getResult();
        javaPrefixCommand.addAll(forkEnvResult.getJavaPrefixCommand());
    }
    return javaPrefixCommand;
}
Also used : ArrayList(java.util.ArrayList) ForkEnvironmentScriptResult(org.ow2.proactive.scripting.ForkEnvironmentScriptResult)

Example 79 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class ForkEnvironmentScriptExecutor method executeForkEnvironmentScript.

/**
 * Executes a fork environment script.
 *
 * @param context    The task context to execute the script in.
 * @param outputSink Standard output sink.
 * @param errorSink  Error output sink.
 * @return Returns a ScriptResult.
 * @throws Exception
 */
public ScriptResult executeForkEnvironmentScript(TaskContext context, PrintStream outputSink, PrintStream errorSink) throws Exception {
    VariablesMap variables = new VariablesMap();
    variables.setInheritedMap(taskContextVariableExtractor.getAllNonTaskVariables(context));
    variables.setScopeMap(taskContextVariableExtractor.getScopeVariables(context));
    Map<String, String> thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(context);
    ScriptHandler scriptHandler = ScriptLoader.createLocalHandler();
    Script<?> script = context.getInitializer().getForkEnvironment().getEnvScript();
    SchedulerNodeClient schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(context);
    RemoteSpace userSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.USER);
    RemoteSpace globalSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL);
    forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, context, variables, thirdPartyCredentials, schedulerNodeClient, userSpaceClient, globalSpaceClient, Collections.<String, String>emptyMap());
    forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, errorSink);
    ScriptResult scriptResult = scriptHandler.handle(script, outputSink, errorSink);
    if (scriptResult.errorOccured()) {
        throw new Exception("Failed to execute fork environment script", scriptResult.getException());
    }
    return scriptResult;
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) RemoteSpace(org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace) SchedulerNodeClient(org.ow2.proactive.scheduler.task.client.SchedulerNodeClient) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Example 80 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class FileStorageSupport method getScriptStorage.

public FileStorage<Script> getScriptStorage(String userName) {
    File scriptsDir = new File(getWorkflowsDir(userName), "scripts");
    scriptsDir.mkdirs();
    return new FileStorage<>(scriptsDir, new ScriptSerializer(), new NameAsIdGenerator());
}
Also used : ScriptSerializer(org.ow2.proactive_grid_cloud_portal.studio.storage.serializers.ScriptSerializer) NameAsIdGenerator(org.ow2.proactive_grid_cloud_portal.studio.storage.generators.NameAsIdGenerator) File(java.io.File)

Aggregations

Test (org.junit.Test)38 SelectionScript (org.ow2.proactive.scripting.SelectionScript)32 File (java.io.File)31 SimpleScript (org.ow2.proactive.scripting.SimpleScript)28 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)25 ArrayList (java.util.ArrayList)16 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)15 Script (org.ow2.proactive.scripting.Script)15 ScriptResult (org.ow2.proactive.scripting.ScriptResult)15 FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)13 HashMap (java.util.HashMap)12 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)11 IOException (java.io.IOException)10 JobId (org.ow2.proactive.scheduler.common.job.JobId)10 NodeSet (org.ow2.proactive.utils.NodeSet)10 RMFunctionalTest (functionaltests.utils.RMFunctionalTest)9 ResourceManager (org.ow2.proactive.resourcemanager.frontend.ResourceManager)9 Job (org.ow2.proactive.scheduler.common.job.Job)8 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)8 Serializable (java.io.Serializable)7