예제 #1
0
    def handle(self, *args, **options):
        if not settings.EMAIL_BATCH_SENDING:
            raise CommandError('Batch sending is turned off')

        lock = FileLock(settings.EMAIL_BATCH_LOCK_FILE)
        try:
            self.stdout.write('acquiring lock...')
            lock.acquire(settings.EMAIL_BATCH_LOCK_WAIT_TIMEOUT)
        except AlreadyLocked:
            raise CommandError('lock already in place. quitting.')
        except LockTimeout:
            raise CommandError('waiting for the lock timed out. quitting.')

        email_sender = get_email_sender()

        sent = 0
        try:
            messages_to_send = EmailMessage.objects.filter(
                Q(state=EmailMessage.STATE.WAITING)
                | Q(state=EmailMessage.STATE.ERROR,
                    number_of_send_attempts__lte=settings.
                    EMAIL_BATCH_MAX_NUMBER_OF_SEND_ATTEMPTS,
                    created_at__gte=now() -
                    timedelta(seconds=settings.EMAIL_BATCH_MAX_SECONDS_TO_SEND)
                    )).order_by('created_at')[:settings.EMAIL_BATCH_SIZE]
            for message in messages_to_send:
                email_sender.publish_message(message)
                sent += 1
            self.stdout.write('{} e-mails sent'.format(sent))
        finally:
            self.stdout.write('releasing lock...')
            lock.release()
            self.stdout.write('released.')
예제 #2
0
def send(sender,
         recipient,
         subject,
         content,
         sender_name=None,
         related_objects=None,
         attachments=None,
         tag=None,
         **email_kwargs):
    """
    Helper for sending e-mail message.
    :param sender: e-mail address of the sender
    :param recipient: e-mail address of the receiver
    :param subject: subject of the e-mail message
    :param content: content of the e-mail message
    :param sender_name: friendly name of the sender
    :param related_objects: list of related objects that will be linked with the e-mail message with generic
        relation
    :param tag: string mark that will be saved with the message
    :param attachments: list of files that will be sent with the message as attachments
    :param email_kwargs: extra data that will be saved in JSON format in the extra_data model field
    :return: True if e-mail was successfully sent or False if e-mail is in error state
    """
    return get_email_sender().send(sender=sender,
                                   recipient=recipient,
                                   subject=subject,
                                   content=content,
                                   sender_name=sender_name,
                                   related_objects=related_objects,
                                   tag=tag,
                                   attachments=attachments,
                                   **email_kwargs).failed
예제 #3
0
    def handle(self, *args, **options):
        email_sender = get_email_sender()

        delay = datetime.timedelta(
            seconds=settings.EMAIL_PULL_INFO_DELAY_SECONDS)
        messages = EmailMessage.objects.filter(
            # start_time = last_webhook_received_at + delay
            # info_changed_at < start_time
            # info_changed_at < last_webhook_received_at + delay
            Q(info_changed_at__isnull=True)
            | Q(info_changed_at__lt=F('last_webhook_received_at') + delay),
            last_webhook_received_at__lt=now() - delay,
            sent_at__gt=now() - datetime.timedelta(
                seconds=settings.EMAIL_PULL_INFO_MAX_TIMEOUT_FROM_SENT_SECONDS)
        ).order_by('-sent_at')
        self.stdout.write('Total number of emails to pull info: {}'.format(
            messages.count()))

        messages = messages[:settings.EMAIL_PULL_INFO_BATCH_SIZE]
        self.stdout.write('Number of emails in this batch: {}'.format(
            messages.count()))

        for message in messages:
            try:
                email_sender.pull_message_info(message)
            except Exception as ex:
                # Rollback should not be applied for already updated e-mails
                logger.exception(ex)
        self.stdout.write('DONE')
    def handle(self, *args, **options):
        if not settings.EMAIL_BATCH_SENDING:
            raise CommandError('Batch sending is turned off')

        email_sender = get_email_sender()

        sent = 0
        try:
            messages_to_send = EmailMessage.objects.filter(
                Q(state=EmailMessage.STATE.WAITING)
                | Q(state=EmailMessage.STATE.ERROR,
                    number_of_send_attempts__lte=settings.
                    EMAIL_BATCH_MAX_NUMBER_OF_SEND_ATTEMPTS,
                    created_at__gte=now() - timedelta(
                        seconds=settings.EMAIL_BATCH_MAX_SECONDS_TO_SEND))
            ).select_for_update(
                nowait=True).order_by('created_at')[:settings.EMAIL_BATCH_SIZE]
            for message in messages_to_send:
                try:
                    email_sender.publish_message(message)
                    sent += 1
                except Exception as ex:
                    # Rollback shoud not be applied for already send e-mails
                    logger.exception(ex)
            self.stdout.write('{} e-mails sent'.format(sent))
        except DatabaseError:
            self.stdout.write('e-mail messages are already locked')
예제 #5
0
 def send(self, recipient, context_data, related_objects=None, tag=None, attachments=None, **kwargs):
     if self.can_send(recipient, context_data):
         return get_email_sender().send(
             self.sender,
             recipient,
             self.render_subject(context_data),
             self.render_body(context_data),
             sender_name=self.sender_name,
             related_objects=related_objects,
             tag=tag,
             template=self,
             attachments=attachments
         )
     else:
         return None
예제 #6
0
 def get_backend_sender(self):
     return get_email_sender()
예제 #7
0
 def handle(self, *args, **kwargs):
     get_email_sender().send_batch()