Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 1 | # 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 | |
| 5 | import ConfigParser, logging, time |
| 6 | |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 7 | import deduping_scheduler, timed_event, board_enumerator |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class 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 Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 16 | @var _enumerator: a BoardEnumerator, used to list plaforms known to |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 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) |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 31 | self._enumerator = board_enumerator.BoardEnumerator(afe) |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 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.""" |
Chris Masone | 92874d3 | 2012-04-03 10:13:04 -0700 | [diff] [blame] | 49 | boards = self._enumerator.Enumerate() |
Chris Masone | 2d61ca2 | 2012-04-02 16:52:46 -0700 | [diff] [blame] | 50 | |
| 51 | for e in self._events: |
| 52 | if e.ShouldHandle(): |
Chris Masone | 3fba86f | 2012-04-03 10:06:56 -0700 | [diff] [blame] | 53 | e.Handle(self._scheduler, boards) |