blob: f0765a1770432db0e6089ef5ee85af4ee6bbd885 [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
5import ConfigParser, logging, time
6
Chris Masone92874d32012-04-03 10:13:04 -07007import deduping_scheduler, timed_event, board_enumerator
Chris Masone2d61ca22012-04-02 16:52:46 -07008
9
10class Driver(object):
11 """Implements the main loop of the suite_scheduler.
12
13 @var _LOOP_INTERVAL: time to wait between loop iterations.
14
15 @var _scheduler: a DedupingScheduler, used to schedule jobs with the AFE.
Chris Masone3fba86f2012-04-03 10:06:56 -070016 @var _enumerator: a BoardEnumerator, used to list plaforms known to
Chris Masone2d61ca22012-04-02 16:52:46 -070017 the AFE
18 @var _events: list of BaseEvents to be handled each time through main loop.
19 """
20
21 _LOOP_INTERVAL = 5
22
23
24 def __init__(self, afe, config):
25 """Constructor
26
27 @param afe: an instance of AFE as defined in server/frontend.py.
28 @param config: an instance of ForgivingConfigParser.
29 """
30 self._scheduler = deduping_scheduler.DedupingScheduler(afe)
Chris Masone3fba86f2012-04-03 10:06:56 -070031 self._enumerator = board_enumerator.BoardEnumerator(afe)
Chris Masone2d61ca22012-04-02 16:52:46 -070032
33 # TODO(cmasone): populate this from |config|.
34 tasks = []
35
36 self._events = [timed_event.Nightly.CreateFromConfig(config, tasks),
37 timed_event.Weekly.CreateFromConfig(config, tasks)]
38
39
40 def RunForever(self):
41 """Main loop of the scheduler. Runs til the process is killed."""
42 while True:
43 self.HandleEventsOnce()
44 time.sleep(self._LOOP_INTERVAL)
45
46
47 def HandleEventsOnce(self):
48 """One turn through the loop. Separated out for unit testing."""
Chris Masone92874d32012-04-03 10:13:04 -070049 boards = self._enumerator.Enumerate()
Chris Masone2d61ca22012-04-02 16:52:46 -070050
51 for e in self._events:
52 if e.ShouldHandle():
Chris Masone3fba86f2012-04-03 10:06:56 -070053 e.Handle(self._scheduler, boards)