blob: da7562af66915e5a24deb35d627177a7d3a89fa3 [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
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 Masone013859b2012-04-01 13:45:26 -070090 def Run(self, scheduler, force=False):
91 """Run this task. Returns False if it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -070092
Chris Masone013859b2012-04-01 13:45:26 -070093 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 Masonefad911a2012-03-29 12:30:26 -070096
97 @param scheduler: an instance of DedupingScheduler, as defined in
98 deduping_scheduler.py
99 @param force: Always schedule the suite.
Chris Masone013859b2012-04-01 13:45:26 -0700100 @return True if the task should be kept, False if not
Chris Masonefad911a2012-03-29 12:30:26 -0700101 """
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 Masone013859b2012-04-01 13:45:26 -0700111class OneShotTask(Task):
112 """A Task that can be run only once. Can schedule itself."""
Chris Masonefad911a2012-03-29 12:30:26 -0700113
114
Chris Masone013859b2012-04-01 13:45:26 -0700115 def Run(self, scheduler, force=False):
116 """Run this task. Returns False, indicating it should be destroyed.
Chris Masonefad911a2012-03-29 12:30:26 -0700117
Chris Masone013859b2012-04-01 13:45:26 -0700118 Run this task. Attempt to schedule the associated suite.
119 Return False, indicating to the caller that it should discard this task.
Chris Masonefad911a2012-03-29 12:30:26 -0700120
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 Masone013859b2012-04-01 13:45:26 -0700126 super(OneShotTask, self).Run(scheduler, force)
Chris Masonefad911a2012-03-29 12:30:26 -0700127 return False