Search in sources :

Example 1 with EngineMessageProducer

use of cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer in project perun by CESNET.

the class TaskScheduler method sendToEngine.

/**
 * Send Task to Engine. Called when it waited long enough in a waiting queue (listening for other changes).
 *
 * @param task Task to be send to Engine
 * @return Resulting state if Task was sent or denied or any error happened.
 */
protected TaskScheduled sendToEngine(Task task) {
    Service service = task.getService();
    Facility facility = task.getFacility();
    try {
        initPerunSession();
        service = perun.getServicesManager().getServiceById(perunSession, service.getId());
        facility = perun.getFacilitiesManager().getFacilityById(perunSession, facility.getId());
        task.setService(service);
        task.setFacility(facility);
    } catch (ServiceNotExistsException e) {
        log.error("[{}] Service for task does not exist...", task.getId());
        task.setEndTime(LocalDateTime.now());
        task.setStatus(TaskStatus.ERROR);
        return DB_ERROR;
    } catch (FacilityNotExistsException e) {
        log.error("[{}] Facility for task does not exist...", task.getId());
        task.setEndTime(LocalDateTime.now());
        task.setStatus(TaskStatus.ERROR);
        return DB_ERROR;
    } catch (PrivilegeException e) {
        log.error("[{}] Privilege error accessing the database: {}", task.getId(), e.getMessage());
        task.setEndTime(LocalDateTime.now());
        task.setStatus(TaskStatus.ERROR);
        return DB_ERROR;
    } catch (InternalErrorException e) {
        log.error("[{}] Internal error: {}", task.getId(), e.getMessage());
        task.setEndTime(LocalDateTime.now());
        task.setStatus(TaskStatus.ERROR);
        return DB_ERROR;
    }
    EngineMessageProducer engineMessageProducer = engineMessageProducerFactory.getProducer();
    log.debug("[{}] Scheduling {}.", task.getId(), task);
    if (engineMessageProducer != null) {
        log.debug("[{}] Assigned queue {} to task.", task.getId(), engineMessageProducer.getQueueName());
    } else {
        log.error("[{}] There are no engines registered.", task.getId());
        return QUEUE_ERROR;
    }
    if (service.isEnabled()) {
        log.debug("[{}] Service {} is enabled globally.", task.getId(), service.getId());
    } else {
        log.debug("[{}] Service {} is disabled globally.", task.getId(), service.getId());
        return DENIED;
    }
    try {
        if (!((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnFacility(service, facility)) {
            log.debug("[{}] Service {} is allowed on Facility {}.", task.getId(), service.getId(), facility.getId());
        } else {
            log.debug("[{}] Service {} is blocked on Facility {}.", task.getId(), service.getId(), facility.getId());
            return DENIED;
        }
    } catch (Exception e) {
        log.error("[{}] Error getting disabled status for Service, task will not run now: {}.", task.getId(), e);
        return ERROR;
    }
    // task|[task_id][is_forced][exec_service_id][facility]|[destination_list]|[dependency_list]
    // - the task|[engine_id] part is added by dispatcherQueue
    List<Destination> destinations = task.getDestinations();
    if (task.isSourceUpdated() || destinations == null || destinations.isEmpty()) {
        log.trace("[{}] No destinations for task, trying to query the database.", task.getId());
        try {
            initPerunSession();
            destinations = perun.getServicesManager().getDestinations(perunSession, task.getService(), task.getFacility());
        } catch (ServiceNotExistsException e) {
            log.error("[{}] No destinations found for task. Service not exists...", task.getId());
            task.setEndTime(LocalDateTime.now());
            task.setStatus(TaskStatus.ERROR);
            return DB_ERROR;
        } catch (FacilityNotExistsException e) {
            log.error("[{}] No destinations found for task. Facility for task does not exist...", task.getId());
            task.setEndTime(LocalDateTime.now());
            task.setStatus(TaskStatus.ERROR);
            return DB_ERROR;
        } catch (PrivilegeException e) {
            log.error("[{}] No destinations found for task. Privilege error accessing the database: {}", task.getId(), e.getMessage());
            task.setEndTime(LocalDateTime.now());
            task.setStatus(TaskStatus.ERROR);
            return DB_ERROR;
        } catch (InternalErrorException e) {
            log.error("[{}] No destinations found for task. Internal error: {}", task.getId(), e.getMessage());
            task.setEndTime(LocalDateTime.now());
            task.setStatus(TaskStatus.ERROR);
            return DB_ERROR;
        }
    }
    log.debug("[{}] Fetched destinations: {}", task.getId(), (destinations == null) ? "[]" : destinations.toString());
    if (destinations != null && !destinations.isEmpty()) {
        Iterator<Destination> iter = destinations.iterator();
        while (iter.hasNext()) {
            Destination dest = iter.next();
            if (((PerunBl) perun).getServicesManagerBl().isServiceBlockedOnDestination(service, dest.getId())) {
                // create fake task result to let admin know about the block
                TaskResult result = new TaskResult();
                result.setTaskId(task.getId());
                result.setDestination(dest);
                result.setDestinationId(dest.getId());
                result.setService(service);
                result.setStandardMessage("");
                result.setErrorMessage("Destination is blocked in Perun.");
                result.setReturnCode(1);
                result.setId(0);
                result.setTimestamp(new Date(System.currentTimeMillis()));
                result.setStatus(TaskResult.TaskResultStatus.DENIED);
                try {
                    schedulingPool.onTaskDestinationComplete(result);
                } catch (Exception ex) {
                    log.warn("Couldn't store fake TaskResult about blocked destination.");
                }
                // actually remove from destinations sent to engine
                iter.remove();
                log.debug("[{}] Removed blocked destination: {}", task.getId(), dest.toString());
            }
        }
        if (destinations.isEmpty()) {
            // when service is blocked globally in Perun or on facility as a whole.
            return DENIED;
        }
    }
    task.setDestinations(destinations);
    // construct JMS message for Engine
    StringBuilder destinations_s = new StringBuilder("Destinations [");
    if (destinations != null) {
        for (Destination destination : destinations) {
            destinations_s.append(destination.serializeToString()).append(", ");
        }
    }
    destinations_s.append("]");
    // send message async
    engineMessageProducer.sendMessage("[" + task.getId() + "][" + task.isPropagationForced() + "]|[" + fixStringSeparators(task.getService().serializeToString()) + "]|[" + fixStringSeparators(task.getFacility().serializeToString()) + "]|[" + fixStringSeparators(destinations_s.toString()) + "]");
    // modify task status and reset forced flag
    task.setSentToEngine(LocalDateTime.now());
    task.setStatus(Task.TaskStatus.PLANNED);
    task.setPropagationForced(false);
    return SUCCESS;
}
Also used : Destination(cz.metacentrum.perun.core.api.Destination) EngineMessageProducer(cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer) Service(cz.metacentrum.perun.core.api.Service) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) Date(java.util.Date) ServiceNotExistsException(cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) TaskResult(cz.metacentrum.perun.taskslib.model.TaskResult) Facility(cz.metacentrum.perun.core.api.Facility)

Example 2 with EngineMessageProducer

use of cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer in project perun by CESNET.

the class SchedulingPoolImpl method reloadTasks.

@Override
public void reloadTasks() {
    log.debug("Going to reload Tasks from database...");
    this.clear();
    EngineMessageProducer queue = engineMessageProducerFactory.getProducer();
    for (Task task : tasksManagerBl.listAllTasks(sess)) {
        try {
            // just add DB Task to in-memory structure
            addToPool(task);
        } catch (TaskStoreException e) {
            log.error("Adding Task {} and Queue {} into SchedulingPool failed, so the Task will be lost.", task, queue);
        }
        // done/error tasks will be rescheduled later by periodic jobs of PropagationMaintainer !!
        if (Arrays.asList(TaskStatus.WAITING, TaskStatus.PLANNED, TaskStatus.GENERATING, TaskStatus.SENDING).contains(task.getStatus())) {
            if (task.getStatus().equals(TaskStatus.WAITING)) {
                // reset timestamp to 'now' for WAITING Tasks, since scheduling task
                // sets this to all tasks except the waiting
                task.setSchedule(LocalDateTime.now());
                tasksManagerBl.updateTask(sess, task);
            }
            scheduleTask(task, 0);
        }
    }
    log.debug("Reload of Tasks from database finished.");
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) EngineMessageProducer(cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer) TaskStoreException(cz.metacentrum.perun.taskslib.exceptions.TaskStoreException)

Example 3 with EngineMessageProducer

use of cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer in project perun by CESNET.

the class EventProcessorTest method eventProcessorTest.

@Test
public void eventProcessorTest() {
    System.out.println("EventProcessor.eventProcessorTest()");
    EngineMessageProducer engineMessageProducer = new EngineMessageProducerMock("testQueue");
    eventProcessor.getEngineMessageProducerFactory().setProducer(engineMessageProducer);
    LinkedBlockingQueue<Event> mockQueue = new LinkedBlockingQueue<>();
    Event event = new Event();
    event.setTimeStamp(System.currentTimeMillis());
    event.setHeader("portishead");
    event.setData(new DirectMemberAddedToGroup(member1, group1));
    mockQueue.add(event);
    eventProcessor.setEventQueue(mockQueue);
    SchedulingPoolMock pool = new SchedulingPoolMock(2);
    eventProcessor.setSchedulingPool(pool);
    // runs inside this thread, should end when message is delivered
    // this necessitates the use of test timeout
    eventProcessor.run();
    List<Task> addedTasks = pool.getTasks();
    List<Facility> facilities = new LinkedList<>();
    for (Task task : addedTasks) {
        Assert.isTrue(service1.equals(task.getService()) || service2.equals(task.getService()), "task service is different");
        Assert.isTrue(facility1.equals(task.getFacility()), "task Facility is different");
        Assert.isTrue(task.getStatus().equals(TaskStatus.WAITING), "task status is not waiting");
    }
}
Also used : Task(cz.metacentrum.perun.taskslib.model.Task) EngineMessageProducer(cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer) DirectMemberAddedToGroup(cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup) Event(cz.metacentrum.perun.dispatcher.model.Event) Facility(cz.metacentrum.perun.core.api.Facility) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) LinkedList(java.util.LinkedList) AbstractDispatcherTest(cz.metacentrum.perun.dispatcher.AbstractDispatcherTest) Test(org.junit.Test)

Aggregations

EngineMessageProducer (cz.metacentrum.perun.dispatcher.jms.EngineMessageProducer)3 Facility (cz.metacentrum.perun.core.api.Facility)2 Task (cz.metacentrum.perun.taskslib.model.Task)2 DirectMemberAddedToGroup (cz.metacentrum.perun.audit.events.GroupManagerEvents.DirectMemberAddedToGroup)1 Destination (cz.metacentrum.perun.core.api.Destination)1 Service (cz.metacentrum.perun.core.api.Service)1 FacilityNotExistsException (cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)1 ServiceNotExistsException (cz.metacentrum.perun.core.api.exceptions.ServiceNotExistsException)1 PerunBl (cz.metacentrum.perun.core.bl.PerunBl)1 AbstractDispatcherTest (cz.metacentrum.perun.dispatcher.AbstractDispatcherTest)1 Event (cz.metacentrum.perun.dispatcher.model.Event)1 TaskStoreException (cz.metacentrum.perun.taskslib.exceptions.TaskStoreException)1 TaskResult (cz.metacentrum.perun.taskslib.model.TaskResult)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 Test (org.junit.Test)1