<commit_before>"""
This module is for the following use case:

 - Automatic backfilling:

    * If we use pulse_actions to listen to mozilla-inbound finished
      *test* jobs, we should be able to re-trigger a job a couple of
      times (similar to what trigger bot does on try) and backfill
      back to the last known good

    * Notice that trigger bot has conditions on when to stop
      re-triggering more than needed
"""
import logging

from mozci.mozci import trigger_range, trigger_job, find_backfill_revlist, \
    query_repo_url_from_buildername
from mozci.query_jobs import FAILURE, WARNING
from mozci.sources import buildjson
from mozci import query_jobs
from mozci.utils import transfer

LOG = logging.getLogger()

MAX_REVISIONS = 5
# Use memory-saving mode
transfer.MEMORY_SAVING_MODE = True


def on_event(data, message, dry_run):
    """Automatically backfill failed jobs."""
    # Cleaning mozci caches
    buildjson.BUILDS_CACHE = {}
    query_jobs.JOBS_CACHE = {}
    payload = data["payload"]
    status = payload["status"]
    buildername = payload["buildername"]

    # Backfill a failed job
    if status in [FAILURE, WARNING]:
        revision = payload["revision"]
        LOG.info("**")  # visual separator
        LOG.info("Failed job found at revision %s. Buildername: %s",
                 revision, buildername)

        # We want to have 2 jobs for the current revision
        trigger_range(
            buildername=buildername,
            revision=[revision],
            times=2,
            dry_run=dry_run)

        # We want to assure 1 apperance of each job on the past revisions<commit_msg>Bug 1194213: Retrigger job on same push besides automatic backfilling<commit_after>"""
This module is for the following use case:

 - Automatic backfilling:

    * If we use pulse_actions to listen to mozilla-inbound finished
      *test* jobs, we should be able to re-trigger a job a couple of
      times (similar to what trigger bot does on try) and backfill
      back to the last known good

    * Notice that trigger bot has conditions on when to stop
      re-triggering more than needed
"""
import logging

from mozci.mozci import trigger_range, trigger_job, find_backfill_revlist, \
    query_repo_url_from_buildername
from mozci.query_jobs import FAILURE, WARNING
from mozci.sources import buildjson
from mozci import query_jobs
from mozci.utils import transfer

LOG = logging.getLogger()

MAX_REVISIONS = 5
# Use memory-saving mode
transfer.MEMORY_SAVING_MODE = True


def on_event(data, message, dry_run):
    """Automatically backfill failed jobs."""
    # Cleaning mozci caches
    buildjson.BUILDS_CACHE = {}
    query_jobs.JOBS_CACHE = {}
    payload = data["payload"]
    status = payload["status"]
    buildername = payload["buildername"]

    # Backfill a failed job
    if status in [FAILURE, WARNING]:
        revision = payload["revision"]
        LOG.info("**")  # visual separator
        LOG.info("Failed job found at revision %s. Buildername: %s",
                 revision, buildername)

        # We want to have 2 jobs for the current revision
        trigger_range(
            buildername=buildername,
            revisions=[revision],
            times=2,
            dry_run=dry_run)

        # We want to assure 1 apperance of each job on the past revisions
{'fraction_non_alphanumeric': 0.0712781954887218, 'fraction_numerical': 0.004511278195488722, 'mean_word_length': 3.0364077669902914, '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': '5200773', 'n_tokens_mistral': 983, 'n_tokens_neox': 931, 'n_words': 348}