Search in sources :

Example 11 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testAddBindingsToScriptHandlerContainsUserURI.

@Test
public void testAddBindingsToScriptHandlerContainsUserURI() throws InvalidScriptException, NodeException, NoSuchFieldException, IllegalAccessException {
    ScriptExecutableContainer scriptContainer = createScriptContainer();
    TaskLauncherInitializer taskLauncherInitializer = new TaskLauncherInitializer();
    taskLauncherInitializer.setForkEnvironment(new ForkEnvironment());
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, null, new NodeDataSpacesURIs(null, null, null, null, testSetString, null), null, null);
    // Expect taskResultArray to be inside the map
    validateThatScriptHandlerBindingsContain(new ScriptHandler(), taskContext, new VariablesMap(), new HashMap<String, String>(), new HashMap<String, String>(), SchedulerConstants.DS_USER_BINDING_NAME, testSetString);
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler) Test(org.junit.Test)

Example 12 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testScriptCredentialsAreReplaced.

@Test
public void testScriptCredentialsAreReplaced() throws InvalidScriptException {
    ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
    // Add $credential_[something] variable to new python script
    Serializable[] parameters = new Serializable[] { "$" + ForkedTaskVariablesManager.CREDENTIALS_KEY_PREFIX + testVariable1Key };
    Script script = new SimpleScript("print 'hello'", "python", parameters);
    // Create credentials
    Map<String, String> credentials = new HashMap<>();
    credentials.put(testVariable1Key, testVariable1Value);
    // Replace the credentials inside the script parameters
    forkedTaskVariablesManager.replaceScriptParameters(script, credentials, new VariablesMap(), System.out);
    assertThat((String) parameters[0], is(testVariable1Value));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) Script(org.ow2.proactive.scripting.Script) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Serializable(java.io.Serializable) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) Test(org.junit.Test)

Example 13 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class JavaClassScriptEngineFactoryTest method replication_index.

@Test
public void replication_index() throws Exception {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("java");
    StringWriter output = new StringWriter();
    StringWriter error = new StringWriter();
    engine.getContext().setWriter(output);
    engine.getContext().setErrorWriter(new PrintWriter(error));
    VariablesMap variables = new VariablesMap();
    variables.getInheritedMap().put(SchedulerVars.PA_TASK_REPLICATION.toString(), 42);
    engine.getContext().setAttribute("variables", variables, ScriptContext.ENGINE_SCOPE);
    Object result = engine.eval(ReturnReplicationIndex.class.getName());
    assertEquals(42, result);
}
Also used : StringWriter(java.io.StringWriter) ScriptEngineManager(javax.script.ScriptEngineManager) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ScriptEngine(javax.script.ScriptEngine) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 14 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class InProcessTaskExecutor method execute.

/**
 * Executes a task inside a task context.
 *
 * @param taskContext Task context to execute.
 * @param output      Standard output sink.
 * @param error       Error sink.
 * @return Returns the task result.
 */
@Override
public TaskResultImpl execute(TaskContext taskContext, PrintStream output, PrintStream error) {
    ScriptHandler scriptHandler = ScriptLoader.createLocalHandler();
    String nodesFile = null;
    SchedulerNodeClient schedulerNodeClient = null;
    RemoteSpace userSpaceClient = null;
    RemoteSpace globalSpaceClient = null;
    try {
        nodesFile = writeNodesFile(taskContext);
        VariablesMap variables = new VariablesMap();
        variables.setInheritedMap(taskContextVariableExtractor.getAllNonTaskVariablesInjectNodesFile(taskContext, nodesFile));
        variables.setScopeMap(taskContextVariableExtractor.getScopeVariables(taskContext));
        Map<String, String> resultMetadata = new HashMap<>();
        Map<String, String> thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(taskContext);
        schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(taskContext);
        userSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.USER);
        globalSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL);
        forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, taskContext, variables, thirdPartyCredentials, schedulerNodeClient, userSpaceClient, globalSpaceClient, resultMetadata);
        Stopwatch stopwatch = Stopwatch.createUnstarted();
        TaskResultImpl taskResult;
        try {
            stopwatch.start();
            Serializable result = execute(taskContext, output, error, scriptHandler, thirdPartyCredentials, variables);
            stopwatch.stop();
            taskResult = new TaskResultImpl(taskContext.getTaskId(), result, null, stopwatch.elapsed(TimeUnit.MILLISECONDS));
        } catch (Throwable e) {
            stopwatch.stop();
            e.printStackTrace(error);
            taskResult = new TaskResultImpl(taskContext.getTaskId(), e, null, stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
        executeFlowScript(taskContext.getControlFlowScript(), scriptHandler, output, error, taskResult);
        taskResult.setPropagatedVariables(SerializationUtil.serializeVariableMap(variables.getPropagatedVariables()));
        taskResult.setMetadata(resultMetadata);
        return taskResult;
    } catch (Throwable e) {
        e.printStackTrace(error);
        return new TaskResultImpl(taskContext.getTaskId(), e);
    } finally {
        if (nodesFile != null && !nodesFile.isEmpty()) {
            FileUtils.deleteQuietly(new File(nodesFile));
        }
    }
}
Also used : RemoteSpace(org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace) Serializable(java.io.Serializable) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) SchedulerNodeClient(org.ow2.proactive.scheduler.task.client.SchedulerNodeClient) HashMap(java.util.HashMap) Stopwatch(com.google.common.base.Stopwatch) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) File(java.io.File) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Example 15 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class TerminationData method terminateRunningTask.

private void terminateRunningTask(SchedulingService service, TaskTerminationData taskToTerminate, RunningTaskData taskData) {
    Map<String, String> genericInformation = new HashMap<>();
    VariablesMap variables = null;
    if (taskToTerminate.internalJob != null) {
        genericInformation = taskData.getTask().getRuntimeGenericInformation();
    }
    try {
        variables = getStringSerializableMap(service, taskToTerminate);
    } catch (Exception e) {
        logger.error("Exception occurred, fail to get variables into the cleaning script: ", e);
    }
    try {
        if (taskToTerminate.terminationStatus == ABORTED) {
            taskData.getLauncher().kill();
        }
    } catch (Throwable t) {
        logger.info("Cannot terminate task launcher for task '" + taskData.getTask().getId() + "'", t);
        try {
            logger.info("Task launcher that cannot be terminated is identified by " + taskData.getLauncher().toString());
        } catch (Throwable ignore) {
            logger.info("Getting information about Task launcher failed (remote object not accessible?)");
        }
    }
    try {
        logger.debug("Releasing nodes for task '" + taskData.getTask().getId() + "'");
        RMProxiesManager proxiesManager = service.getInfrastructure().getRMProxiesManager();
        proxiesManager.getUserRMProxy(taskData.getUser(), taskData.getCredentials()).releaseNodes(taskData.getNodes(), taskToTerminate.terminationStatus != NODEFAILED ? taskData.getTask().getCleaningScript() : null, variables, genericInformation, taskToTerminate.taskData.getTask().getId(), service.addThirdPartyCredentials(taskData.getCredentials()));
    } catch (Throwable t) {
        logger.info("Failed to release nodes for task '" + taskData.getTask().getId() + "'", t);
    }
}
Also used : HashMap(java.util.HashMap) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) IOException(java.io.IOException) RMProxiesManager(org.ow2.proactive.scheduler.core.rmproxies.RMProxiesManager)

Aggregations

VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)22 Test (org.junit.Test)20 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)17 HashMap (java.util.HashMap)13 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)10 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)9 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)9 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)8 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)8 Serializable (java.io.Serializable)7 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)4 SchedulerNodeClient (org.ow2.proactive.scheduler.task.client.SchedulerNodeClient)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)3 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)3 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 LoginException (javax.security.auth.login.LoginException)2