<commit_before>import logging
import traceback

from celery.task import task
from django.conf import settings
from django.core.files.base import ContentFile
from taskmonitor.decorators import monitor_task
from taskmonitor.utils import lock_task

from .manager import GmailManager, ManagerError, SyncLimitReached
from .models.models import (EmailAccount, EmailMessage, EmailOutboxMessage, EmailTemplateAttachment,
                            EmailOutboxAttachment, EmailAttachment)


logger = logging.getLogger(__name__)


@task(name='synchronize_email_account_scheduler')
def synchronize_email_account_scheduler():
    """
    Start new tasks for every active mailbox to start synchronizing.
    """
    for email_account in EmailAccount.objects.filter(is_authorized=True, is_deleted=False):
        logger.debug('Scheduling sync for %s', email_account.email_address)

        if not email_account.history_id:
            # First synchronize
            # Lock the task to prevent multiple tasks
            locked, status = lock_task('first_synchronize_email_account', email_account.pk)
            if locked:
                logger.debug('Adding task for first sync for %s', email_account.email_address)

                first_synchronize_email_account.apply_async(
                    args=(email_account.pk,),
                    max_retries=1,
                    default_retry_delay=100,
                    kwargs={'status_id': status.pk},
                )
            else:
                logger.debug('Skipping task first sync for %s, already scheduled', email_account.email_address)
        else:
            # Update synchronize
            # Lock the task to prevent multiple tasks
            locked, status = lock_task('synchronize_email_account', email_account.pk)
            if locked:
                logger.debug('Adding task for sync for %s', email_account.email_address)

                synchronize_email_account.apply_async(
                    args=(email_account.pk,),
                    max_retries=1,
                    default_retry_delay=100,
                    kwargs={'status_id': status.pk},
                )
            else:
                logger.debug('Skipping task sync for %s, already scheduled', email_account.email_address)


@task(name='synchronize_email_account', bind=True)
@monitor_task(logger=logger)
def synchronize_email_account(account_id):
    """
    Synchronize task for all email accounts that are connected with gmail api.

    Args:
        account_id (int): id of the EmailAccount
    """
    try:
        email_account = EmailAccount.objects.get(pk=account_id, is_deleted=False)
    except EmailAccount.DoesNotExist:
        logger.warning('EmailAccount no longer exists: %s', account_id)
    else:
        if email_account.is_authorized:
            logger.info('Sync for: %s', email_account)
            manager = GmailManager(email_account)
            try:
                manager.synchronize()
                logger.info('Sync done for: %s', email_account)
            except ManagerError:
                pass
            except Exception:
                logger.exception('No sync for account %s' % email_account)
            finally:
                manager.cleanup()
        else:
            logger.debug('Not syncing, no authorization for: %s', email_account.email_address)<commit_msg>LILY-887: Email sync TaskStatus exception fix<commit_after>import logging
import traceback

from celery.task import task
from django.conf import settings
from django.core.files.base import ContentFile
from taskmonitor.decorators import monitor_task
from taskmonitor.utils import lock_task

from .manager import GmailManager, ManagerError, SyncLimitReached
from .models.models import (EmailAccount, EmailMessage, EmailOutboxMessage, EmailTemplateAttachment,
                            EmailOutboxAttachment, EmailAttachment)


logger = logging.getLogger(__name__)


@task(name='synchronize_email_account_scheduler')
def synchronize_email_account_scheduler():
    """
    Start new tasks for every active mailbox to start synchronizing.
    """
    for email_account in EmailAccount.objects.filter(is_authorized=True, is_deleted=False):
        logger.debug('Scheduling sync for %s', email_account.email_address)

        if not email_account.history_id:
            # First synchronize
            # Lock the task to prevent multiple tasks
            locked, status = lock_task('first_synchronize_email_account', email_account.pk)
            if locked:
                logger.debug('Adding task for first sync for %s', email_account.email_address)

                first_synchronize_email_account.apply_async(
                    args=(email_account.pk,),
                    max_retries=1,
                    default_retry_delay=100,
                    kwargs={'status_id': status.pk},
                )
            else:
                logger.debug('Skipping task first sync for %s, already scheduled', email_account.email_address)
        else:
            logger.debug('Adding task for sync for %s', email_account.email_address)

            synchronize_email_account.apply_async(
                args=(email_account.pk,),
                max_retries=1,
                default_retry_delay=100,
            )


@task(name='synchronize_email_account', bind=True)
@task(logger=logger)
def synchronize_email_account(account_id):
    """
    Synchronize task for all email accounts that are connected with gmail api.

    Args:
        account_id (int): id of the EmailAccount
    """
    try:
        email_account = EmailAccount.objects.get(pk=account_id, is_deleted=False)
    except EmailAccount.DoesNotExist:
        logger.warning('EmailAccount no longer exists: %s', account_id)
    else:
        if email_account.is_authorized:
            logger.info('Sync for: %s', email_account)
            manager = GmailManager(email_account)
            try:
                manager.synchronize()
                logger.info('Sync done for: %s', email_account)
            except ManagerError:
                pass
            except Exception:
                logger.exception('No sync for account %s' % email_account)
            finally:
                manager.cleanup()
        else:
            logger.debug('Not syncing, no authorization for: %s', email_account.email_address)
{'fraction_non_alphanumeric': 0.07641405388372459, 'fraction_numerical': 0.0029935402552386955, 'mean_word_length': 2.4313513513513514, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083026', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1623, 'n_words': 412}