Search in sources :

Example 61 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskLoggerTest method logPattern.

@Test
public void logPattern() throws Exception {
    TaskId taskId = TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L);
    taskLogger = new TaskLogger(taskId, "myhost");
    assertEquals("", taskLogger.getLogs().getAllLogs(false));
    taskLogger.getOutputSink().println("hello");
    String quotedStringTaskId = Pattern.quote(taskId.toString());
    assertTrue(taskLogger.getLogs().getAllLogs(true).matches("\\[" + quotedStringTaskId + "@myhost;[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\] hello \r?\n"));
    taskLogger.getErrorSink().println("error");
    assertTrue(taskLogger.getLogs().getStderrLogs(true).matches("\\[" + quotedStringTaskId + "@myhost;[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\] error \r?\n"));
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) Test(org.junit.Test)

Example 62 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskLoggerTest method logStreaming.

@Test
public void logStreaming() throws Exception {
    taskLogger = new TaskLogger(TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L), "myhost");
    final StringWriter stringAppender = new StringWriter();
    AppenderProvider stringAppenderProvider = new AppenderProvider() {

        @Override
        public Appender getAppender() throws LogForwardingException {
            return new WriterAppender(new PatternLayout("%m%n"), stringAppender);
        }
    };
    taskLogger.activateLogs(stringAppenderProvider);
    assertEquals("", stringAppender.toString());
    taskLogger.getOutputSink().println("hello");
    // async appender, it gets buffered
    assertEquals("", stringAppender.toString());
    for (int i = 0; i < 1000; i++) {
        taskLogger.getOutputSink().println("hello");
    }
    assertNotEquals("", stringAppender.toString());
}
Also used : StringWriter(java.io.StringWriter) AppenderProvider(org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider) PatternLayout(org.apache.log4j.PatternLayout) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) WriterAppender(org.apache.log4j.WriterAppender) Test(org.junit.Test)

Example 63 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskContextVariableExtractorTest method testExtractThrowsExceptionIfVariablesAreInvalidByteStream.

@Test(expected = Exception.class)
public void testExtractThrowsExceptionIfVariablesAreInvalidByteStream() throws Exception {
    ScriptExecutableContainer scriptContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    TaskLauncherInitializer taskLauncherInitializer = getTaskLauncherInitializerWithWorkflowVariables();
    Map<String, byte[]> taskResultVariables = new HashMap<>();
    // The task result variables are expected to be converted to byte streams.
    taskResultVariables.put(taskResultPropagatedVariables1Key, taskResultPropagatedVariables1Value.getBytes());
    TaskResultImpl taskResult = new TaskResultImpl(taskLauncherInitializer.getTaskId(), new Exception("Exception"));
    taskResult.setPropagatedVariables(taskResultVariables);
    TaskResult[] taskResultArray = { taskResult };
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, taskResultArray, new NodeDataSpacesURIs(null, null, null, null, null, null), null, null);
    new TaskContextVariableExtractor().getAllVariables(taskContext);
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) Test(org.junit.Test)

Example 64 with Task

use of org.ow2.proactive.scheduler.common.task.Task in project scheduling by ow2-proactive.

the class TaskKillerTest method testThatTaskKillerInterruptsThreadImmediatelyWhenSetToZero.

@Test
public void testThatTaskKillerInterruptsThreadImmediatelyWhenSetToZero() {
    KilledThread testThreadToBeInterrupted = new KilledThread();
    testThreadToBeInterrupted.start();
    CleanupTimeoutGetter cleanupTimeoutGetterMock = mock(CleanupTimeoutGetter.class);
    doReturn(0L).when(cleanupTimeoutGetterMock).getCleanupTimeSeconds();
    TaskKiller taskKiller = new TaskKiller(testThreadToBeInterrupted, cleanupTimeoutGetterMock);
    assertThat("Task Killer must not interrupt thread before kill() is called", testThreadToBeInterrupted.isInterruptedOnce, is(false));
    assertThat("Task Killer must not interrupt thread before kill() is called", testThreadToBeInterrupted.isInterruptedMoreThanOnce, is(false));
    startKilling(taskKiller);
    // Wait 100 milliseconds for killing thread to start
    waitOrFailTest(100);
    assertThat("Task Killer must interrupt once if kill() is called and then wait for the timeout which is set to 10 seconds.", testThreadToBeInterrupted.isInterruptedOnce, is(true));
    assertThat("Task Killer must only interrupt once (not twice) after kill() is called and then wait for the timeout which is set to 10 seconds.", testThreadToBeInterrupted.isInterruptedMoreThanOnce, is(true));
    // Cleanup - remove system property
    System.clearProperty(this.taskKillerCleanupTimePropertyName);
}
Also used : CleanupTimeoutGetter(org.ow2.proactive.scheduler.task.utils.task.termination.CleanupTimeoutGetter) TaskKiller(org.ow2.proactive.scheduler.task.utils.task.termination.TaskKiller) Test(org.junit.Test)

Example 65 with Task

use of org.ow2.proactive.scheduler.common.task.Task 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)

Aggregations

Test (org.junit.Test)152 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)112 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)100 JobId (org.ow2.proactive.scheduler.common.job.JobId)73 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)69 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)68 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)60 ArrayList (java.util.ArrayList)49 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)48 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)43 Task (org.ow2.proactive.scheduler.common.task.Task)41 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)41 File (java.io.File)37 Scheduler (org.ow2.proactive.scheduler.common.Scheduler)34 HashMap (java.util.HashMap)33 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)33 SimpleScript (org.ow2.proactive.scripting.SimpleScript)33 InternalScriptTask (org.ow2.proactive.scheduler.task.internal.InternalScriptTask)27 TaskScript (org.ow2.proactive.scripting.TaskScript)27 UnknownTaskException (org.ow2.proactive.scheduler.common.exception.UnknownTaskException)26