taskcluster/taskgraph/actions/cancel_all.py
author Dustin J. Mitchell <dustin@mozilla.com>
Fri, 04 Jan 2019 21:19:39 +0000
changeset 514005 96a7386c6e5bda914f59fd651b01ec7c03b644d9
parent 512047 7433ce13608181f6e2738127c6fd0ab572109526
child 514006 53380666b5c70c68da513dc8ba0c8ff7dfca48c5
permissions -rw-r--r--
Bug 1517645 - log each task as it is cancelled Reviewers: bstack Subscribers: tomprince Tags: #secure-revision Bug #: 1517645 Differential Revision: https://phabricator.services.mozilla.com/D15852

# -*- 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 concurrent.futures as futures
import logging
import os

from taskgraph.util.taskcluster import list_task_group, cancel_task
from .registry import register_callback_action

# the maximum number of parallel cancelTask calls to make
CONCURRENCY = 50

logger = logging.getLogger(__name__)


@register_callback_action(
    title='Cancel All',
    name='cancel-all',
    kind='hook',
    generic=True,
    symbol='cAll',
    description=(
        'Cancel all running and pending tasks created by the decision task '
        'this action task is associated with.'
    ),
    order=400,
    context=[]
)
def cancel_all_action(parameters, graph_config, input, task_group_id, task_id, task):
    def do_cancel_task(task_id):
        logger.info('Cancelling task {}'.format(task_id))
        cancel_task(task_id, use_proxy=True)

    own_task_id = os.environ.get('TASK_ID', '')
    with futures.ThreadPoolExecutor(CONCURRENCY) as e:
        cancels_jobs = [
            e.submit(do_cancel_task, t)
            for t in list_task_group(task_group_id) if t != own_task_id
        ]
        for job in cancels_jobs:
            job.result()