Search in sources :

Example 11 with TaskResult

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

the class ExecutorEngineWorkerImpl method run.

@Override
public void run() {
    log.info("EXECUTING(worker:" + this.hashCode() + "): Task ID:" + task.getId() + ", Facility ID:" + task.getFacilityId() + ", ExecService ID:" + task.getExecServiceId() + ", ExecServiceType:" + execService.getExecServiceType());
    String stdout = null;
    String stderr = null;
    int returnCode = -1;
    if (execService.getExecServiceType().equals(ExecServiceType.GENERATE)) {
        ProcessBuilder pb = new ProcessBuilder(execService.getScript(), "-f", String.valueOf(task.getFacilityId()));
        if (genDirectory != null) {
            // set path relative to current working dir
            pb.directory(genDirectory);
        }
        try {
            Process process = pb.start();
            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
            StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
            errorGobbler.start();
            outputGobbler.start();
            returnCode = process.waitFor();
            while (errorGobbler.isAlive() || outputGobbler.isAlive()) Thread.sleep(50);
            stderr = errorGobbler.getSb();
            stdout = outputGobbler.getSb();
            // TaskStatus.ERROR);
            if (returnCode != 0) {
                log.info("GEN task failed. Ret code " + returnCode + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
                resultListener.onTaskDestinationError(task, destination, null);
            } else {
                resultListener.onTaskDestinationDone(task, destination, null);
                log.info("GEN task completed. Ret code " + returnCode + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
            }
        } catch (IOException e) {
            log.error(e.toString(), e);
            // task.setStatus(TaskStatus.ERROR);
            resultListener.onTaskDestinationError(task, destination, null);
        } catch (Exception e) {
            log.error(e.toString(), e);
            // task.setStatus(TaskStatus.ERROR);
            resultListener.onTaskDestinationError(task, destination, null);
        } finally {
            String ret = returnCode == -1 ? "unknown" : String.valueOf(returnCode);
            log.debug("GEN task ended. Ret code " + ret + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
        // taskManager.updateTask(task, getEngineId());
        }
    } else if (execService.getExecServiceType().equals(ExecServiceType.SEND)) {
        ProcessBuilder pb = new ProcessBuilder(execService.getScript(), facility.getName(), destination.getDestination(), destination.getType());
        if (sendDirectory != null) {
            // set path relative to current working dir
            pb.directory(sendDirectory);
        }
        try {
            Process process = pb.start();
            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream());
            StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream());
            errorGobbler.start();
            outputGobbler.start();
            returnCode = process.waitFor();
            while (errorGobbler.isAlive() || outputGobbler.isAlive()) Thread.sleep(50);
            stderr = errorGobbler.getSb();
            stdout = outputGobbler.getSb();
            TaskResult taskResult = new TaskResult();
            taskResult.setTaskId(task.getId());
            taskResult.setDestinationId(destination.getId());
            taskResult.setErrorMessage(stderr);
            taskResult.setStandardMessage(stdout);
            taskResult.setReturnCode(returnCode);
            taskResult.setStatus(returnCode == 0 ? TaskResultStatus.DONE : TaskResultStatus.ERROR);
            taskResult.setTimestamp(new Date(System.currentTimeMillis()));
            taskResult.setService(execService.getService());
            if (taskResult.getStatus().equals(TaskResultStatus.ERROR)) {
                resultListener.onTaskDestinationError(task, destination, taskResult);
                log.info("SEND task failed. Ret code " + returnCode + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
            } else {
                resultListener.onTaskDestinationDone(task, destination, taskResult);
                log.info("SEND task completed. Ret code " + returnCode + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
            }
        } catch (Exception e) {
            log.info("SEND task ended. Ret code " + returnCode + ". STDOUT: {}  STDERR: {}. Task: " + task.getId(), stdout, stderr);
            log.error("ERROR with TASK ID: " + task.getId() + " , Exception:" + e.toString(), e);
            // If we are unable to switch to the ERROR state,
            // PropagationMaintainer would resolve
            // the Tasks status correctly anyhow (count Destinations x count
            // TaskResults)
            TaskResult taskResult = new TaskResult();
            taskResult.setTaskId(task.getId());
            taskResult.setDestinationId(destination.getId());
            taskResult.setStatus(TaskResultStatus.ERROR);
            taskResult.setTimestamp(new Date(System.currentTimeMillis()));
            taskResult.setService(execService.getService());
            resultListener.onTaskDestinationError(task, destination, taskResult);
        } finally {
            String ret = returnCode == -1 ? "unknown" : String.valueOf(returnCode);
        }
    } else {
        throw new IllegalArgumentException("Expected ExecService type is SEND or GENERATE.");
    }
}
Also used : TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException) Date(java.util.Date)

Example 12 with TaskResult

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

the class TasksManagerBlImpl method getFacilityState.

@Override
public FacilityState getFacilityState(PerunSession session, Facility facility) throws FacilityNotExistsException {
    perun.getFacilitiesManagerBl().checkFacilityExists(session, facility);
    // get all tasks
    List<Task> tasks = getTasksManagerImpl().listAllTasksForFacility(facility.getId());
    // define state
    FacilityState state = new FacilityState();
    state.setFacility(facility);
    // if no tasks we can't determine facility state
    if (tasks == null || tasks.isEmpty()) {
        state.setState(FacilityState.FacilityPropagationState.NOT_DETERMINED);
        return state;
    } else {
        // OK if no change
        state.setState(FacilityState.FacilityPropagationState.OK);
    }
    // fill all available destinations
    List<RichDestination> destinations = perun.getServicesManagerBl().getAllRichDestinations(session, facility);
    for (RichDestination rd : destinations) {
        state.getResults().put(rd.getDestination(), FacilityState.FacilityPropagationState.NOT_DETERMINED);
    }
    // magic with tasks :-)
    for (Task task : tasks) {
        // save previous facility state
        FacilityState.FacilityPropagationState facState = state.getState();
        // PROCESSING and not ERROR before
        if (Arrays.asList(Task.TaskStatus.GENERATED, Task.TaskStatus.GENERATING, Task.TaskStatus.PLANNED, Task.TaskStatus.SENDING).contains(task.getStatus()) && (facState != FacilityState.FacilityPropagationState.ERROR)) {
            state.setState(FacilityState.FacilityPropagationState.PROCESSING);
        } else // ERROR - set ERROR
        if (Arrays.asList(Task.TaskStatus.ERROR, Task.TaskStatus.GENERROR, Task.TaskStatus.SENDERROR).contains(task.getStatus())) {
            state.setState(FacilityState.FacilityPropagationState.ERROR);
        }
        // get destination status
        if (task.getService() != null) {
            List<TaskResult> results = getTasksManagerImpl().getTaskResultsByTask(task.getId());
            Map<Service, Map<Destination, TaskResult>> latestResults = new HashMap<>();
            for (TaskResult res : results) {
                if (latestResults.get(res.getService()) == null) {
                    // put in map since result for service exists
                    Map<Destination, TaskResult> value = new HashMap<>();
                    value.put(res.getDestination(), res);
                    latestResults.put(res.getService(), value);
                } else if (latestResults.get(res.getService()) != null && latestResults.get(res.getService()).get(res.getDestination()) == null) {
                    // put in inner map, since destination for service not yet exists
                    latestResults.get(res.getService()).put(res.getDestination(), res);
                } else {
                    // update in inner map since this is later task result
                    if (latestResults.get(res.getService()).get(res.getDestination()).getId() < res.getId()) {
                        // put in map
                        latestResults.get(res.getService()).put(res.getDestination(), res);
                    }
                }
            }
            for (Map<Destination, TaskResult> res : latestResults.values()) {
                for (TaskResult result : res.values()) {
                    // iterate over all latest tasks results
                    String destination = result.getDestination().getDestination();
                    FacilityState.FacilityPropagationState propState = state.getResults().get(destination);
                    // if any error => state is error
                    if (TaskResult.TaskResultStatus.ERROR.equals(result.getStatus())) {
                        state.getResults().put(destination, FacilityState.FacilityPropagationState.ERROR);
                        continue;
                    }
                    // if result ok and previous was not bad
                    if (TaskResult.TaskResultStatus.DONE.equals(result.getStatus())) {
                        if (FacilityState.FacilityPropagationState.NOT_DETERMINED.equals(propState)) {
                            state.getResults().put(destination, FacilityState.FacilityPropagationState.OK);
                        }
                    }
                }
            }
        }
    }
    return state;
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) RichDestination(cz.metacentrum.perun.core.api.RichDestination) Task(cz.metacentrum.perun.taskslib.model.Task) HashMap(java.util.HashMap) Service(cz.metacentrum.perun.core.api.Service) FacilityState(cz.metacentrum.perun.controller.model.FacilityState) RichDestination(cz.metacentrum.perun.core.api.RichDestination) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with TaskResult

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

the class AuditParser method createTaskResult.

private static TaskResult createTaskResult(Map<String, String> beanAttr) {
    if (beanAttr == null)
        return null;
    TaskResult taskResult = new TaskResult();
    taskResult.setId(Integer.valueOf(beanAttr.get("id")));
    taskResult.setTaskId(Integer.valueOf(beanAttr.get("taskId")));
    taskResult.setDestinationId(Integer.valueOf(beanAttr.get("destinationId")));
    String errorMessage;
    if (beanAttr.get("errorMessage").equals("\\0"))
        errorMessage = null;
    else {
        errorMessage = BeansUtils.eraseEscaping(beanAttr.get("errorMessage"));
    }
    taskResult.setErrorMessage(errorMessage);
    String standardMessage;
    if (beanAttr.get("standardMessage").equals("\\0"))
        standardMessage = null;
    else {
        standardMessage = BeansUtils.eraseEscaping(beanAttr.get("standardMessage"));
    }
    taskResult.setStandardMessage(standardMessage);
    taskResult.setReturnCode(Integer.valueOf(beanAttr.get("returnCode")));
    try {
        taskResult.setTimestamp(BeansUtils.getDateFormatter().parse(BeansUtils.eraseEscaping(beanAttr.get("timestamp"))));
    } catch (ParseException e) {
        throw new InternalErrorException("Error when date was parsing from String to Date.", e);
    }
    String status = BeansUtils.eraseEscaping(beanAttr.get("status"));
    TaskResultStatus st;
    if (status.equals("\\0"))
        st = null;
    else {
        if (status.equals("DENIED"))
            st = TaskResultStatus.DENIED;
        else if (status.equals("DONE"))
            st = TaskResultStatus.DONE;
        else if (status.equals("ERROR"))
            st = TaskResultStatus.ERROR;
        else if (status.equals("WARNING"))
            st = TaskResultStatus.WARNING;
        else
            st = null;
    }
    taskResult.setStatus(st);
    Service service;
    if (beanAttr.get("service").equals("\\0"))
        service = null;
    else {
        List<Pair<String, Map<String, String>>> serviceList = beansToMap(beanAttr.get("service"));
        if (serviceList.size() > 0) {
            service = createService(serviceList.get(0).getRight());
        } else
            service = null;
    }
    taskResult.setService(service);
    return taskResult;
}
Also used : TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) ParseException(java.text.ParseException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) TaskResultStatus(cz.metacentrum.perun.taskslib.model.TaskResult.TaskResultStatus)

Example 14 with TaskResult

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

the class TasksManagerBlImplTest method setUp.

@Before
public void setUp() throws Exception {
    perunSession = perun.getPerunSession(new PerunPrincipal("perunTests", ExtSourcesManager.EXTSOURCE_NAME_INTERNAL, ExtSourcesManager.EXTSOURCE_INTERNAL), new PerunClient());
    jdbcTemplate = new JdbcPerunTemplate(dataSource);
    tasksManager = ((PerunBl) perun).getTasksManagerBl();
    facility1 = new Facility();
    facility2 = new Facility();
    // Test Service #1
    testService1 = new Service();
    testService1.setName("Test_service_1_" + Long.toHexString(System.currentTimeMillis()));
    testService1.setDelay(1);
    testService1.setRecurrence(1);
    testService1.setEnabled(true);
    testService1.setScript("/hellish/test/script");
    testService1.setId(servicesManager.createService(perunSession, testService1).getId());
    // Test Service #2
    testService2 = new Service();
    testService2.setName("Test_service_2_" + Long.toHexString(System.currentTimeMillis()));
    testService2.setDelay(1);
    testService2.setRecurrence(1);
    testService2.setEnabled(true);
    testService2.setScript("/hellish/test/script");
    testService2.setId(servicesManager.createService(perunSession, testService2).getId());
    // 
    // Testing Destination #1
    testDestinationId1 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId1, "test.destination." + testDestinationId1);
    // Testing Destination #2
    testDestinationId2 = Utils.getNewId(jdbcTemplate, "destinations_id_seq");
    jdbcTemplate.update("insert into destinations(id, destination, type) values (?,?,'host')", testDestinationId2, "test.destination." + testDestinationId2);
    // Testing Facility #1
    testFacilityId1 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId1, "Cluster_" + testFacilityId1);
    facility1.setId(testFacilityId1);
    // Testing Facility #2
    testFacilityId2 = Utils.getNewId(jdbcTemplate, "facilities_id_seq");
    jdbcTemplate.update("insert into facilities(id, name) values (?,?)", testFacilityId2, "Cluster_" + testFacilityId2);
    facility2.setId(testFacilityId2);
    // facility_service_destinations
    destination1 = ((PerunBl) perun).getServicesManagerBl().getDestinationById(perunSession, testDestinationId1);
    destination2 = ((PerunBl) perun).getServicesManagerBl().getDestinationById(perunSession, testDestinationId2);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility1, destination1);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility1, destination2);
    ((PerunBl) perun).getServicesManagerBl().addDestination(perunSession, testService1, facility2, destination2);
    // vo
    vo = new Vo(0, "TasksManagerTestVo", "TMTestVo");
    vo = ((PerunBl) perun).getVosManagerBl().createVo(perunSession, vo);
    // resource
    resource = new Resource();
    resource.setName("TasksManagerTestResource");
    resource.setDescription("Testovaci");
    resource = ((PerunBl) perun).getResourcesManagerBl().createResource(perunSession, resource, vo, facility1);
    // tasks
    task1 = new Task();
    task1.setFacility(facility1);
    task1.setService(testService1);
    task1.setSchedule(0L);
    task1.setStatus(TaskStatus.DONE);
    List<Destination> destinationsList = new ArrayList<>();
    destinationsList.add(destination1);
    destinationsList.add(destination2);
    task1.setDestinations(destinationsList);
    task1Id = tasksManager.insertTask(perunSession, task1);
    task1.setId(task1Id);
    // tasks
    task2 = new Task();
    task2.setFacility(facility2);
    task2.setService(testService1);
    task2.setSchedule(0L);
    task2.setStatus(TaskStatus.WARNING);
    destinationsList = new ArrayList<>();
    destinationsList.add(destination2);
    task2.setDestinations(destinationsList);
    task2Id = tasksManager.insertTask(perunSession, task2);
    task2.setId(task2Id);
    // task results
    result1 = new TaskResult();
    result1.setDestination(destination1);
    result1.setDestinationId(testDestinationId1);
    result1.setService(testService1);
    result1.setTaskId(task1Id);
    result1.setStatus(TaskResultStatus.DONE);
    result1.setTimestamp(new Date());
    result1Id = tasksManager.insertNewTaskResult(perunSession, result1);
    result1.setId(result1Id);
    // task results
    result2 = new TaskResult();
    result2.setDestination(destination1);
    result2.setDestinationId(testDestinationId1);
    result2.setService(testService1);
    result2.setTaskId(task1Id);
    result2.setStatus(TaskResultStatus.DONE);
    result2.setTimestamp(Date.from(LocalDate.now().minusDays(7).atStartOfDay(ZoneId.systemDefault()).toInstant()));
    result2Id = tasksManager.insertNewTaskResult(perunSession, result2);
    result2.setId(result2Id);
    // task results
    result3 = new TaskResult();
    result3.setDestination(destination2);
    result3.setDestinationId(testDestinationId2);
    result3.setService(testService1);
    result3.setTaskId(task1Id);
    result3.setStatus(TaskResultStatus.DONE);
    result3.setTimestamp(Date.from(LocalDate.now().minusDays(7).atStartOfDay(ZoneId.systemDefault()).toInstant()));
    result3Id = tasksManager.insertNewTaskResult(perunSession, result3);
    result3.setId(result3Id);
    jdbcTemplate.query("select id from tasks_results where task_id = ?", row -> {
        System.out.println("ID: " + row.getInt("id"));
    }, task2Id);
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) Task(cz.metacentrum.perun.taskslib.model.Task) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) Service(cz.metacentrum.perun.core.api.Service) Date(java.util.Date) LocalDate(java.time.LocalDate) JdbcPerunTemplate(org.springframework.jdbc.core.JdbcPerunTemplate) PerunClient(cz.metacentrum.perun.core.api.PerunClient) PerunPrincipal(cz.metacentrum.perun.core.api.PerunPrincipal) Vo(cz.metacentrum.perun.core.api.Vo) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) Facility(cz.metacentrum.perun.core.api.Facility) Before(org.junit.Before)

Aggregations

TaskResult (cz.metacentrum.perun.taskslib.model.TaskResult)14 Date (java.util.Date)7 Task (cz.metacentrum.perun.taskslib.model.Task)6 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)5 Service (cz.metacentrum.perun.core.api.Service)4 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)4 Destination (cz.metacentrum.perun.core.api.Destination)3 Facility (cz.metacentrum.perun.core.api.Facility)3 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)3 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)3 FacilityState (cz.metacentrum.perun.controller.model.FacilityState)2 PerunBean (cz.metacentrum.perun.core.api.PerunBean)2 TaskStoreException (cz.metacentrum.perun.taskslib.exceptions.TaskStoreException)2 ExecService (cz.metacentrum.perun.taskslib.model.ExecService)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 ForcePropagationOnFacilityAndService (cz.metacentrum.perun.audit.events.GeneralServiceManagerEvents.ForcePropagationOnFacilityAndService)1 ForcePropagationOnService (cz.metacentrum.perun.audit.events.GeneralServiceManagerEvents.ForcePropagationOnService)1