<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import datetime
import sqlalchemy as sa
import structlog

from random import randint

from flask import Blueprint
from flask import current_app
from flask import redirect
from flask import url_for

from relengapi.blueprints.archiver import tables
from relengapi.blueprints.archiver.tasks import TASK_TIME_OUT
from relengapi.blueprints.archiver.tasks import create_and_upload_archive
from relengapi.blueprints.archiver.types import MozharnessArchiveTask
from relengapi.lib import api
from relengapi.lib import badpenny
from relengapi.lib.time import now

bp = Blueprint('archiver', __name__)
logger = structlog.get_logger()

GET_EXPIRES_IN = 300
PENDING_EXPIRES_IN = 60
FINISHED_STATES = ['SUCCESS', 'FAILURE', 'REVOKED']


def delete_tracker(tracker):
    session = current_app.db.session('relengapi')
    logger.info("deleting tracker with id: {}".format(tracker.task_id))
    session.delete(tracker)
    session.commit()


def update_tracker_state(tracker, state):
    session = current_app.db.session('relengapi')
    logger.info("updating tracker with id: {} to state: {}".format(tracker.id, state))
    try:
        tracker.state = state
        session.commit()
    except sa.exc.IntegrityError:
        session.rollback()


@badpenny.periodic_task(seconds=TASK_TIME_OUT)
def cleanup_old_tasks(job_status):
    """delete any tracker task if it is older than the time a task can live for."""
    session = current_app.db.session('relengapi')
    expiry_cutoff = now() - datetime.timedelta(seconds=TASK_TIME_OUT)
    table = tables.ArchiverTask
    for tracker in session.query(table).order_by(table.created_at):
        if tracker.created_at < expiry_cutoff:
            delete_tracker(tracker)
        else:
            break


def renew_tracker_pending_expiry(tracker):
    pending_expires_at = now() + datetime.timedelta(seconds=PENDING_EXPIRES_IN)
    session = current_app.db.session('relengapi')
    log.info("renewing tracker {} with pending expiry: {}".format(tracker.id, pending_expires_at))
    tracker.pending_expires_at = pending_expires_at
    session.commit()


@bp.route('/status/<task_id>')
@api.apimethod(MozharnessArchiveTask, unicode)
def task_status(task_id):
    """
    Check and return the current state of the create_and_upload_archive celery task with task id
    of <task_id>.<commit_msg>fix up archiver logging after rebase<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import datetime
import sqlalchemy as sa
import structlog

from random import randint

from flask import Blueprint
from flask import current_app
from flask import redirect
from flask import url_for

from relengapi.blueprints.archiver import tables
from relengapi.blueprints.archiver.tasks import TASK_TIME_OUT
from relengapi.blueprints.archiver.tasks import create_and_upload_archive
from relengapi.blueprints.archiver.types import MozharnessArchiveTask
from relengapi.lib import api
from relengapi.lib import badpenny
from relengapi.lib.time import now

bp = Blueprint('archiver', __name__)
logger = structlog.get_logger()

GET_EXPIRES_IN = 300
PENDING_EXPIRES_IN = 60
FINISHED_STATES = ['SUCCESS', 'FAILURE', 'REVOKED']


def delete_tracker(tracker):
    session = current_app.db.session('relengapi')
    logger.info("deleting tracker with id: {}".format(tracker.task_id),
                archiver_task=tracker.task_id)
    session.delete(tracker)
    session.commit()


def update_tracker_state(tracker, state):
    session = current_app.db.session('relengapi')
    logger.info("updating tracker with id: {} to state: {}".format(tracker.id, state),
                archiver_task=tracker.task_id, archiver_task_state=state)
    try:
        tracker.state = state
        session.commit()
    except sa.exc.IntegrityError:
        session.rollback()


@badpenny.periodic_task(seconds=TASK_TIME_OUT)
def cleanup_old_tasks(job_status):
    """delete any tracker task if it is older than the time a task can live for."""
    session = current_app.db.session('relengapi')
    expiry_cutoff = now() - datetime.timedelta(seconds=TASK_TIME_OUT)
    table = tables.ArchiverTask
    for tracker in session.query(table).order_by(table.created_at):
        if tracker.created_at < expiry_cutoff:
            delete_tracker(tracker)
        else:
            break


def renew_tracker_pending_expiry(tracker):
    pending_expires_at = now() + datetime.timedelta(seconds=PENDING_EXPIRES_IN)
    session = current_app.db.session('relengapi')
    logger.info("renewing tracker {} with pending expiry: {}".format(
                tracker.id, pending_expires_at), archiver_task=tracker.task_id)
    tracker.pending_expires_at = pending_expires_at
    session.commit()


@bp.route('/status/<task_id>')
@api.apimethod(MozharnessArchiveTask, unicode)
def task_status(task_id):
    """
    Check and return the current state of the create_and_upload_archive celery task with task id
    of <task_id>.
{'fraction_non_alphanumeric': 0.09063271898156945, 'fraction_numerical': 0.003420102603078092, 'mean_word_length': 4.338742393509127, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '6070326', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1642, 'n_words': 427}