|
1 # Copyright (c) 2009 Google Inc. All rights reserved. |
|
2 # Copyright (c) 2009 Apple Inc. All rights reserved. |
|
3 # |
|
4 # Redistribution and use in source and binary forms, with or without |
|
5 # modification, are permitted provided that the following conditions are |
|
6 # met: |
|
7 # |
|
8 # * Redistributions of source code must retain the above copyright |
|
9 # notice, this list of conditions and the following disclaimer. |
|
10 # * Redistributions in binary form must reproduce the above |
|
11 # copyright notice, this list of conditions and the following disclaimer |
|
12 # in the documentation and/or other materials provided with the |
|
13 # distribution. |
|
14 # * Neither the name of Google Inc. nor the names of its |
|
15 # contributors may be used to endorse or promote products derived from |
|
16 # this software without specific prior written permission. |
|
17 # |
|
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 |
|
30 import os |
|
31 import time |
|
32 import traceback |
|
33 |
|
34 from datetime import datetime, timedelta |
|
35 |
|
36 from webkitpy.common.net.statusserver import StatusServer |
|
37 from webkitpy.common.system.executive import ScriptError |
|
38 from webkitpy.common.system.deprecated_logging import log, OutputTee |
|
39 |
|
40 |
|
41 class TerminateQueue(Exception): |
|
42 pass |
|
43 |
|
44 |
|
45 class QueueEngineDelegate: |
|
46 def queue_log_path(self): |
|
47 raise NotImplementedError, "subclasses must implement" |
|
48 |
|
49 def work_item_log_path(self, work_item): |
|
50 raise NotImplementedError, "subclasses must implement" |
|
51 |
|
52 def begin_work_queue(self): |
|
53 raise NotImplementedError, "subclasses must implement" |
|
54 |
|
55 def should_continue_work_queue(self): |
|
56 raise NotImplementedError, "subclasses must implement" |
|
57 |
|
58 def next_work_item(self): |
|
59 raise NotImplementedError, "subclasses must implement" |
|
60 |
|
61 def should_proceed_with_work_item(self, work_item): |
|
62 # returns (safe_to_proceed, waiting_message, patch) |
|
63 raise NotImplementedError, "subclasses must implement" |
|
64 |
|
65 def process_work_item(self, work_item): |
|
66 raise NotImplementedError, "subclasses must implement" |
|
67 |
|
68 def handle_unexpected_error(self, work_item, message): |
|
69 raise NotImplementedError, "subclasses must implement" |
|
70 |
|
71 |
|
72 class QueueEngine: |
|
73 def __init__(self, name, delegate, wakeup_event): |
|
74 self._name = name |
|
75 self._delegate = delegate |
|
76 self._wakeup_event = wakeup_event |
|
77 self._output_tee = OutputTee() |
|
78 |
|
79 log_date_format = "%Y-%m-%d %H:%M:%S" |
|
80 sleep_duration_text = "2 mins" # This could be generated from seconds_to_sleep |
|
81 seconds_to_sleep = 120 |
|
82 handled_error_code = 2 |
|
83 |
|
84 # Child processes exit with a special code to the parent queue process can detect the error was handled. |
|
85 @classmethod |
|
86 def exit_after_handled_error(cls, error): |
|
87 log(error) |
|
88 exit(cls.handled_error_code) |
|
89 |
|
90 def run(self): |
|
91 self._begin_logging() |
|
92 |
|
93 self._delegate.begin_work_queue() |
|
94 while (self._delegate.should_continue_work_queue()): |
|
95 try: |
|
96 self._ensure_work_log_closed() |
|
97 work_item = self._delegate.next_work_item() |
|
98 if not work_item: |
|
99 self._sleep("No work item.") |
|
100 continue |
|
101 if not self._delegate.should_proceed_with_work_item(work_item): |
|
102 self._sleep("Not proceeding with work item.") |
|
103 continue |
|
104 |
|
105 # FIXME: Work logs should not depend on bug_id specificaly. |
|
106 # This looks fixed, no? |
|
107 self._open_work_log(work_item) |
|
108 try: |
|
109 if not self._delegate.process_work_item(work_item): |
|
110 self._sleep("Unable to process work item.") |
|
111 except ScriptError, e: |
|
112 # Use a special exit code to indicate that the error was already |
|
113 # handled in the child process and we should just keep looping. |
|
114 if e.exit_code == self.handled_error_code: |
|
115 continue |
|
116 message = "Unexpected failure when processing patch! Please file a bug against webkit-patch.\n%s" % e.message_with_output() |
|
117 self._delegate.handle_unexpected_error(work_item, message) |
|
118 except TerminateQueue, e: |
|
119 log("\nTerminateQueue exception received.") |
|
120 return 0 |
|
121 except KeyboardInterrupt, e: |
|
122 log("\nUser terminated queue.") |
|
123 return 1 |
|
124 except Exception, e: |
|
125 traceback.print_exc() |
|
126 # Don't try tell the status bot, in case telling it causes an exception. |
|
127 self._sleep("Exception while preparing queue") |
|
128 # Never reached. |
|
129 self._ensure_work_log_closed() |
|
130 |
|
131 def _begin_logging(self): |
|
132 self._queue_log = self._output_tee.add_log(self._delegate.queue_log_path()) |
|
133 self._work_log = None |
|
134 |
|
135 def _open_work_log(self, work_item): |
|
136 work_item_log_path = self._delegate.work_item_log_path(work_item) |
|
137 self._work_log = self._output_tee.add_log(work_item_log_path) |
|
138 |
|
139 def _ensure_work_log_closed(self): |
|
140 # If we still have a bug log open, close it. |
|
141 if self._work_log: |
|
142 self._output_tee.remove_log(self._work_log) |
|
143 self._work_log = None |
|
144 |
|
145 def _now(self): |
|
146 """Overriden by the unit tests to allow testing _sleep_message""" |
|
147 return datetime.now() |
|
148 |
|
149 def _sleep_message(self, message): |
|
150 wake_time = self._now() + timedelta(seconds=self.seconds_to_sleep) |
|
151 return "%s Sleeping until %s (%s)." % (message, wake_time.strftime(self.log_date_format), self.sleep_duration_text) |
|
152 |
|
153 def _sleep(self, message): |
|
154 log(self._sleep_message(message)) |
|
155 self._wakeup_event.wait(self.seconds_to_sleep) |
|
156 self._wakeup_event.clear() |