Search in sources :

Example 1 with Status

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;
}
Also used : alfio.repository(alfio.repository) TemplateProcessor(alfio.controller.support.TemplateProcessor) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) Autowired(org.springframework.beans.factory.annotation.Autowired) PartialTicketTextGenerator(alfio.manager.support.PartialTicketTextGenerator) Mailer(alfio.manager.system.Mailer) StringUtils(org.apache.commons.lang3.StringUtils) TicketCheckInUtil(alfio.util.checkin.TicketCheckInUtil) Pair(org.apache.commons.lang3.tuple.Pair) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) SubscriptionDescriptor(alfio.model.subscription.SubscriptionDescriptor) OrganizationRepository(alfio.repository.user.OrganizationRepository) Organization(alfio.model.user.Organization) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) Type(java.lang.reflect.Type) Hex(org.springframework.security.crypto.codec.Hex) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TemplateGenerator(alfio.manager.support.TemplateGenerator) java.util(java.util) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageSourceManager(alfio.manager.i18n.MessageSourceManager) TransactionDefinition(org.springframework.transaction.TransactionDefinition) MessageDigest(java.security.MessageDigest) PurchaseContextType(alfio.model.PurchaseContext.PurchaseContextType) ConfigurationManager(alfio.manager.system.ConfigurationManager) Function(java.util.function.Function) Supplier(java.util.function.Supplier) alfio.util(alfio.util) Status(alfio.model.EmailMessage.Status) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) Objects.requireNonNullElseGet(java.util.Objects.requireNonNullElseGet) MessageSource(org.springframework.context.MessageSource) INCLUDE_CHECK_IN_URL_ICAL(alfio.model.system.ConfigurationKeys.INCLUDE_CHECK_IN_URL_ICAL) IOException(java.io.IOException) CustomMessageManager(alfio.manager.support.CustomMessageManager) Component(org.springframework.stereotype.Component) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) ConfigurationKeys(alfio.model.system.ConfigurationKeys) com.google.gson(com.google.gson) Transactional(org.springframework.transaction.annotation.Transactional) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Aggregations

TemplateProcessor (alfio.controller.support.TemplateProcessor)1 MessageSourceManager (alfio.manager.i18n.MessageSourceManager)1 CustomMessageManager (alfio.manager.support.CustomMessageManager)1 PartialTicketTextGenerator (alfio.manager.support.PartialTicketTextGenerator)1 TemplateGenerator (alfio.manager.support.TemplateGenerator)1 ConfigurationLevel (alfio.manager.system.ConfigurationLevel)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 Mailer (alfio.manager.system.Mailer)1 alfio.model (alfio.model)1 Status (alfio.model.EmailMessage.Status)1 PurchaseContextType (alfio.model.PurchaseContext.PurchaseContextType)1 SubscriptionDescriptor (alfio.model.subscription.SubscriptionDescriptor)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 INCLUDE_CHECK_IN_URL_ICAL (alfio.model.system.ConfigurationKeys.INCLUDE_CHECK_IN_URL_ICAL)1 Organization (alfio.model.user.Organization)1 alfio.repository (alfio.repository)1 OrganizationRepository (alfio.repository.user.OrganizationRepository)1 alfio.util (alfio.util)1 TicketCheckInUtil (alfio.util.checkin.TicketCheckInUtil)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1