Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 5 | import logging, time |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 6 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 7 | import base_event, board_enumerator, build_event, deduping_scheduler |
| 8 | import forgiving_config_parser, manifest_versions, task, timed_event |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class Driver(object): |
| 12 | """Implements the main loop of the suite_scheduler. |
| 13 | |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 14 | @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 15 | |
| 16 | @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE. |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 17 | @var _enumerator: a BoardEnumerator, used to list plaforms known to |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 18 | the AFE |
| 19 | @var _events: list of BaseEvents to be handled each time through main loop. |
| 20 | """ |
| 21 | |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 22 | _LOOP_INTERVAL_SECONDS = 5 * 60 |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 23 | |
| 24 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 25 | def __init__(self, scheduler, enumerator): |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 26 | """Constructor |
| 27 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 28 | @param scheduler: an instance of deduping_scheduler.DedupingScheduler. |
| 29 | @param enumerator: an instance of board_enumerator.BoardEnumerator. |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 30 | """ |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 31 | self._scheduler = scheduler |
| 32 | self._enumerator = enumerator |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 33 | |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 34 | |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 35 | def SetUpEventsAndTasks(self, config, mv): |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 36 | """Populate self._events and create task lists from config. |
| 37 | |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 38 | @param config: an instance of ForgivingConfigParser. |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 39 | @param mv: an instance of ManifestVersions. |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 40 | """ |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 41 | self._events = [timed_event.Nightly.CreateFromConfig(config, mv), |
| 42 | timed_event.Weekly.CreateFromConfig(config, mv), |
| 43 | build_event.NewBuild.CreateFromConfig(config, mv)] |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 44 | |
| 45 | tasks = self.TasksFromConfig(config) |
| 46 | |
| 47 | for event in self._events: |
| 48 | if event.keyword in tasks: |
| 49 | event.tasks = tasks[event.keyword] |
| 50 | # TODO(cmasone): warn about unknown keywords? |
| 51 | |
| 52 | |
| 53 | def TasksFromConfig(self, config): |
| 54 | """Generate a dict of {event_keyword: [tasks]} mappings from |config|. |
| 55 | |
| 56 | For each section in |config| that encodes a Task, instantiate a Task |
| 57 | object. Determine the event that Task is supposed to run_on and |
| 58 | append the object to a list associated with the appropriate event |
| 59 | keyword. Return a dictionary of these keyword: list of task mappings. |
| 60 | |
| 61 | @param config: a ForgivingConfigParser containing tasks to be parsed. |
| 62 | @return dict of {event_keyword: [tasks]} mappings. |
| 63 | @raise MalformedConfigEntry on a task parsing error. |
| 64 | """ |
| 65 | tasks = {} |
| 66 | for section in config.sections(): |
Chris Masone | 93f51d4 | 2012-04-18 08:46:52 -0700 | [diff] [blame] | 67 | if not base_event.HonoredSection(section): |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 68 | try: |
| 69 | keyword, new_task = task.Task.CreateFromConfigSection( |
| 70 | config, section) |
| 71 | except task.MalformedConfigEntry as e: |
| 72 | logging.warn('%s is malformed: %s', section, e) |
| 73 | continue |
| 74 | tasks.setdefault(keyword, []).append(new_task) |
| 75 | return tasks |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 76 | |
| 77 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 78 | def RunForever(self, mv): |
| 79 | """Main loop of the scheduler. Runs til the process is killed. |
| 80 | |
| 81 | @param mv: an instance of manifest_versions.ManifestVersions. |
| 82 | """ |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 83 | while True: |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 84 | self.HandleEventsOnce(mv) |
| 85 | mv.Update() |
| 86 | # TODO(cmasone): Do we want to run every _LOOP_INTERVAL_SECONDS? |
| 87 | # Or is it OK to wait that long between every run? |
Chris Masone | fe5a509 | 2012-04-11 18:29:07 -0700 | [diff] [blame] | 88 | time.sleep(self._LOOP_INTERVAL_SECONDS) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 89 | |
| 90 | |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 91 | def HandleEventsOnce(self, mv): |
| 92 | """One turn through the loop. Separated out for unit testing. |
| 93 | |
| 94 | @param mv: an instance of manifest_versions.ManifestVersions. |
| 95 | """ |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 96 | boards = self._enumerator.Enumerate() |
Chris Masone | 9273c0d | 2012-04-13 11:28:02 -0700 | [diff] [blame] | 97 | logging.info('Running suites for boards: %r', boards) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 98 | for e in self._events: |
| 99 | if e.ShouldHandle(): |
Chris Masone | 83af70c | 2012-04-18 14:33:05 -0700 | [diff] [blame] | 100 | logging.debug('Handling %s event', e.keyword) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 101 | for board in boards: |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 102 | branch_builds = e.GetBranchBuildsForBoard(board, mv) |
Chris Masone | 96f1663 | 2012-04-04 18:36:03 -0700 | [diff] [blame] | 103 | e.Handle(self._scheduler, branch_builds, board) |
Chris Masone | 67f06d6 | 2012-04-12 15:16:56 -0700 | [diff] [blame] | 104 | |
| 105 | |
| 106 | def ForceEventsOnceForBuild(self, keywords, build_name): |
| 107 | """Force events with provided keywords to happen, with given build. |
| 108 | |
| 109 | @param keywords: iterable of event keywords to force |
| 110 | @param build_name: instead of looking up builds to test, test this one. |
| 111 | """ |
| 112 | board, type, milestone, manifest = base_event.ParseBuildName(build_name) |
| 113 | branch_builds = {task.PickBranchName(type, milestone): build_name} |
| 114 | logging.info('Testing build %s-%s on %s' % (milestone, manifest, board)) |
| 115 | |
| 116 | for e in self._events: |
| 117 | if e.keyword in keywords: |
| 118 | e.Handle(self._scheduler, branch_builds, board, force=True) |