use of io.lumeer.api.model.rule.CronRule in project engine by Lumeer.
the class CollectionFacade method runRule.
public void runRule(final Collection collection, final String ruleId) {
if (adapter.getDocumentsCountByCollection(collection.getId()) > 2_000) {
throw new UnsuccessfulOperationException("Too many documents in the source collection");
}
final Rule rule = collection.getRules().get(ruleId);
if (rule != null && rule.getType() == Rule.RuleType.AUTO_LINK) {
final AutoLinkRule autoLinkRule = new AutoLinkRule(rule);
final String otherCollectionId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getCollection1() : autoLinkRule.getCollection2();
final String attributeId = autoLinkRule.getCollection1().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
final Attribute attribute = collection.getAttributes().stream().filter(a -> a.getId().equals(attributeId)).findFirst().orElse(null);
final Collection otherCollection = getCollection(otherCollectionId);
final String otherAttributeId = autoLinkRule.getCollection2().equals(collection.getId()) ? autoLinkRule.getAttribute1() : autoLinkRule.getAttribute2();
final Attribute otherAttribute = otherCollection.getAttributes().stream().filter(a -> a.getId().equals(otherAttributeId)).findFirst().orElse(null);
final Map<String, AllowedPermissions> permissions = permissionsChecker.getCollectionsPermissions(List.of(collection, otherCollection));
if (adapter.getDocumentsCountByCollection(otherCollectionId) > 10_000) {
throw new UnsuccessfulOperationException("Too many documents in the target collection");
}
final LinkType linkType = linkTypeDao.getLinkType(autoLinkRule.getLinkType());
final AutoLinkBatchTask task = taskFactory.getInstance(AutoLinkBatchTask.class);
task.setupBatch(autoLinkRule, linkType, collection, attribute, otherCollection, otherAttribute, getCurrentUser(), permissions);
taskExecutor.submitTask(task);
} else if (rule != null && rule.getType() == Rule.RuleType.CRON) {
final CronRule cronRule = new CronRule(rule);
List<Document> documents = new ArrayList<>();
if (cronRule.getViewId() != null) {
try {
final View view = viewDao.getViewById(cronRule.getViewId());
final User user = AuthenticatedUser.getMachineUser();
final AllowedPermissions allowedPermissions = AllowedPermissions.allAllowed();
documents = DocumentUtils.getDocuments(collectionDao, documentDao, dataDao, userDao, groupDao, selectionListDao, getOrganization(), getProject(), view.getQuery(), user, cronRule.getLanguage(), allowedPermissions, null);
documents = documents.stream().filter(document -> document.getCollectionId().equals(collection.getId())).collect(Collectors.toList());
} catch (ResourceNotFoundException ignore) {
}
}
RuleTask task = taskFactory.getInstance(RuleTask.class);
task.setRule(rule.getName(), rule, collection, documents);
taskExecutor.submitTask(task);
}
}
use of io.lumeer.api.model.rule.CronRule 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.api.model.rule.CronRule 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();
}
Aggregations