Search in sources :

Example 1 with DaoContextSnapshot

use of io.lumeer.storage.api.dao.context.DaoContextSnapshot in project engine by Lumeer.

the class CronTaskProcessor method processRules.

private void processRules(final DaoContextSnapshot dao, final Collection collection) {
    var rules = collection.getRules().entrySet().stream().filter(e -> e.getValue().getType() == Rule.RuleType.CRON).collect(Collectors.toList());
    if (rules.size() > 0) {
        final ContextualTaskFactory taskFactory = getTaskFactory(dao);
        final String signature = UUID.randomUUID().toString();
        final ZonedDateTime now = ZonedDateTime.now();
        final Map<String, CronRule> rulesToExecute = new HashMap<>();
        rules.forEach(entry -> {
            final CronRule rule = new CronRule(entry.getValue());
            if (checker.shouldExecute(rule, ZonedDateTime.now())) {
                // this is a sign of an error in previous execution, let's revert normal state and let it pass to another round
                if (rule.getExecuting() != null && !"".equals(rule.getExecuting())) {
                    log.info(String.format("Fixing rule execution signature on %s/%s, %s, '%s'.", dao.getOrganization().getCode(), dao.getProject().getCode(), collection.getName(), rule.getRule().getName()));
                    rule.setExecuting(null);
                } else {
                    log.info(String.format("Planning to run rule on %s/%s, %s, '%s'.", dao.getOrganization().getCode(), dao.getProject().getCode(), collection.getName(), rule.getRule().getName()));
                    rule.setLastRun(now);
                    rule.setExecuting(signature);
                    if (rule.getExecutionsLeft() != null) {
                        rule.setExecutionsLeft(Math.max(rule.getExecutionsLeft() - 1, 0));
                    }
                    rulesToExecute.put(entry.getKey(), rule);
                }
            }
        });
        // bookedCollection is the previous version of collection before updating the rules!!!
        final Collection bookedCollection = dao.getCollectionDao().updateCollectionRules(collection);
        rulesToExecute.forEach((key, rule) -> {
            log.info(String.format("Running cron rule on %s/%s, %s, '%s'.", dao.getOrganization().getCode(), dao.getProject().getCode(), collection.getName(), rule.getRule().getName()));
            // is it us who tried last?
            final String executing = new CronRule(bookedCollection.getRules().get(key)).getExecuting();
            if (executing == null || "".equals(executing)) {
                final List<Document> documents = getDocuments(rule, collection, dao);
                taskExecutor.submitTask(getTask(taskFactory, rule.getRule().getName() != null ? rule.getRule().getName() : key, rule.getRule(), collection, documents));
            }
        });
        // Make sure we have the rules with updated lastRun and simply clean the signatures no matter what (lastRun is updated anyway)
        final Collection latestCollection = dao.getCollectionDao().getCollectionById(collection.getId());
        rulesToExecute.keySet().forEach(key -> new CronRule(latestCollection.getRules().get(key)).setExecuting(null));
        dao.getCollectionDao().updateCollectionRules(latestCollection);
    }
}
Also used : WorkspaceContext(io.lumeer.core.WorkspaceContext) DaoContextSnapshot(io.lumeer.storage.api.dao.context.DaoContextSnapshot) CronTaskChecker(io.lumeer.core.util.CronTaskChecker) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) ZonedDateTime(java.time.ZonedDateTime) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) HashMap(java.util.HashMap) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) io.lumeer.api.model(io.lumeer.api.model) DocumentUtils(io.lumeer.core.util.DocumentUtils) Inject(javax.inject.Inject) OrganizationDao(io.lumeer.storage.api.dao.OrganizationDao) List(java.util.List) Singleton(javax.ejb.Singleton) Map(java.util.Map) Schedule(javax.ejb.Schedule) DataStorage(io.lumeer.engine.api.data.DataStorage) Startup(javax.ejb.Startup) CronRule(io.lumeer.api.model.rule.CronRule) ZonedDateTime(java.time.ZonedDateTime) HashMap(java.util.HashMap) CronRule(io.lumeer.api.model.rule.CronRule)

Example 2 with DaoContextSnapshot

use of io.lumeer.storage.api.dao.context.DaoContextSnapshot in project engine by Lumeer.

the class DailyTaskProcessor method process.

// every day at 4:03 am
@Schedule(hour = "4", minute = "3")
public void process() {
    final List<Organization> organizations = organizationDao.getAllOrganizations();
    final LumeerS3Client lumeerS3Client = new LumeerS3Client(configurationProducer);
    final FileAttachmentAdapter fileAttachmentAdapter = new FileAttachmentAdapter(lumeerS3Client, fileAttachmentDao, configurationProducer.getEnvironment().name());
    final List<FileAttachment> attachmentsToDelete = new ArrayList<>();
    log.info(String.format("Running for %d organizations.", organizations.size()));
    organizations.forEach(organization -> {
        final DataStorage userDataStorage = getDataStorage(organization.getId());
        var limits = paymentFacade.getCurrentServiceLimits(organization);
        var cleanOlderThan = ZonedDateTime.now().minusDays(limits.getAuditDays());
        final DaoContextSnapshot orgDao = getDaoContextSnapshot(userDataStorage, new Workspace(organization, null));
        final List<Project> projects = orgDao.getProjectDao().getAllProjects();
        projects.forEach(project -> {
            final DaoContextSnapshot projDao = getDaoContextSnapshot(userDataStorage, new Workspace(organization, project));
            List<AuditRecord> deletedAuditRecords = projDao.getAuditDao().findAuditRecords(cleanOlderThan, AuditType.Deleted);
            final List<FileAttachment> projectAttachmentsToDelete = new ArrayList<>();
            Set<String> documentIds = deletedAuditRecords.stream().filter(record -> ResourceType.DOCUMENT.equals(record.getResourceType())).map(AuditRecord::getResourceId).collect(Collectors.toSet());
            projectAttachmentsToDelete.addAll(fileAttachmentAdapter.getAllFileAttachments(organization, project, documentIds, FileAttachment.AttachmentType.DOCUMENT));
            Set<String> linkIds = deletedAuditRecords.stream().filter(record -> ResourceType.LINK.equals(record.getResourceType())).map(AuditRecord::getResourceId).collect(Collectors.toSet());
            projectAttachmentsToDelete.addAll(fileAttachmentAdapter.getAllFileAttachments(organization, project, linkIds, FileAttachment.AttachmentType.LINK));
            if (projectAttachmentsToDelete.size() > 0) {
                log.info(String.format("Will remove %d attachments on %s/%s.", projectAttachmentsToDelete.size(), organization.getCode(), organization.getCode()));
            }
            attachmentsToDelete.addAll(projectAttachmentsToDelete);
            projDao.getAuditDao().cleanAuditRecords(cleanOlderThan);
        });
    });
    if (attachmentsToDelete.size() > 0) {
        log.info(String.format("Removing %d attachments.", attachmentsToDelete.size()));
        fileAttachmentAdapter.removeFileAttachments(attachmentsToDelete);
    }
}
Also used : DataStorage(io.lumeer.engine.api.data.DataStorage) Organization(io.lumeer.api.model.Organization) FileAttachment(io.lumeer.api.model.FileAttachment) FileAttachmentAdapter(io.lumeer.core.adapter.FileAttachmentAdapter) ArrayList(java.util.ArrayList) Project(io.lumeer.api.model.Project) LumeerS3Client(io.lumeer.core.util.LumeerS3Client) AuditRecord(io.lumeer.api.model.AuditRecord) DaoContextSnapshot(io.lumeer.storage.api.dao.context.DaoContextSnapshot) Schedule(javax.ejb.Schedule)

Example 3 with DaoContextSnapshot

use of io.lumeer.storage.api.dao.context.DaoContextSnapshot in project engine by Lumeer.

the class CronTaskProcessor method process.

// every 15 minutes
@Schedule(hour = "*", minute = "*/15")
public void process() {
    final List<Organization> organizations = organizationDao.getAllOrganizations();
    organizations.forEach(organization -> {
        final DataStorage userDataStorage = getDataStorage(organization.getId());
        final DaoContextSnapshot orgDao = getDaoContextSnapshot(userDataStorage, new Workspace(organization, null));
        final List<Project> projects = orgDao.getProjectDao().getAllProjects();
        projects.forEach(project -> {
            final DaoContextSnapshot projDao = getDaoContextSnapshot(userDataStorage, new Workspace(organization, project));
            final List<Collection> collections = projDao.getCollectionDao().getAllCollections();
            collections.forEach(collection -> processRules(projDao, collection));
        });
    });
}
Also used : DataStorage(io.lumeer.engine.api.data.DataStorage) DaoContextSnapshot(io.lumeer.storage.api.dao.context.DaoContextSnapshot) Schedule(javax.ejb.Schedule)

Example 4 with DaoContextSnapshot

use of io.lumeer.storage.api.dao.context.DaoContextSnapshot in project engine by Lumeer.

the class CronTaskProcessor method getDocuments.

private List<Document> getDocuments(final CronRule rule, final Collection collection, final DaoContextSnapshot dao) {
    if (dao.getSelectedWorkspace().getOrganization().isPresent() && rule.getViewId() != null) {
        try {
            final View view = dao.getViewDao().getViewById(rule.getViewId());
            final User user = AuthenticatedUser.getMachineUser();
            final AllowedPermissions allowedPermissions = AllowedPermissions.allAllowed();
            final List<Document> documents = DocumentUtils.getDocuments(dao, view.getQuery(), user, rule.getLanguage(), allowedPermissions, null);
            return documents.stream().filter(document -> document.getCollectionId().equals(collection.getId())).collect(Collectors.toList());
        } catch (ResourceNotFoundException e) {
            return List.of();
        }
    }
    return List.of();
}
Also used : WorkspaceContext(io.lumeer.core.WorkspaceContext) DaoContextSnapshot(io.lumeer.storage.api.dao.context.DaoContextSnapshot) CronTaskChecker(io.lumeer.core.util.CronTaskChecker) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) ZonedDateTime(java.time.ZonedDateTime) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) HashMap(java.util.HashMap) UUID(java.util.UUID) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) io.lumeer.api.model(io.lumeer.api.model) DocumentUtils(io.lumeer.core.util.DocumentUtils) Inject(javax.inject.Inject) OrganizationDao(io.lumeer.storage.api.dao.OrganizationDao) List(java.util.List) Singleton(javax.ejb.Singleton) Map(java.util.Map) Schedule(javax.ejb.Schedule) DataStorage(io.lumeer.engine.api.data.DataStorage) Startup(javax.ejb.Startup) CronRule(io.lumeer.api.model.rule.CronRule) AuthenticatedUser(io.lumeer.core.auth.AuthenticatedUser) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException)

Example 5 with DaoContextSnapshot

use of io.lumeer.storage.api.dao.context.DaoContextSnapshot in project engine by Lumeer.

the class DelayedActionProcessor method checkActionResourceExistsAndFillData.

private Collection checkActionResourceExistsAndFillData(final DelayedAction action, final User receiver) {
    final String organizationId = action.getData().getString(DelayedAction.DATA_ORGANIZATION_ID);
    final String projectId = action.getData().getString(DelayedAction.DATA_PROJECT_ID);
    final String collectionId = action.getData().getString(DelayedAction.DATA_COLLECTION_ID);
    final String documentId = action.getData().getString(DelayedAction.DATA_DOCUMENT_ID);
    try {
        if (organizationId != null) {
            final DataStorage userDataStorage = getDataStorage(organizationId);
            final Organization organization = organizations.computeIfAbsent(organizationId, id -> organizationDao.getOrganizationById(organizationId));
            action.getData().append(DelayedAction.DATA_ORGANIZATION_NAME, organization.getName());
            action.getData().append(DelayedAction.DATA_ORGANIZATION_CODE, organization.getCode());
            action.getData().append(DelayedAction.DATA_ORGANIZATION_ICON, organization.getIcon());
            action.getData().append(DelayedAction.DATA_ORGANIZATION_COLOR, organization.getColor());
            final DaoContextSnapshot organizationDaoSnapshot = organizationDaoSnapshots.computeIfAbsent(organizationId, id -> getDaoContextSnapshot(userDataStorage, new Workspace(organization, null)));
            if (projectId != null) {
                final Project project = projects.computeIfAbsent(projectId, id -> organizationDaoSnapshot.getProjectDao().getProjectById(projectId));
                action.getData().append(DelayedAction.DATA_PROJECT_NAME, project.getName());
                action.getData().append(DelayedAction.DATA_PROJECT_CODE, project.getCode());
                action.getData().append(DelayedAction.DATA_PROJECT_ICON, project.getIcon());
                action.getData().append(DelayedAction.DATA_PROJECT_COLOR, project.getColor());
                final String projectKey = organizationId + ":" + projectId;
                final DaoContextSnapshot projectDaoSnapshot = projectDaoSnapshots.computeIfAbsent(projectKey, key -> getDaoContextSnapshot(userDataStorage, new Workspace(organization, project)));
                final PermissionAdapter permissionAdapter = permissionAdapters.computeIfAbsent(projectKey, key -> new PermissionAdapter(projectDaoSnapshot.getUserDao(), projectDaoSnapshot.getGroupDao(), projectDaoSnapshot.getViewDao(), projectDaoSnapshot.getLinkTypeDao(), projectDaoSnapshot.getCollectionDao()));
                if (receiver != null && !permissionAdapter.canReadWorkspace(organization, project, receiver.getId())) {
                    return null;
                }
                if (collectionId != null) {
                    final Collection collection = collections.computeIfAbsent(collectionId, id -> projectDaoSnapshot.getCollectionDao().getCollectionById(collectionId));
                    action.getData().append(DelayedAction.DATA_COLLECTION_NAME, collection.getName());
                    action.getData().append(DelayedAction.DATA_COLLECTION_ICON, collection.getIcon());
                    action.getData().append(DelayedAction.DATA_COLLECTION_COLOR, collection.getColor());
                    if (documentId != null) {
                        final Document document = projectDaoSnapshot.getDocumentDao().getDocumentById(documentId);
                        document.setData(projectDaoSnapshot.getDataDao().getData(collectionId, documentId));
                        if (receiver != null && !permissionAdapter.canReadDocument(organization, project, document, collection, receiver.getId())) {
                            return null;
                        }
                        return collection;
                    }
                }
            }
        }
    } catch (ResourceNotFoundException e) {
        return null;
    }
    return null;
}
Also used : DataStorage(io.lumeer.engine.api.data.DataStorage) Project(io.lumeer.api.model.Project) Organization(io.lumeer.api.model.Organization) Collection(io.lumeer.api.model.Collection) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) ResourceNotFoundException(io.lumeer.storage.api.exception.ResourceNotFoundException) DaoContextSnapshot(io.lumeer.storage.api.dao.context.DaoContextSnapshot) PermissionAdapter(io.lumeer.core.adapter.PermissionAdapter)

Aggregations

DataStorage (io.lumeer.engine.api.data.DataStorage)5 DaoContextSnapshot (io.lumeer.storage.api.dao.context.DaoContextSnapshot)5 Schedule (javax.ejb.Schedule)4 ResourceNotFoundException (io.lumeer.storage.api.exception.ResourceNotFoundException)3 io.lumeer.api.model (io.lumeer.api.model)2 Organization (io.lumeer.api.model.Organization)2 Project (io.lumeer.api.model.Project)2 CronRule (io.lumeer.api.model.rule.CronRule)2 WorkspaceContext (io.lumeer.core.WorkspaceContext)2 AuthenticatedUser (io.lumeer.core.auth.AuthenticatedUser)2 CronTaskChecker (io.lumeer.core.util.CronTaskChecker)2 DocumentUtils (io.lumeer.core.util.DocumentUtils)2 OrganizationDao (io.lumeer.storage.api.dao.OrganizationDao)2 ZonedDateTime (java.time.ZonedDateTime)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 UUID (java.util.UUID)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2