blob: ec7ebd38332791558504350f2942ef9060546cd6 [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
7import deduping_scheduler, timed_event, platform_enumerator
8
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.
16 @var _enumerator: a PlatformEnumerator, used to list plaforms known to
17 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)
31 self._enumerator = platform_enumerator.PlatformEnumerator(afe)
32
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."""
49 # TODO(cmasone): Make Handle() deal with platforms.
50 platforms = self._enumerator.Enumerate()
51
52 for e in self._events:
53 if e.ShouldHandle():
54 e.Handle(self._scheduler)