Search in sources :

Example 1 with Perun

use of cz.metacentrum.perun.core.api.Perun in project perun by CESNET.

the class EventDispatcherImpl method run.

@Override
public void run() {
    if (!ldapProperties.propsLoaded())
        throw new RuntimeException("LdapcProperties is not autowired correctly!");
    running = true;
    AuditMessage message = null;
    List<AuditMessage> messages;
    try {
        PerunSession perunSession = ldapcManager.getPerunSession();
        Perun perun = ldapcManager.getPerunBl();
        if (lastProcessedIdNumber == 0) {
            loadLastProcessedId();
        }
        // If running is true, then this process will be continuously
        while (running) {
            messages = null;
            int sleepTime = 1000;
            // Waiting for new messages. If consumer failed in some internal case, waiting until it will be repaired (waiting time is increases by each attempt)
            do {
                try {
                    // IMPORTANT STEP1: Get new bulk of messages
                    messages = perun.getAuditMessagesManager().pollConsumerMessages(perunSession, ldapProperties.getLdapConsumerName(), lastProcessedIdNumber);
                // Rpc.AuditMessagesManager.pollConsumerMessages(ldapcManager.getRpcCaller(), ldapProperties.getLdapConsumerName());
                } catch (InternalErrorException ex) {
                    log.error("Consumer failed due to {}. Sleeping for {} ms.", ex, sleepTime);
                    Thread.sleep(sleepTime);
                    sleepTime += sleepTime;
                }
                // If there are no messages, sleep for 1 sec and then try it again
                if (messages == null)
                    Thread.sleep(1000);
            } while (messages == null);
            // If new messages exist, resolve them all
            Iterator<AuditMessage> messagesIterator = messages.iterator();
            while (messagesIterator.hasNext()) {
                message = messagesIterator.next();
                messagesIterator.remove();
                // Warning when two consecutive messages are separated by more than 15 ids
                if (lastProcessedIdNumber > 0 && lastProcessedIdNumber < message.getId()) {
                    if ((message.getId() - lastProcessedIdNumber) > 15)
                        log.debug("SKIP FLAG WARNING: lastProcessedIdNumber: " + lastProcessedIdNumber + " - newMessageNumber: " + message.getId() + " = " + (lastProcessedIdNumber - message.getId()));
                }
                lastProcessedIdNumber = message.getId();
                // IMPORTANT STEP2: Resolve next message
                MessageBeans presentBeans = this.resolveMessage(message.getEvent().getMessage(), message.getId());
                this.dispatchEvent(message.getEvent().getMessage(), presentBeans);
            }
            // After all messages has been resolved, test interrupting of thread and if its ok, wait and go for another bulk of messages
            if (Thread.interrupted()) {
                running = false;
            } else {
                saveLastProcessedId();
                Thread.sleep(5000);
            }
        }
    // If ldapc is interrupted
    } catch (InterruptedException e) {
        Date date = new Date();
        log.error("Last message has ID='" + ((message != null) ? message.getId() : 0) + "' and was INTERRUPTED at " + DATE_FORMAT.format(date) + " due to interrupting.");
        running = false;
        Thread.currentThread().interrupt();
    // If some other exception is thrown
    } catch (Exception e) {
        Date date = new Date();
        log.error("Last message has ID='" + ((message != null) ? message.getId() : 0) + "' and was bad PARSED or EXECUTE at " + DATE_FORMAT.format(date) + " due to exception " + e.toString());
        throw new RuntimeException(e);
    } finally {
        saveLastProcessedId();
    }
}
Also used : Perun(cz.metacentrum.perun.core.api.Perun) AuditMessage(cz.metacentrum.perun.core.api.AuditMessage) PerunSession(cz.metacentrum.perun.core.api.PerunSession) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Date(java.util.Date) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) IOException(java.io.IOException)

Example 2 with Perun

use of cz.metacentrum.perun.core.api.Perun in project perun by CESNET.

the class GroupEventProcessor method processMemberInvalidated.

public void processMemberInvalidated(String msg, MessageBeans beans) {
    if (beans.getMember() == null) {
        return;
    }
    List<Group> memberGroups = new ArrayList<Group>();
    Perun perun = ldapcManager.getPerunBl();
    try {
        log.debug("Getting list of groups for member {}", beans.getMember().getId());
        // memberGroups = Rpc.GroupsManager.getAllMemberGroups(ldapcManager.getRpcCaller(), beans.getMember());
        memberGroups = perun.getGroupsManager().getAllMemberGroups(ldapcManager.getPerunSession(), beans.getMember());
        for (Group g : memberGroups) {
            log.debug("Removing invalidated member {} from group {}", beans.getMember(), g);
            perunGroup.removeMemberFromGroup(beans.getMember(), g);
        }
    } catch (MemberNotExistsException e) {
    // IMPORTANT this is not problem, if member not exist, we expected that will be deleted in some message after that, in DB is deleted
    } catch (PrivilegeException e) {
        log.warn("There are no privilegies for getting member's groups", e);
    } catch (NamingException | InternalErrorException e) {
        log.error("Error removing validated member from group", e);
    }
}
Also used : Perun(cz.metacentrum.perun.core.api.Perun) Group(cz.metacentrum.perun.core.api.Group) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) ArrayList(java.util.ArrayList) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) NamingException(org.springframework.ldap.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 3 with Perun

use of cz.metacentrum.perun.core.api.Perun in project perun by CESNET.

the class GroupEventProcessor method processMemberValidated.

public void processMemberValidated(String msg, MessageBeans beans) {
    if (beans.getMember() == null) {
        return;
    }
    List<Group> memberGroups = new ArrayList<Group>();
    Perun perun = ldapcManager.getPerunBl();
    try {
        log.debug("Getting list of groups for member {}", beans.getMember().getId());
        // memberGroups = Rpc.GroupsManager.getAllMemberGroups(ldapcManager.getRpcCaller(), beans.getMember());
        memberGroups = perun.getGroupsManager().getAllGroupsWhereMemberIsActive(ldapcManager.getPerunSession(), beans.getMember());
        for (Group g : memberGroups) {
            log.debug("Adding validated member {} to group {}", beans.getMember(), g);
            perunGroup.addMemberToGroup(beans.getMember(), g);
        }
    } catch (MemberNotExistsException e) {
    // IMPORTANT this is not problem, if member not exist, we expected that will be deleted in some message after that, in DB is deleted
    } catch (PrivilegeException e) {
        log.warn("There are no privileges for getting member's groups", e);
    } catch (NamingException | InternalErrorException e) {
        log.error("Error adding validated member to group", e);
    }
}
Also used : Perun(cz.metacentrum.perun.core.api.Perun) Group(cz.metacentrum.perun.core.api.Group) MemberNotExistsException(cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException) ArrayList(java.util.ArrayList) PrivilegeException(cz.metacentrum.perun.core.api.exceptions.PrivilegeException) NamingException(org.springframework.ldap.NamingException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

Perun (cz.metacentrum.perun.core.api.Perun)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 Group (cz.metacentrum.perun.core.api.Group)2 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)2 PrivilegeException (cz.metacentrum.perun.core.api.exceptions.PrivilegeException)2 ArrayList (java.util.ArrayList)2 NamingException (org.springframework.ldap.NamingException)2 AuditMessage (cz.metacentrum.perun.core.api.AuditMessage)1 PerunSession (cz.metacentrum.perun.core.api.PerunSession)1 IOException (java.io.IOException)1 Date (java.util.Date)1