use of alfio.model.EmailMessage.Status in project alf.io by alfio-event.
the class NotificationManager method processMessage.
private int processMessage(EmailMessage message, PurchaseContext purchaseContext) {
int messageId = message.getId();
ConfigurationLevel configurationLevel = ConfigurationLevel.purchaseContext(purchaseContext);
if (message.getAttempts() >= configurationManager.getFor(ConfigurationKeys.MAIL_ATTEMPTS_COUNT, configurationLevel).getValueAsIntOrDefault(10)) {
tx.execute(status -> emailMessageRepository.updateStatusAndAttempts(messageId, ERROR.name(), message.getAttempts(), Arrays.asList(IN_PROCESS.name(), WAITING.name(), RETRY.name())));
log.warn("Message with id {} will be discarded", messageId);
return 0;
}
try {
int result = Optional.ofNullable(tx.execute(status -> emailMessageRepository.updateStatus(messageId, message.getChecksum(), IN_PROCESS.name(), Arrays.asList(WAITING.name(), RETRY.name())))).orElse(0);
if (result > 0) {
return Optional.ofNullable(tx.execute(status -> {
sendMessage(purchaseContext, message);
return 1;
})).orElse(0);
} else {
log.debug("no messages have been updated on DB for the following criteria: id: {}, checksum: {}", messageId, message.getChecksum());
}
} catch (Exception e) {
tx.execute(status -> emailMessageRepository.updateStatusAndAttempts(message.getId(), RETRY.name(), ZonedDateTime.now(clockProvider.getClock()).plusMinutes(message.getAttempts() + 1L), message.getAttempts() + 1, Arrays.asList(IN_PROCESS.name(), WAITING.name(), RETRY.name())));
log.warn("could not send message: ", e);
}
return 0;
}
Aggregations