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