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);
}
}
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);
}
}
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));
});
});
}
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();
}
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;
}
Aggregations