blob: caeb7768c867ce2440597bd3b40caed5ef0466e3 [file] [log] [blame]
Chris Masone2d61ca22012-04-02 16:52:46 -07001# 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 Masone96f16632012-04-04 18:36:03 -07005import logging, time
Chris Masone2d61ca22012-04-02 16:52:46 -07006
Chris Masone93f51d42012-04-18 08:46:52 -07007import base_event, board_enumerator, build_event, deduping_scheduler
8import forgiving_config_parser, manifest_versions, task, timed_event
Chris Masone2d61ca22012-04-02 16:52:46 -07009
10
11class Driver(object):
12 """Implements the main loop of the suite_scheduler.
13
Chris Masonebf8775a2012-09-10 10:44:18 -070014 @var EVENT_CLASSES: list of the event classes Driver supports.
Chris Masonefe5a5092012-04-11 18:29:07 -070015 @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations.
Chris Masone2d61ca22012-04-02 16:52:46 -070016
17 @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
Chris Masone3fba86f2012-04-03 10:06:56 -070018 @var _enumerator: a BoardEnumerator, used to list plaforms known to
Chris Masone2d61ca22012-04-02 16:52:46 -070019 the AFE
Chris Masone855d86f2012-05-07 13:48:07 -070020 @var _events: dict of BaseEvents to be handled each time through main loop.
Chris Masone2d61ca22012-04-02 16:52:46 -070021 """
22
Chris Masonebf8775a2012-09-10 10:44:18 -070023 EVENT_CLASSES = [timed_event.Nightly, timed_event.Weekly,
24 build_event.NewBuild]
Chris Masonefe5a5092012-04-11 18:29:07 -070025 _LOOP_INTERVAL_SECONDS = 5 * 60
Chris Masone2d61ca22012-04-02 16:52:46 -070026
27
Chris Masone67f06d62012-04-12 15:16:56 -070028 def __init__(self, scheduler, enumerator):
Chris Masone2d61ca22012-04-02 16:52:46 -070029 """Constructor
30
Chris Masone67f06d62012-04-12 15:16:56 -070031 @param scheduler: an instance of deduping_scheduler.DedupingScheduler.
32 @param enumerator: an instance of board_enumerator.BoardEnumerator.
Chris Masone2d61ca22012-04-02 16:52:46 -070033 """
Chris Masone67f06d62012-04-12 15:16:56 -070034 self._scheduler = scheduler
35 self._enumerator = enumerator
Chris Masone2d61ca22012-04-02 16:52:46 -070036
Chris Masone2d61ca22012-04-02 16:52:46 -070037
Chris Masone855d86f2012-05-07 13:48:07 -070038 def RereadAndReprocessConfig(self, config, mv):
39 """Re-read config, re-populate self._events and recreate task lists.
40
41 @param config: an instance of ForgivingConfigParser.
42 @param mv: an instance of ManifestVersions.
43 """
44 config.reread()
45 new_events = self._CreateEventsWithTasks(config, mv)
46 for keyword, event in self._events.iteritems():
47 event.Merge(new_events[keyword])
48
49
Chris Masone93f51d42012-04-18 08:46:52 -070050 def SetUpEventsAndTasks(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -070051 """Populate self._events and create task lists from config.
52
Chris Masone96f16632012-04-04 18:36:03 -070053 @param config: an instance of ForgivingConfigParser.
Chris Masone93f51d42012-04-18 08:46:52 -070054 @param mv: an instance of ManifestVersions.
Chris Masone96f16632012-04-04 18:36:03 -070055 """
Chris Masone855d86f2012-05-07 13:48:07 -070056 self._events = self._CreateEventsWithTasks(config, mv)
57
58
59 def _CreateEventsWithTasks(self, config, mv):
60 """Create task lists from config, and assign to newly-minted events.
61
62 Calling multiple times should start afresh each time.
63
64 @param config: an instance of ForgivingConfigParser.
65 @param mv: an instance of ManifestVersions.
66 """
Chris Masone855d86f2012-05-07 13:48:07 -070067 events = {}
Chris Masonebf8775a2012-09-10 10:44:18 -070068 for klass in self.EVENT_CLASSES:
Chris Masone855d86f2012-05-07 13:48:07 -070069 events[klass.KEYWORD] = klass.CreateFromConfig(config, mv)
Chris Masone96f16632012-04-04 18:36:03 -070070
71 tasks = self.TasksFromConfig(config)
Chris Masone645c7e42012-05-17 17:28:40 -070072 for keyword, task_list in tasks.iteritems():
73 if keyword in events:
74 events[keyword].tasks = task_list
75 else:
76 logging.warn('%s, is an unknown keyword.', keyword)
Chris Masone855d86f2012-05-07 13:48:07 -070077 return events
Chris Masone96f16632012-04-04 18:36:03 -070078
79
80 def TasksFromConfig(self, config):
81 """Generate a dict of {event_keyword: [tasks]} mappings from |config|.
82
83 For each section in |config| that encodes a Task, instantiate a Task
84 object. Determine the event that Task is supposed to run_on and
85 append the object to a list associated with the appropriate event
86 keyword. Return a dictionary of these keyword: list of task mappings.
87
88 @param config: a ForgivingConfigParser containing tasks to be parsed.
89 @return dict of {event_keyword: [tasks]} mappings.
90 @raise MalformedConfigEntry on a task parsing error.
91 """
92 tasks = {}
93 for section in config.sections():
Chris Masone93f51d42012-04-18 08:46:52 -070094 if not base_event.HonoredSection(section):
Chris Masone96f16632012-04-04 18:36:03 -070095 try:
96 keyword, new_task = task.Task.CreateFromConfigSection(
97 config, section)
98 except task.MalformedConfigEntry as e:
99 logging.warn('%s is malformed: %s', section, e)
100 continue
101 tasks.setdefault(keyword, []).append(new_task)
102 return tasks
Chris Masone2d61ca22012-04-02 16:52:46 -0700103
104
Chris Masone855d86f2012-05-07 13:48:07 -0700105 def RunForever(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -0700106 """Main loop of the scheduler. Runs til the process is killed.
107
Chris Masone855d86f2012-05-07 13:48:07 -0700108 @param config: an instance of ForgivingConfigParser.
Chris Masone67f06d62012-04-12 15:16:56 -0700109 @param mv: an instance of manifest_versions.ManifestVersions.
110 """
Chris Masone855d86f2012-05-07 13:48:07 -0700111 for event in self._events.itervalues():
Chris Masone73a78382012-04-20 13:25:51 -0700112 event.Prepare()
Chris Masone2d61ca22012-04-02 16:52:46 -0700113 while True:
Chris Masone645c7e42012-05-17 17:28:40 -0700114 try:
115 self.HandleEventsOnce(mv)
Scott Zawalskic15c6b42012-07-09 13:16:05 -0400116 except board_enumerator.EnumeratorException as e:
Chris Masone645c7e42012-05-17 17:28:40 -0700117 logging.warn('Failed to enumerate boards: %r', e)
Chris Masone67f06d62012-04-12 15:16:56 -0700118 mv.Update()
Chris Masonefe5a5092012-04-11 18:29:07 -0700119 time.sleep(self._LOOP_INTERVAL_SECONDS)
Chris Masone855d86f2012-05-07 13:48:07 -0700120 self.RereadAndReprocessConfig(config, mv)
Chris Masone2d61ca22012-04-02 16:52:46 -0700121
122
Chris Masone67f06d62012-04-12 15:16:56 -0700123 def HandleEventsOnce(self, mv):
124 """One turn through the loop. Separated out for unit testing.
125
126 @param mv: an instance of manifest_versions.ManifestVersions.
Chris Masone645c7e42012-05-17 17:28:40 -0700127 @raise EnumeratorException if we can't enumerate any supported boards.
Chris Masone67f06d62012-04-12 15:16:56 -0700128 """
Chris Masone92874d32012-04-03 10:13:04 -0700129 boards = self._enumerator.Enumerate()
Chris Masonea3a38172012-05-14 15:19:56 -0700130 logging.info('Boards currently in the lab: %r', boards)
Chris Masone855d86f2012-05-07 13:48:07 -0700131 for e in self._events.itervalues():
Chris Masone2d61ca22012-04-02 16:52:46 -0700132 if e.ShouldHandle():
Chris Masonea3a38172012-05-14 15:19:56 -0700133 logging.info('Handling %s event', e.keyword)
Chris Masone96f16632012-04-04 18:36:03 -0700134 for board in boards:
Chris Masoned17d1852012-04-20 11:52:49 -0700135 branch_builds = e.GetBranchBuildsForBoard(board)
Chris Masone96f16632012-04-04 18:36:03 -0700136 e.Handle(self._scheduler, branch_builds, board)
Chris Masonebbde3862012-05-07 14:29:51 -0700137 e.UpdateCriteria()
Chris Masone67f06d62012-04-12 15:16:56 -0700138
139
140 def ForceEventsOnceForBuild(self, keywords, build_name):
141 """Force events with provided keywords to happen, with given build.
142
143 @param keywords: iterable of event keywords to force
144 @param build_name: instead of looking up builds to test, test this one.
145 """
146 board, type, milestone, manifest = base_event.ParseBuildName(build_name)
Chris Masonecbe42772012-04-30 22:18:23 -0700147 branch_builds = {task.PickBranchName(type, milestone): [build_name]}
Chris Masone67f06d62012-04-12 15:16:56 -0700148 logging.info('Testing build %s-%s on %s' % (milestone, manifest, board))
149
Chris Masone855d86f2012-05-07 13:48:07 -0700150 for e in self._events.itervalues():
Chris Masone67f06d62012-04-12 15:16:56 -0700151 if e.keyword in keywords:
152 e.Handle(self._scheduler, branch_builds, board, force=True)