Search in sources :

Example 6 with SendTask

use of cz.metacentrum.perun.taskslib.model.SendTask in project perun by CESNET.

the class SendWorkerImpl method call.

@Override
public SendTask call() throws TaskExecutionException {
    Task task = sendTask.getTask();
    Service service = task.getService();
    // we never actually run DUMMY destinations !!
    if (sendTask.getDestination().getPropagationType().equals(Destination.PROPAGATIONTYPE_DUMMY)) {
        log.info("[{}] Executing SEND worker skipped for dummy Destination: {}. Marked as SENT.", sendTask.getTask().getId(), sendTask.getDestination().getDestination());
        // set results
        sendTask.setStatus(SENT);
        sendTask.setStdout("");
        sendTask.setStderr("");
        sendTask.setReturnCode(0);
        sendTask.setEndTime(new Date(System.currentTimeMillis()));
        return sendTask;
    }
    log.info("[{}] Executing SEND worker for Task with Service ID: {} and Facility ID: {} and Destination: {}", sendTask.getTask().getId(), sendTask.getTask().getServiceId(), sendTask.getTask().getFacilityId(), sendTask.getDestination().getDestination());
    ProcessBuilder pb = new ProcessBuilder(service.getScript(), task.getFacility().getName(), sendTask.getDestination().getDestination(), sendTask.getDestination().getType());
    try {
        // start the script and wait for results
        super.execute(pb);
        // set results
        sendTask.setStdout(super.getStdout());
        sendTask.setStderr(super.getStderr());
        sendTask.setReturnCode(super.getReturnCode());
        sendTask.setEndTime(new Date(System.currentTimeMillis()));
        if (getReturnCode() != 0) {
            log.error("[{}] SEND worker failed for Task. Ret code {}, STDOUT: {}, STDERR: {}", task.getId(), getReturnCode(), getStdout(), getStderr());
            sendTask.setStatus(ERROR);
            // XXX: why exception? There is nothing exceptional about the situation.
            throw new TaskExecutionException(task, sendTask.getDestination(), getReturnCode(), getStdout(), getStderr());
        } else {
            if (getStderr().isEmpty()) {
                sendTask.setStatus(SENT);
            } else {
                sendTask.setStatus(WARNING);
            }
            log.info("[{}] SEND worker finished for Task with status {}. Ret code {}, STDOUT: {}, STDERR: {}", sendTask.getTask().getId(), sendTask.getStatus(), getReturnCode(), getStdout(), getStderr());
            return sendTask;
        }
    } catch (IOException e) {
        log.error("[{}] SEND worker failed for Task. IOException: {}.", task.getId(), e);
        sendTask.setStatus(ERROR);
        sendTask.setEndTime(new Date(System.currentTimeMillis()));
        throw new TaskExecutionException(task, sendTask.getDestination(), 2, "", e.getMessage());
    } catch (InterruptedException e) {
        log.warn("[{}] SEND worker failed for Task. Execution was interrupted {}.", task.getId(), e);
        sendTask.setStatus(ERROR);
        sendTask.setEndTime(new Date(System.currentTimeMillis()));
        throw new TaskExecutionException(task, sendTask.getDestination(), 1, "", e.getMessage());
    }
}
Also used : TaskExecutionException(cz.metacentrum.perun.engine.exceptions.TaskExecutionException) Task(cz.metacentrum.perun.taskslib.model.Task) SendTask(cz.metacentrum.perun.taskslib.model.SendTask) Service(cz.metacentrum.perun.core.api.Service) IOException(java.io.IOException) Date(java.util.Date)

Example 7 with SendTask

use of cz.metacentrum.perun.taskslib.model.SendTask in project perun by CESNET.

the class SendWorkerImplTest method testSendWorkerSuccess.

@Test
public void testSendWorkerSuccess() throws Exception {
    SendWorker worker = new SendWorkerImpl(sendTask1, null);
    SendTask resultSendTask = worker.call();
    assertEquals(SENT, resultSendTask.getStatus());
    assertEquals((long) 0, (long) resultSendTask.getReturnCode());
    Date now = new Date(System.currentTimeMillis());
    assertTrue(resultSendTask.getEndTime().before(now) || resultSendTask.getEndTime().equals(now));
}
Also used : SendWorker(cz.metacentrum.perun.engine.scheduling.SendWorker) SendTask(cz.metacentrum.perun.taskslib.model.SendTask) Date(java.util.Date) SendWorkerImpl(cz.metacentrum.perun.engine.scheduling.impl.SendWorkerImpl) Test(org.junit.Test) AbstractEngineTest(cz.metacentrum.perun.engine.AbstractEngineTest)

Example 8 with SendTask

use of cz.metacentrum.perun.taskslib.model.SendTask in project perun by CESNET.

the class SendPlanner method run.

@Override
public void run() {
    BlockingQueue<Task> generatedTasks = schedulingPool.getGeneratedTasksQueue();
    while (!shouldStop()) {
        try {
            Task task = generatedTasks.take();
            // has destinations -> SENDING
            if (task.getDestinations().isEmpty()) {
                task.setStatus(Task.TaskStatus.ERROR);
                try {
                    jmsQueueManager.reportTaskStatus(task.getId(), task.getStatus(), System.currentTimeMillis());
                } catch (JMSException e) {
                    jmsLogError(task);
                }
                try {
                    schedulingPool.removeTask(task);
                } catch (TaskStoreException e) {
                    log.error("[{}] Generated Task without destinations could not be removed from SchedulingPool: {}", task.getId(), e);
                }
                // skip to next generated Task
                continue;
            }
            // Task has destinations
            task.setStatus(Task.TaskStatus.SENDING);
            // TODO - would be probably better to have this as one time call after first SendWorker is submitted
            // TODO   but then processing stuck tasks must reflect, that SENDING task might have sendStartTime=NULL
            task.setSendStartTime(LocalDateTime.now());
            schedulingPool.addSendTaskCount(task, task.getDestinations().size());
            try {
                jmsQueueManager.reportTaskStatus(task.getId(), task.getStatus(), task.getSendStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
            } catch (JMSException e) {
                jmsLogError(task);
            }
            // create SendTask and SendWorker for each Destination
            for (Destination destination : task.getDestinations()) {
                // submit for execution
                SendTask sendTask = new SendTask(task, destination);
                SendWorker worker = new SendWorkerImpl(sendTask, directory);
                sendCompletionService.blockingSubmit(worker);
            }
        } catch (InterruptedException e) {
            String errorStr = "Thread planning SendTasks was interrupted.";
            log.error(errorStr);
            throw new RuntimeException(errorStr, e);
        } catch (Throwable ex) {
            log.error("Unexpected exception in SendPlanner thread. Stuck Tasks will be cleaned by PropagationMaintainer#endStuckTasks() later.", ex);
        }
    }
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) SendWorker(cz.metacentrum.perun.engine.scheduling.SendWorker) Task(cz.metacentrum.perun.taskslib.model.Task) SendTask(cz.metacentrum.perun.taskslib.model.SendTask) SendTask(cz.metacentrum.perun.taskslib.model.SendTask) JMSException(javax.jms.JMSException) TaskStoreException(cz.metacentrum.perun.taskslib.exceptions.TaskStoreException) SendWorkerImpl(cz.metacentrum.perun.engine.scheduling.impl.SendWorkerImpl)

Aggregations

SendTask (cz.metacentrum.perun.taskslib.model.SendTask)8 Task (cz.metacentrum.perun.taskslib.model.Task)5 Date (java.util.Date)5 TaskExecutionException (cz.metacentrum.perun.engine.exceptions.TaskExecutionException)4 SendWorker (cz.metacentrum.perun.engine.scheduling.SendWorker)3 TaskStoreException (cz.metacentrum.perun.taskslib.exceptions.TaskStoreException)3 Destination (cz.metacentrum.perun.core.api.Destination)2 Service (cz.metacentrum.perun.core.api.Service)2 SendWorkerImpl (cz.metacentrum.perun.engine.scheduling.impl.SendWorkerImpl)2 JMSException (javax.jms.JMSException)2 AbstractEngineTest (cz.metacentrum.perun.engine.AbstractEngineTest)1 BlockingSendExecutorCompletionService (cz.metacentrum.perun.engine.scheduling.impl.BlockingSendExecutorCompletionService)1 TaskResult (cz.metacentrum.perun.taskslib.model.TaskResult)1 IOException (java.io.IOException)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Future (java.util.concurrent.Future)1 Before (org.junit.Before)1 Test (org.junit.Test)1