blob: 0b40a4e061b94719c38b830a96ba9fca26dcef1a [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 Masonefe5a5092012-04-11 18:29:07 -070014 @var _LOOP_INTERVAL_SECONDS: seconds to wait between loop iterations.
Chris Masone2d61ca22012-04-02 16:52:46 -070015
16 @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
Chris Masone3fba86f2012-04-03 10:06:56 -070017 @var _enumerator: a BoardEnumerator, used to list plaforms known to
Chris Masone2d61ca22012-04-02 16:52:46 -070018 the AFE
19 @var _events: list of BaseEvents to be handled each time through main loop.
20 """
21
Chris Masonefe5a5092012-04-11 18:29:07 -070022 _LOOP_INTERVAL_SECONDS = 5 * 60
Chris Masone2d61ca22012-04-02 16:52:46 -070023
24
Chris Masone67f06d62012-04-12 15:16:56 -070025 def __init__(self, scheduler, enumerator):
Chris Masone2d61ca22012-04-02 16:52:46 -070026 """Constructor
27
Chris Masone67f06d62012-04-12 15:16:56 -070028 @param scheduler: an instance of deduping_scheduler.DedupingScheduler.
29 @param enumerator: an instance of board_enumerator.BoardEnumerator.
Chris Masone2d61ca22012-04-02 16:52:46 -070030 """
Chris Masone67f06d62012-04-12 15:16:56 -070031 self._scheduler = scheduler
32 self._enumerator = enumerator
Chris Masone2d61ca22012-04-02 16:52:46 -070033
Chris Masone2d61ca22012-04-02 16:52:46 -070034
Chris Masone93f51d42012-04-18 08:46:52 -070035 def SetUpEventsAndTasks(self, config, mv):
Chris Masone67f06d62012-04-12 15:16:56 -070036 """Populate self._events and create task lists from config.
37
Chris Masone96f16632012-04-04 18:36:03 -070038 @param config: an instance of ForgivingConfigParser.
Chris Masone93f51d42012-04-18 08:46:52 -070039 @param mv: an instance of ManifestVersions.
Chris Masone96f16632012-04-04 18:36:03 -070040 """
Chris Masone93f51d42012-04-18 08:46:52 -070041 self._events = [timed_event.Nightly.CreateFromConfig(config, mv),
42 timed_event.Weekly.CreateFromConfig(config, mv),
43 build_event.NewBuild.CreateFromConfig(config, mv)]
Chris Masone96f16632012-04-04 18:36:03 -070044
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 Masone93f51d42012-04-18 08:46:52 -070067 if not base_event.HonoredSection(section):
Chris Masone96f16632012-04-04 18:36:03 -070068 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 Masone2d61ca22012-04-02 16:52:46 -070076
77
Chris Masone67f06d62012-04-12 15:16:56 -070078 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 Masone73a78382012-04-20 13:25:51 -070083 for event in self._events:
84 event.Prepare()
Chris Masone2d61ca22012-04-02 16:52:46 -070085 while True:
Chris Masone67f06d62012-04-12 15:16:56 -070086 self.HandleEventsOnce(mv)
87 mv.Update()
88 # TODO(cmasone): Do we want to run every _LOOP_INTERVAL_SECONDS?
89 # Or is it OK to wait that long between every run?
Chris Masonefe5a5092012-04-11 18:29:07 -070090 time.sleep(self._LOOP_INTERVAL_SECONDS)
Chris Masone2d61ca22012-04-02 16:52:46 -070091
92
Chris Masone67f06d62012-04-12 15:16:56 -070093 def HandleEventsOnce(self, mv):
94 """One turn through the loop. Separated out for unit testing.
95
96 @param mv: an instance of manifest_versions.ManifestVersions.
97 """
Chris Masone92874d32012-04-03 10:13:04 -070098 boards = self._enumerator.Enumerate()
Chris Masone9273c0d2012-04-13 11:28:02 -070099 logging.info('Running suites for boards: %r', boards)
Chris Masone2d61ca22012-04-02 16:52:46 -0700100 for e in self._events:
101 if e.ShouldHandle():
Chris Masone83af70c2012-04-18 14:33:05 -0700102 logging.debug('Handling %s event', e.keyword)
Chris Masone96f16632012-04-04 18:36:03 -0700103 for board in boards:
Chris Masoned17d1852012-04-20 11:52:49 -0700104 branch_builds = e.GetBranchBuildsForBoard(board)
Chris Masone96f16632012-04-04 18:36:03 -0700105 e.Handle(self._scheduler, branch_builds, board)
Chris Masone67f06d62012-04-12 15:16:56 -0700106
107
108 def ForceEventsOnceForBuild(self, keywords, build_name):
109 """Force events with provided keywords to happen, with given build.
110
111 @param keywords: iterable of event keywords to force
112 @param build_name: instead of looking up builds to test, test this one.
113 """
114 board, type, milestone, manifest = base_event.ParseBuildName(build_name)
115 branch_builds = {task.PickBranchName(type, milestone): build_name}
116 logging.info('Testing build %s-%s on %s' % (milestone, manifest, board))
117
118 for e in self._events:
119 if e.keyword in keywords:
120 e.Handle(self._scheduler, branch_builds, board, force=True)