Debian Codegraph @

third_party/python/taskcluster_taskgraph/taskgraph/optimize/strategies.py

raw
1 import logging
2 from datetime import datetime
3
4 from taskgraph.optimize.base import OptimizationStrategy, register_strategy
5 from taskgraph.util.path import match as match_path
6 from taskgraph.util.taskcluster import find_task_id, status_task
7
8 logger = logging.getLogger("optimization")
9
10
11 @register_strategy("index-search")
12 class IndexSearch(OptimizationStrategy):
13 # A task with no dependencies remaining after optimization will be replaced
14 # if artifacts exist for the corresponding index_paths.
15 # Otherwise, we're in one of the following cases:
16 # - the task has un-optimized dependencies
17 # - the artifacts have expired
18 # - some changes altered the index_paths and new artifacts need to be
19 # created.
20 # In every of those cases, we need to run the task to create or refresh
21 # artifacts.
22
23 fmt = "%Y-%m-%dT%H:%M:%S.%fZ"
24
25 def should_replace_task(self, task, params, deadline, arg):
26 "Look for a task with one of the given index paths"
27 batched = False
28 # Appease static checker that doesn't understand that this is not needed
29 label_to_taskid = {}
30 taskid_to_status = {}
31
32 if isinstance(arg, tuple) and len(arg) == 3:
33 # allow for a batched call optimization instead of two queries
34 # per index path
35 index_paths, label_to_taskid, taskid_to_status = arg
36 batched = True
37 else:
38 index_paths = arg
39
40 for index_path in index_paths:
41 try:
42 if batched:
43 task_id = label_to_taskid[index_path]
44 status = taskid_to_status[task_id]
45 else:
46 # 404 is raised as `KeyError` also end up here
47 task_id = find_task_id(index_path)
48 status = status_task(task_id)
49 # status can be `None` if we're in `testing` mode
50 # (e.g. test-action-callback)
51 if not status or status.get("state") in ("exception", "failed"):
52 logger.debug(
53 f"not replacing {task.label} with {task_id} because it is in failed or exception state"
54 )
55 continue
56
57 if deadline and datetime.strptime(
58 status["expires"], self.fmt
59 ) < datetime.strptime(deadline, self.fmt):
60 logger.debug(
61 f"not replacing {task.label} with {task_id} because it expires before {deadline}"
62 )
63 continue
64
65 return task_id
66 except KeyError:
67 # go on to the next index path
68 pass
69
70 return False
71
72
73 @register_strategy("skip-unless-changed")
74 class SkipUnlessChanged(OptimizationStrategy):
75 def check(self, files_changed, patterns):
76 for pattern in patterns:
77 for path in files_changed:
78 if match_path(path, pattern):
79 return True
80 return False
81
82 def should_remove_task(self, task, params, file_patterns):
83 # skip-unless-changed should not apply when there is no commit delta,
84 # such as for cron and action tasks (there will never be file changes)
85 if params.get("base_rev") and params.get("head_rev") == params.get("base_rev"):
86 return False
87
88 changed = self.check(params["files_changed"], file_patterns)
89 if not changed:
90 logger.debug(
91 f'no files found matching a pattern in `skip-unless-changed` for "{task.label}"'
92 )
93 return True
94 return False