| 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 |
import sys |
| 9 |
from concurrent import futures |
| 10 |
|
| 11 |
from slugid import nice as slugid |
| 12 |
|
| 13 |
from taskgraph.util import json |
| 14 |
from taskgraph.util.parameterization import resolve_timestamps |
| 15 |
from taskgraph.util.taskcluster import CONCURRENCY, get_session |
| 16 |
from taskgraph.util.time import current_json_time |
| 17 |
|
| 18 |
logger = logging.getLogger(__name__) |
| 19 |
|
| 20 |
# this is set to true for `mach taskgraph action-callback --test` |
| 21 |
testing = False |
| 22 |
|
| 23 |
|
| 24 |
def create_tasks(graph_config, taskgraph, label_to_taskid, params, decision_task_id): |
| 25 |
taskid_to_label = {t: l for l, t in label_to_taskid.items()} |
| 26 |
|
| 27 |
# when running as an actual decision task, we use the decision task's |
| 28 |
# taskId as the taskGroupId. The process that created the decision task |
| 29 |
# helpfully placed it in this same taskGroup. If there is no $TASK_ID, |
| 30 |
# fall back to a slugid |
| 31 |
scheduler_id = "{}-level-{}".format(graph_config["trust-domain"], params["level"]) |
| 32 |
|
| 33 |
# Add the taskGroupId, schedulerId and optionally the decision task |
| 34 |
# dependency |
| 35 |
for task_id in taskgraph.graph.nodes: |
| 36 |
task_def = taskgraph.tasks[task_id].task |
| 37 |
|
| 38 |
# if this task has no dependencies *within* this taskgraph, make it |
| 39 |
# depend on this decision task. If it has another dependency within |
| 40 |
# the taskgraph, then it already implicitly depends on the decision |
| 41 |
# task. The result is that tasks do not start immediately. if this |
| 42 |
# loop fails halfway through, none of the already-created tasks run. |
| 43 |
if not any(t in taskgraph.tasks for t in task_def.get("dependencies", [])): |
| 44 |
task_def.setdefault("dependencies", []).append(decision_task_id) |
| 45 |
|
| 46 |
task_def["taskGroupId"] = decision_task_id |
| 47 |
task_def["schedulerId"] = scheduler_id |
| 48 |
|
| 49 |
# If `testing` is True, then run without parallelization |
| 50 |
concurrency = CONCURRENCY if not testing else 1 |
| 51 |
session = get_session() |
| 52 |
with futures.ThreadPoolExecutor(concurrency) as e: |
| 53 |
fs = {} |
| 54 |
|
| 55 |
# We can't submit a task until its dependencies have been submitted. |
| 56 |
# So our strategy is to walk the graph and submit tasks once all |
| 57 |
# their dependencies have been submitted. |
| 58 |
tasklist = set(taskgraph.graph.visit_postorder()) |
| 59 |
alltasks = tasklist.copy() |
| 60 |
|
| 61 |
def schedule_tasks(): |
| 62 |
# bail out early if any futures have failed |
| 63 |
if any(f.done() and f.exception() for f in fs.values()): |
| 64 |
return |
| 65 |
|
| 66 |
to_remove = set() |
| 67 |
new = set() |
| 68 |
|
| 69 |
def submit(task_id, label, task_def): |
| 70 |
fut = e.submit(create_task, session, task_id, label, task_def) |
| 71 |
new.add(fut) |
| 72 |
fs[task_id] = fut |
| 73 |
|
| 74 |
for task_id in tasklist: |
| 75 |
task_def = taskgraph.tasks[task_id].task |
| 76 |
# If we haven't finished submitting all our dependencies yet, |
| 77 |
# come back to this later. |
| 78 |
# Some dependencies aren't in our graph, so make sure to filter |
| 79 |
# those out |
| 80 |
deps = set(task_def.get("dependencies", [])) & alltasks |
| 81 |
if any((d not in fs or not fs[d].done()) for d in deps): |
| 82 |
continue |
| 83 |
|
| 84 |
submit(task_id, taskid_to_label[task_id], task_def) |
| 85 |
to_remove.add(task_id) |
| 86 |
|
| 87 |
# Schedule tasks as many times as task_duplicates indicates |
| 88 |
attributes = taskgraph.tasks[task_id].attributes |
| 89 |
for i in range(1, attributes.get("task_duplicates", 1)): |
| 90 |
# We use slugid() since we want a distinct task id |
| 91 |
submit(slugid(), taskid_to_label[task_id], task_def) |
| 92 |
tasklist.difference_update(to_remove) |
| 93 |
|
| 94 |
# as each of those futures complete, try to schedule more tasks |
| 95 |
for f in futures.as_completed(new): |
| 96 |
schedule_tasks() |
| 97 |
|
| 98 |
# start scheduling tasks and run until everything is scheduled |
| 99 |
schedule_tasks() |
| 100 |
|
| 101 |
# check the result of each future, raising an exception if it failed |
| 102 |
for f in futures.as_completed(fs.values()): |
| 103 |
f.result() |
| 104 |
|
| 105 |
|
| 106 |
def create_task(session, task_id, label, task_def): |
| 107 |
# create the task using 'http://taskcluster/queue', which is proxied to the queue service |
| 108 |
# with credentials appropriate to this task. |
| 109 |
|
| 110 |
# Resolve timestamps |
| 111 |
now = current_json_time(datetime_format=True) |
| 112 |
task_def = resolve_timestamps(now, task_def) |
| 113 |
|
| 114 |
if testing: |
| 115 |
json.dump( |
| 116 |
[task_id, task_def], |
| 117 |
sys.stdout, |
| 118 |
sort_keys=True, |
| 119 |
indent=2, |
| 120 |
) |
| 121 |
# add a newline |
| 122 |
print("") |
| 123 |
return |
| 124 |
|
| 125 |
logger.info(f"Creating task with taskId {task_id} for {label}") |
| 126 |
proxy_url = os.environ.get("TASKCLUSTER_PROXY_URL", "http://taskcluster").rstrip( |
| 127 |
"/" |
| 128 |
) |
| 129 |
res = session.put( |
| 130 |
f"{proxy_url}/queue/v1/task/{task_id}", |
| 131 |
json=task_def, |
| 132 |
) |
| 133 |
if res.status_code != 200: |
| 134 |
try: |
| 135 |
logger.error(res.json()["message"]) |
| 136 |
except Exception: |
| 137 |
logger.error(res.text) |
| 138 |
res.raise_for_status() |