Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/actions/cancel_all.py

raw
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
6 import logging
7 import os
8 from concurrent import futures
9
10 import requests
11
12 from taskgraph.util.taskcluster import (
13 CONCURRENCY,
14 cancel_task,
15 list_task_group_incomplete_tasks,
16 )
17
18 from .registry import register_callback_action
19
20 logger = logging.getLogger(__name__)
21
22
23 @register_callback_action(
24 title="Cancel All",
25 name="cancel-all",
26 symbol="cAll",
27 description=(
28 "Cancel all running and pending tasks created by the decision task "
29 "this action task is associated with."
30 ),
31 order=400,
32 context=[],
33 )
34 def cancel_all_action(parameters, graph_config, input, task_group_id, task_id):
35 def do_cancel_task(task_id):
36 logger.info(f"Cancelling task {task_id}")
37 try:
38 cancel_task(task_id, use_proxy=True)
39 except requests.HTTPError as e:
40 if e.response.status_code == 409:
41 # A 409 response indicates that this task is past its deadline. It
42 # cannot be cancelled at this time, but it's also not running
43 # anymore, so we can ignore this error.
44 logger.info(
45 f"Task {task_id} is past its deadline and cannot be cancelled."
46 )
47 return
48 raise
49
50 own_task_id = os.environ.get("TASK_ID", "")
51 to_cancel = [
52 t for t in list_task_group_incomplete_tasks(task_group_id) if t != own_task_id
53 ]
54 logger.info(f"Cancelling {len(to_cancel)} tasks")
55 with futures.ThreadPoolExecutor(CONCURRENCY) as e:
56 cancel_futs = [e.submit(do_cancel_task, t) for t in to_cancel]
57 for f in futures.as_completed(cancel_futs):
58 f.result()