blob: 09e3244f8a7dd4a0e40ed713fd2b70c07603fed7 [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 Masonefe5a5092012-04-11 18:29:07 -07007import board_enumerator, deduping_scheduler, forgiving_config_parser
Chris Masone67f06d62012-04-12 15:16:56 -07008import manifest_versions, task, base_event, 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 Masone96f16632012-04-04 18:36:03 -070035 def SetUpEventsAndTasks(self, config):
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.
39 """
40 self._events = [timed_event.Nightly.CreateFromConfig(config),
41 timed_event.Weekly.CreateFromConfig(config)]
42
43 tasks = self.TasksFromConfig(config)
44
45 for event in self._events:
46 if event.keyword in tasks:
47 event.tasks = tasks[event.keyword]
48 # TODO(cmasone): warn about unknown keywords?
49
50
51 def TasksFromConfig(self, config):
52 """Generate a dict of {event_keyword: [tasks]} mappings from |config|.
53
54 For each section in |config| that encodes a Task, instantiate a Task
55 object. Determine the event that Task is supposed to run_on and
56 append the object to a list associated with the appropriate event
57 keyword. Return a dictionary of these keyword: list of task mappings.
58
59 @param config: a ForgivingConfigParser containing tasks to be parsed.
60 @return dict of {event_keyword: [tasks]} mappings.
61 @raise MalformedConfigEntry on a task parsing error.
62 """
63 tasks = {}
64 for section in config.sections():
65 if not timed_event.TimedEvent.HonorsSection(section):
66 try:
67 keyword, new_task = task.Task.CreateFromConfigSection(
68 config, section)
69 except task.MalformedConfigEntry as e:
70 logging.warn('%s is malformed: %s', section, e)
71 continue
72 tasks.setdefault(keyword, []).append(new_task)
73 return tasks
Chris Masone2d61ca22012-04-02 16:52:46 -070074
75
Chris Masone67f06d62012-04-12 15:16:56 -070076 def RunForever(self, mv):
77 """Main loop of the scheduler. Runs til the process is killed.
78
79 @param mv: an instance of manifest_versions.ManifestVersions.
80 """
Chris Masone2d61ca22012-04-02 16:52:46 -070081 while True:
Chris Masone67f06d62012-04-12 15:16:56 -070082 self.HandleEventsOnce(mv)
83 mv.Update()
84 # TODO(cmasone): Do we want to run every _LOOP_INTERVAL_SECONDS?
85 # Or is it OK to wait that long between every run?
Chris Masonefe5a5092012-04-11 18:29:07 -070086 time.sleep(self._LOOP_INTERVAL_SECONDS)
Chris Masone2d61ca22012-04-02 16:52:46 -070087
88
Chris Masone67f06d62012-04-12 15:16:56 -070089 def HandleEventsOnce(self, mv):
90 """One turn through the loop. Separated out for unit testing.
91
92 @param mv: an instance of manifest_versions.ManifestVersions.
93 """
Chris Masone92874d32012-04-03 10:13:04 -070094 boards = self._enumerator.Enumerate()
Chris Masone9273c0d2012-04-13 11:28:02 -070095 logging.info('Running suites for boards: %r', boards)
Chris Masone2d61ca22012-04-02 16:52:46 -070096 for e in self._events:
97 if e.ShouldHandle():
Chris Masone96f16632012-04-04 18:36:03 -070098 for board in boards:
Chris Masone67f06d62012-04-12 15:16:56 -070099 branch_builds = e.GetBranchBuildsForBoard(board, mv)
Chris Masone96f16632012-04-04 18:36:03 -0700100 e.Handle(self._scheduler, branch_builds, board)
Chris Masone67f06d62012-04-12 15:16:56 -0700101
102
103 def ForceEventsOnceForBuild(self, keywords, build_name):
104 """Force events with provided keywords to happen, with given build.
105
106 @param keywords: iterable of event keywords to force
107 @param build_name: instead of looking up builds to test, test this one.
108 """
109 board, type, milestone, manifest = base_event.ParseBuildName(build_name)
110 branch_builds = {task.PickBranchName(type, milestone): build_name}
111 logging.info('Testing build %s-%s on %s' % (milestone, manifest, board))
112
113 for e in self._events:
114 if e.keyword in keywords:
115 e.Handle(self._scheduler, branch_builds, board, force=True)