<commit_before># coding: utf-8
import datetime
import logging
from xmlrpclib import Fault, ProtocolError

from .balaio import BalaioRPC
from .models import Checkin
from scielomanager.celery import app

logger = logging.getLogger(__name__)


@app.task(bind=True)
def do_expires_checkin(self, checkin):
    """
    For this checkin:
    - call do_expires() that change status and create workflow logs.
    - call BalaioRPC.expire_attempt API method to trigger action on Balaio's side.
    if API is down or RPC call raise exception -> task will be retried
    """
    rpc_client = BalaioRPC()
    logger.debug('[do_expires_checkin] START process to expire checkin (pk=%s) at %s' % (checkin.pk, datetime.datetime.now()))
    checkin.do_expires()
    logger.info('[do_expires_checkin] EXPIRED checkin pk: %s' % checkin.pk)
    if rpc_client.is_up():
        try:
            rpc_response = rpc_client.call('expire_attempt', [checkin.attempt_ref, ])
        except (Fault, ProtocolError, ValuerError) as e:
            logger.debug('[do_expires_checkin] FAIL, retrying: exception' % e)
            raise self.retry(exc=e)
        logger.info('[do_expires_checkin] BALAIO RPC method call success!.')
    else:
        raise self.retry(exc=Exception("Balaio is down!"))
        logger.error('[do_expires_checkin] BALAIO RPC is Down. Could not call "expire_attempt" method.')
    logger.debug('[do_expires_checkin] FINISH process at %s' % datetime.datetime.now())


@app.task(bind=True)
def process_expirable_checkins(self):
    """
    RUN: daily!
    For each checkin that is "expirable":
        call the task: do_expires_checkin to change the checkins as expired
    """
    logger.debug('[process_expirable_checkins] START process at %s' % datetime.datetime.now())
    checkins = Checkin.objects.filter(status='pending', expiration_at__isnull=False)
    logger.info('[process_expirable_checkins] Found %s checkins matching conditions' % checkins.count())
    for checkin in checkins:
        logger.info('[process_expirable_checkins] Processing checkin (pk=%s)' % checkin.pk)
        if checkin.is_expirable:
            do_expires_checkin.apply_async(args=[checkin, ])

    logger.debug('[process_expirable_checkins] FINISH process at %s' % datetime.datetime.now())<commit_msg>Ajuste da task de expiração do checkin garantindo, a expiração no balaio e no checkin<commit_after># coding: utf-8
import datetime
import logging
from xmlrpclib import Fault, ProtocolError

from .balaio import BalaioRPC
from .models import Checkin
from scielomanager.celery import app

logger = logging.getLogger(__name__)


@app.task(bind=True)
def do_expires_checkin(self, checkin):
    """
    For this checkin:
    - call do_expires() that change status and create workflow logs.
    - call BalaioRPC.expire_attempt API method to trigger action on Balaio's side.
    if API is down or RPC call raise exception -> task will be retried
    """
    rpc_client = BalaioRPC()
    logger.debug('[do_expires_checkin] START process to expire checkin (pk=%s) at %s' % (checkin.pk, datetime.datetime.now()))
    if rpc_client.is_up():
        checkin.do_expires()
        logger.info('[do_expires_checkin] EXPIRED checkin pk: %s' % checkin.pk)
        try:
            rpc_response = rpc_client.call('expire_attempt', [checkin.attempt_ref, ])
        except (Fault, ProtocolError, ValuerError) as e:
            logger.debug('[do_expires_checkin] FAIL, retrying: exception' % e)
            raise self.retry(exc=e)
        logger.info('[do_expires_checkin] BALAIO RPC method call success!.')
    else:
        logger.error('[do_expires_checkin] BALAIO RPC is Down. Could not call "expire_attempt" method.')
        raise self.retry()
    logger.debug('[do_expires_checkin] FINISH process at %s' % datetime.datetime.now())


@app.task(bind=True)
def process_expirable_checkins(self):
    """
    RUN: daily!
    For each checkin that is "expirable":
        call the task: do_expires_checkin to change the checkins as expired
    """
    logger.debug('[process_expirable_checkins] START process at %s' % datetime.datetime.now())
    checkins = Checkin.objects.filter(status='pending', expiration_at__isnull=False)
    logger.info('[process_expirable_checkins] Found %s checkins matching conditions' % checkins.count())
    for checkin in checkins:
        logger.info('[process_expirable_checkins] Processing checkin (pk=%s)' % checkin.pk)
        if checkin.is_expirable:
            do_expires_checkin.apply_async(args=[checkin, ])

    logger.debug('[process_expirable_checkins] FINISH process at %s' % datetime.datetime.now())
{'fraction_non_alphanumeric': 0.10635058721183123, 'fraction_numerical': 0.00043497172683775554, 'mean_word_length': 3.876988335100742, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5983348', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1469, 'n_words': 383}