Chris Masone | fad911a | 2012-03-29 12:30:26 -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 deduping_scheduler |
| 6 | import logging |
| 7 | |
| 8 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 9 | class Task(object): |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 10 | """Represents an entry from the scheduler config. Can schedule itself. |
| 11 | |
| 12 | Each entry from the scheduler config file maps one-to-one to a |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 13 | Task. Each instance has enough info to schedule itself |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 14 | on-demand with the AFE. |
| 15 | |
| 16 | This class also overrides __hash__() and all comparitor methods to enable |
| 17 | correct use in dicts, sets, etc. |
| 18 | """ |
| 19 | |
| 20 | |
| 21 | def __init__(self, suite, board, build, pool=None): |
| 22 | """Constructor |
| 23 | |
| 24 | @param suite: the name of the suite to run, e.g. 'bvt' |
| 25 | @param board: the board to run the suite on, e.g. x86-alex |
| 26 | @param build: the build to install e.g. |
| 27 | x86-alex-release/R18-1655.0.0-a1-b1584. |
| 28 | @param pool: the pool of machines to use for scheduling purposes. |
| 29 | Default: None |
| 30 | """ |
| 31 | self._suite = suite |
| 32 | self._board = board |
| 33 | self._build = build |
| 34 | self._pool = pool |
| 35 | # Since we expect __hash__() and other comparitor methods to be used |
| 36 | # frequently by set operations, and they use str() a lot, pre-compute |
| 37 | # the string representation of this object. |
| 38 | self._str = '%s: %s on %s against %s' % (self.__class__.__name__, suite, |
| 39 | build, board) |
| 40 | |
| 41 | |
| 42 | @property |
| 43 | def suite(self): |
| 44 | return self._suite |
| 45 | |
| 46 | |
| 47 | @property |
| 48 | def build(self): |
| 49 | return self._build |
| 50 | |
| 51 | |
| 52 | @property |
| 53 | def board(self): |
| 54 | return self._board |
| 55 | |
| 56 | |
| 57 | def __str__(self): |
| 58 | return self._str |
| 59 | |
| 60 | |
| 61 | def __lt__(self, other): |
| 62 | return str(self) < str(other) |
| 63 | |
| 64 | |
| 65 | def __le__(self, other): |
| 66 | return str(self) <= str(other) |
| 67 | |
| 68 | |
| 69 | def __eq__(self, other): |
| 70 | return str(self) == str(other) |
| 71 | |
| 72 | |
| 73 | def __ne__(self, other): |
| 74 | return str(self) != str(other) |
| 75 | |
| 76 | |
| 77 | def __gt__(self, other): |
| 78 | return str(self) > str(other) |
| 79 | |
| 80 | |
| 81 | def __ge__(self, other): |
| 82 | return str(self) >= str(other) |
| 83 | |
| 84 | |
| 85 | def __hash__(self): |
| 86 | """Allows instances to be correctly deduped when used in a set.""" |
| 87 | return hash(str(self)) |
| 88 | |
| 89 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 90 | def Run(self, scheduler, force=False): |
| 91 | """Run this task. Returns False if it should be destroyed. |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 92 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 93 | Execute this task. Attempt to schedule the associated suite. |
| 94 | Return True if this task should be kept around, False if it |
| 95 | should be destroyed. This allows for one-shot Tasks. |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 96 | |
| 97 | @param scheduler: an instance of DedupingScheduler, as defined in |
| 98 | deduping_scheduler.py |
| 99 | @param force: Always schedule the suite. |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 100 | @return True if the task should be kept, False if not |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 101 | """ |
| 102 | try: |
| 103 | if not scheduler.ScheduleSuite(self._suite, self._board, |
| 104 | self._build, self._pool, force): |
| 105 | logging.info('Skipping scheduling on %s', self) |
| 106 | except deduping_scheduler.DedupingSchedulerException as e: |
| 107 | logging.error(e) |
| 108 | return True |
| 109 | |
| 110 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 111 | class OneShotTask(Task): |
| 112 | """A Task that can be run only once. Can schedule itself.""" |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 113 | |
| 114 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 115 | def Run(self, scheduler, force=False): |
| 116 | """Run this task. Returns False, indicating it should be destroyed. |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 117 | |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 118 | Run this task. Attempt to schedule the associated suite. |
| 119 | Return False, indicating to the caller that it should discard this task. |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 120 | |
| 121 | @param scheduler: an instance of DedupingScheduler, as defined in |
| 122 | deduping_scheduler.py |
| 123 | @param force: Always schedule the suite. |
| 124 | @return False |
| 125 | """ |
Chris Masone | 013859b | 2012-04-01 13:45:26 -0700 | [diff] [blame^] | 126 | super(OneShotTask, self).Run(scheduler, force) |
Chris Masone | fad911a | 2012-03-29 12:30:26 -0700 | [diff] [blame] | 127 | return False |