<commit_before>"""
Periodic task that updates user data.
"""
from datetime import (
    datetime,
    timedelta,
)
import logging

from celery import group
import pytz

from dashboard.api import (
    calculate_users_to_refresh_in_bulk,
    refresh_user_data,
)
from micromasters.celery import app
from micromasters.locks import (
    Lock,
    release_lock,
)
from micromasters.utils import (
    chunks,
    now_in_utc,
)


log = logging.getLogger(__name__)


PARALLEL_RATE_LIMIT = '10/m'
LOCK_ID = 'batch_update_user_data_lock'


@app.task
def release_batch_update_user_data_lock(*args, token):  # pylint: disable=unused-argument
    """
    Task which releases the lock acquired in batch_update_user_data

    Args:
        token (str): The token used with the lock
    """
    release_lock(LOCK_ID, token.encode())
    log.info("Released batch_update_user_data lock")


# This lock is not safe against repeated executions of the task since there is no logic
# to stop the batch update halfway through and the lock expiration might be shorter than the length of time this
# executes.
@app.task
def batch_update_user_data():
    """
    Create sub tasks to update user data like enrollments,
    certificates and grades from edX platform.
    """
    expiration = now_in_utc() + timedelta(hours=5)
    lock = Lock(LOCK_ID, expiration)
    if not lock.acquire():
        # Lock should have expired by now<commit_msg>Reduce rate of batch_update_user_data (#3702)<commit_after>"""
Periodic task that updates user data.
"""
from datetime import (
    datetime,
    timedelta,
)
import logging

from celery import group
import pytz

from dashboard.api import (
    calculate_users_to_refresh_in_bulk,
    refresh_user_data,
)
from micromasters.celery import app
from micromasters.locks import (
    Lock,
    release_lock,
)
from micromasters.utils import (
    chunks,
    now_in_utc,
)


log = logging.getLogger(__name__)


PARALLEL_RATE_LIMIT = '5/m'
LOCK_ID = 'batch_update_user_data_lock'


@app.task
def release_batch_update_user_data_lock(*args, token):  # pylint: disable=unused-argument
    """
    Task which releases the lock acquired in batch_update_user_data

    Args:
        token (str): The token used with the lock
    """
    release_lock(LOCK_ID, token.encode())
    log.info("Released batch_update_user_data lock")


# This lock is not safe against repeated executions of the task since there is no logic
# to stop the batch update halfway through and the lock expiration might be shorter than the length of time this
# executes.
@app.task
def batch_update_user_data():
    """
    Create sub tasks to update user data like enrollments,
    certificates and grades from edX platform.
    """
    expiration = now_in_utc() + timedelta(hours=5)
    lock = Lock(LOCK_ID, expiration)
    if not lock.acquire():
        # Lock should have expired by now
{'fraction_non_alphanumeric': 0.0749912800837112, 'fraction_numerical': 0.003139169863969306, 'mean_word_length': 3.8527918781725887, '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': '5959294', 'n_tokens_mistral': 903, 'n_tokens_neox': 867, 'n_words': 289}