1 # Copyright (c) 2009 Google Inc. All rights reserved.
2 # Copyright (c) 2009 Apple Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
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
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.
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.
34 from datetime import datetime, timedelta
36 from webkitpy.common.system.executive import ScriptError
37 from webkitpy.common.system.deprecated_logging import log, OutputTee
40 # FIXME: This will be caught by "except Exception:" blocks, we should consider
41 # making this inherit from SystemExit instead (or BaseException, except that's not recommended).
42 class TerminateQueue(Exception):
46 class QueueEngineDelegate:
47 def queue_log_path(self):
48 raise NotImplementedError, "subclasses must implement"
50 def work_item_log_path(self, work_item):
51 raise NotImplementedError, "subclasses must implement"
53 def begin_work_queue(self):
54 raise NotImplementedError, "subclasses must implement"
56 def should_continue_work_queue(self):
57 raise NotImplementedError, "subclasses must implement"
59 def next_work_item(self):
60 raise NotImplementedError, "subclasses must implement"
62 def should_proceed_with_work_item(self, work_item):
63 # returns (safe_to_proceed, waiting_message, patch)
64 raise NotImplementedError, "subclasses must implement"
66 def process_work_item(self, work_item):
67 raise NotImplementedError, "subclasses must implement"
69 def handle_unexpected_error(self, work_item, message):
70 raise NotImplementedError, "subclasses must implement"
74 def __init__(self, name, delegate, wakeup_event):
76 self._delegate = delegate
77 self._wakeup_event = wakeup_event
78 self._output_tee = OutputTee()
80 log_date_format = "%Y-%m-%d %H:%M:%S"
81 sleep_duration_text = "2 mins" # This could be generated from seconds_to_sleep
82 seconds_to_sleep = 120
83 handled_error_code = 2
85 # Child processes exit with a special code to the parent queue process can detect the error was handled.
87 def exit_after_handled_error(cls, error):
89 exit(cls.handled_error_code)
94 self._delegate.begin_work_queue()
95 while (self._delegate.should_continue_work_queue()):
97 self._ensure_work_log_closed()
98 work_item = self._delegate.next_work_item()
100 self._sleep("No work item.")
102 if not self._delegate.should_proceed_with_work_item(work_item):
103 self._sleep("Not proceeding with work item.")
106 # FIXME: Work logs should not depend on bug_id specificaly.
107 # This looks fixed, no?
108 self._open_work_log(work_item)
110 if not self._delegate.process_work_item(work_item):
111 log("Unable to process work item.")
113 except ScriptError, e:
114 # Use a special exit code to indicate that the error was already
115 # handled in the child process and we should just keep looping.
116 if e.exit_code == self.handled_error_code:
118 message = "Unexpected failure when processing patch! Please file a bug against webkit-patch.\n%s" % e.message_with_output()
119 self._delegate.handle_unexpected_error(work_item, message)
120 except TerminateQueue, e:
121 self._stopping("TerminateQueue exception received.")
123 except KeyboardInterrupt, e:
124 self._stopping("User terminated queue.")
127 traceback.print_exc()
128 # Don't try tell the status bot, in case telling it causes an exception.
129 self._sleep("Exception while preparing queue")
130 self._stopping("Delegate terminated queue.")
133 def _stopping(self, message):
134 log("\n%s" % message)
135 self._delegate.stop_work_queue(message)
136 # Be careful to shut down our OutputTee or the unit tests will be unhappy.
137 self._ensure_work_log_closed()
138 self._output_tee.remove_log(self._queue_log)
140 def _begin_logging(self):
141 self._queue_log = self._output_tee.add_log(self._delegate.queue_log_path())
142 self._work_log = None
144 def _open_work_log(self, work_item):
145 work_item_log_path = self._delegate.work_item_log_path(work_item)
146 if not work_item_log_path:
148 self._work_log = self._output_tee.add_log(work_item_log_path)
150 def _ensure_work_log_closed(self):
151 # If we still have a bug log open, close it.
153 self._output_tee.remove_log(self._work_log)
154 self._work_log = None
157 """Overriden by the unit tests to allow testing _sleep_message"""
158 return datetime.now()
160 def _sleep_message(self, message):
161 wake_time = self._now() + timedelta(seconds=self.seconds_to_sleep)
162 return "%s Sleeping until %s (%s)." % (message, wake_time.strftime(self.log_date_format), self.sleep_duration_text)
164 def _sleep(self, message):
165 log(self._sleep_message(message))
166 self._wakeup_event.wait(self.seconds_to_sleep)
167 self._wakeup_event.clear()