blob: 0448c8a7b8240d95522086643410b36db5c6b6e6 [file] [log] [blame]
Chris Masonefad911a2012-03-29 12:30:26 -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 deduping_scheduler
6import logging
7
8
Chris Masone013859b2012-04-01 13:45:26 -07009class Task(object):
Chris Masonefad911a2012-03-29 12:30:26 -070010 """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 Masone013859b2012-04-01 13:45:26 -070013 Task. Each instance has enough info to schedule itself
Chris Masonefad911a2012-03-29 12:30:26 -070014 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 Masone3fba86f2012-04-03 10:06:56 -070021 def __init__(self, suite, build, pool=None):
Chris Masonefad911a2012-03-29 12:30:26 -070022 """Constructor
23
24 @param suite: the name of the suite to run, e.g. 'bvt'
Chris Masonefad911a2012-03-29 12:30:26 -070025 @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 Masonefad911a2012-03-29 12:30:26 -070031 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 Masone3fba86f2012-04-03 10:06:56 -070036 self._str = '%s: %s on %s with pool %s' % (self.__class__.__name__,
37 suite, build, pool)
Chris Masonefad911a2012-03-29 12:30:26 -070038
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 Masone3fba86f2012-04-03 10:06:56 -070051 def pool(self):
52 return self._pool
Chris Masonefad911a2012-03-29 12:30:26 -070053
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 Masone3fba86f2012-04-03 10:06:56 -070088 def Run(self, scheduler, boards, force=False):
Chris Masone013859b2012-04-01 13:45:26 -070089 """Run this task. Returns False if it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -070090
Chris Masone013859b2012-04-01 13:45:26 -070091 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 Masonefad911a2012-03-29 12:30:26 -070094
95 @param scheduler: an instance of DedupingScheduler, as defined in
96 deduping_scheduler.py
Chris Masone3fba86f2012-04-03 10:06:56 -070097 @param boards: the boards against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -070098 @param force: Always schedule the suite.
Chris Masone013859b2012-04-01 13:45:26 -070099 @return True if the task should be kept, False if not
Chris Masonefad911a2012-03-29 12:30:26 -0700100 """
Chris Masone3fba86f2012-04-03 10:06:56 -0700101 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 Masonefad911a2012-03-29 12:30:26 -0700109 return True
110
111
Chris Masone013859b2012-04-01 13:45:26 -0700112class OneShotTask(Task):
113 """A Task that can be run only once. Can schedule itself."""
Chris Masonefad911a2012-03-29 12:30:26 -0700114
115
Chris Masone3fba86f2012-04-03 10:06:56 -0700116 def Run(self, scheduler, boards, force=False):
Chris Masone013859b2012-04-01 13:45:26 -0700117 """Run this task. Returns False, indicating it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700118
Chris Masone013859b2012-04-01 13:45:26 -0700119 Run this task. Attempt to schedule the associated suite.
120 Return False, indicating to the caller that it should discard this task.
Chris Masonefad911a2012-03-29 12:30:26 -0700121
122 @param scheduler: an instance of DedupingScheduler, as defined in
123 deduping_scheduler.py
Chris Masone3fba86f2012-04-03 10:06:56 -0700124 @param boards: the boards against which to run self._suite.
Chris Masonefad911a2012-03-29 12:30:26 -0700125 @param force: Always schedule the suite.
126 @return False
127 """
Chris Masone3fba86f2012-04-03 10:06:56 -0700128 super(OneShotTask, self).Run(scheduler, boards, force)
Chris Masonefad911a2012-03-29 12:30:26 -0700129 return False