DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b3f5385fa0b3)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
# -*- coding: utf-8 -*-

# 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/.

from __future__ import absolute_import, print_function, unicode_literals

import sys

import logging
import textwrap

from slugid import nice as slugid
from .util import (
    combine_task_graph_files,
    create_tasks,
    fetch_graph_and_labels,
    relativize_datestamps,
    create_task_from_def,
)
from .registry import register_callback_action
from taskgraph.util import taskcluster

logger = logging.getLogger(__name__)

RERUN_STATES = ('exception', 'failed')


@register_callback_action(
    title='Retrigger',
    name='retrigger',
    symbol='rt',
    cb_name='retrigger-decision',
    description=textwrap.dedent('''\
        Create a clone of the task (retriggering decision, action, and cron tasks requires
        special scopes).'''),
    order=11,
    context=[
        {'kind': 'decision-task'},
        {'kind': 'action-callback'},
        {'kind': 'cron-task'},
    ],
)
def retrigger_decision_action(parameters, graph_config, input, task_group_id, task_id):
    """For a single task, we try to just run exactly the same task once more.
    It's quite possible that we don't have the scopes to do so (especially for
    an action), but this is best-effort."""

    # make all of the timestamps relative; they will then be turned back into
    # absolute timestamps relative to the current time.
    task = taskcluster.get_task_definition(task_id)
    task = relativize_datestamps(task)
    create_task_from_def(slugid(), task, parameters['level'])


@register_callback_action(
    title='Retrigger',
    name='retrigger',
    symbol='rt',
    generic=True,
    description=(
        'Create a clone of the task.'
    ),
    order=19,  # must be greater than other orders in this file, as this is the fallback version
    context=[{'retrigger': 'true'}],
    schema={
        'type': 'object',
        'properties': {
            'downstream': {
                'type': 'boolean',
                'description': (
                    'If true, downstream tasks from this one will be cloned as well. '
                    'The dependencies will be updated to work with the new task at the root.'
                ),
                'default': False,
            },
            'times': {
                'type': 'integer',
                'default': 1,
                'minimum': 1,
                'maximum': 100,
                'title': 'Times',
                'description': 'How many times to run each task.',
            }
        }
    }
)
@register_callback_action(
    title='Retrigger (disabled)',
    name='retrigger',
    cb_name='retrigger-disabled',
    symbol='rt',
    generic=True,
    description=(
        'Create a clone of the task.\n\n'
        'This type of task should typically be re-run instead of re-triggered.'
    ),
    order=20,  # must be greater than other orders in this file, as this is the fallback version
    context=[{}],
    schema={
        'type': 'object',
        'properties': {
            'downstream': {
                'type': 'boolean',
                'description': (
                    'If true, downstream tasks from this one will be cloned as well. '
                    'The dependencies will be updated to work with the new task at the root.'
                ),
                'default': False,
            },
            'times': {
                'type': 'integer',
                'default': 1,
                'minimum': 1,
                'maximum': 100,
                'title': 'Times',
                'description': 'How many times to run each task.',
            },
            'force': {
                'type': 'boolean',
                'default': False,
                'description': (
                    'This task should not be re-triggered. '
                    'This can be overridden by passing `true` here.'
                ),
            },
        }
    }
)
def retrigger_action(parameters, graph_config, input, task_group_id, task_id):
    decision_task_id, full_task_graph, label_to_taskid = fetch_graph_and_labels(
        parameters, graph_config)

    task = taskcluster.get_task_definition(task_id)
    label = task['metadata']['name']

    with_downstream = ' '
    to_run = [label]

    if not input.get('force', None) and not full_task_graph[label].attributes.get('retrigger'):
        logger.info(
            "Not retriggering task {}, task should not be retrigged "
            "and force not specified.".format(
                label
            )
        )
        sys.exit(1)

    if input.get('downstream'):
        to_run = full_task_graph.graph.transitive_closure(set(to_run), reverse=True).nodes
        to_run = to_run & set(label_to_taskid.keys())
        with_downstream = ' (with downstream) '

    times = input.get('times', 1)
    for i in xrange(times):
        create_tasks(
            graph_config,
            to_run,
            full_task_graph,
            label_to_taskid,
            parameters,
            decision_task_id,
            i,
        )

        logger.info('Scheduled {}{}(time {}/{})'.format(label, with_downstream, i+1, times))
    combine_task_graph_files(list(range(times)))


@register_callback_action(
    title='Rerun',
    name='rerun',
    generic=True,
    symbol='rr',
    description=(
        'Rerun a task.\n\n'
        'This only works on failed or exception tasks in the original taskgraph,'
        ' and is CoT friendly.'
    ),
    order=300,
    context=[{}],
    schema={
        'type': 'object',
        'properties': {}
    }
)
def rerun_action(parameters, graph_config, input, task_group_id, task_id):
    task = taskcluster.get_task_definition(task_id)
    parameters = dict(parameters)
    decision_task_id, full_task_graph, label_to_taskid = fetch_graph_and_labels(
        parameters, graph_config)
    label = task['metadata']['name']
    if task_id not in label_to_taskid.values():
        logger.error(
            "Refusing to rerun {}: taskId {} not in decision task {} label_to_taskid!".format(
                label, task_id, decision_task_id
            )
        )

    _rerun_task(task_id, label)


def _rerun_task(task_id, label):
    status = taskcluster.status_task(task_id)
    if status not in RERUN_STATES:
        logger.warning(
            "No need to rerun {}: state '{}' not in {}!".format(label, status, RERUN_STATES)
        )
        return
    taskcluster.rerun_task(task_id)
    logger.info('Reran {}'.format(label))


@register_callback_action(
    title='Retrigger',
    name='retrigger-multiple',
    symbol='rt',
    generic=True,
    description=(
        'Create a clone of the task.'
    ),
    context=[],
    schema={
        "type": "object",
        "properties": {
            "requests": {
                "type": "array",
                "items": {
                    "tasks": {
                        "type": "array",
                        'description': 'An array of task labels',
                        'items': {
                            'type': 'string'
                        }
                    },
                    "times": {
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 100,
                        "title": "Times",
                        "description": "How many times to run each task.",
                    },
                    "additionalProperties": False,
                },
            },
            "additionalProperties": False,
        },
    },
)
def retrigger_multiple(parameters, graph_config, input, task_group_id, task_id):
    decision_task_id, full_task_graph, label_to_taskid = fetch_graph_and_labels(
        parameters, graph_config)

    suffixes = []
    for i, request in enumerate(input.get('requests', [])):
        times = request.get('times', 1)
        rerun_tasks = [
            label for label in request.get('tasks')
            if not full_task_graph[label].attributes.get('retrigger')]
        retrigger_tasks = [
            label for label in request.get('tasks')
            if full_task_graph[label].attributes.get('retrigger')
        ]

        for label in rerun_tasks:
            # XXX we should not re-run tasks pulled in from other pushes
            # In practice, this shouldn't matter, as only completed tasks
            # are pulled in from other pushes and treeherder won't pass
            # those labels.
            _rerun_task(label_to_taskid[label], label)

        for j in xrange(times):
            suffix = '{}-{}'.format(i, j)
            suffixes.append(suffix)
            create_tasks(
                graph_config,
                retrigger_tasks,
                full_task_graph,
                label_to_taskid,
                parameters,
                decision_task_id,
                suffix,
            )

    combine_task_graph_files(suffixes)