| OLD | NEW |
| (Empty) |
| 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 logging | |
| 31 import sys | |
| 32 import traceback | |
| 33 | |
| 34 from datetime import datetime, timedelta | |
| 35 | |
| 36 from webkitpy.common.system.executive import ScriptError | |
| 37 from webkitpy.common.system.outputtee import OutputTee | |
| 38 | |
| 39 _log = logging.getLogger(__name__) | |
| 40 | |
| 41 | |
| 42 # FIXME: This will be caught by "except Exception:" blocks, we should consider | |
| 43 # making this inherit from SystemExit instead (or BaseException, except that's n
ot recommended). | |
| 44 class TerminateQueue(Exception): | |
| 45 pass | |
| 46 | |
| 47 | |
| 48 class QueueEngineDelegate: | |
| 49 def queue_log_path(self): | |
| 50 raise NotImplementedError, "subclasses must implement" | |
| 51 | |
| 52 def work_item_log_path(self, work_item): | |
| 53 raise NotImplementedError, "subclasses must implement" | |
| 54 | |
| 55 def begin_work_queue(self): | |
| 56 raise NotImplementedError, "subclasses must implement" | |
| 57 | |
| 58 def should_continue_work_queue(self): | |
| 59 raise NotImplementedError, "subclasses must implement" | |
| 60 | |
| 61 def next_work_item(self): | |
| 62 raise NotImplementedError, "subclasses must implement" | |
| 63 | |
| 64 def process_work_item(self, work_item): | |
| 65 raise NotImplementedError, "subclasses must implement" | |
| 66 | |
| 67 def handle_unexpected_error(self, work_item, message): | |
| 68 raise NotImplementedError, "subclasses must implement" | |
| 69 | |
| 70 | |
| 71 class QueueEngine: | |
| 72 def __init__(self, name, delegate, wakeup_event, seconds_to_sleep=120): | |
| 73 self._name = name | |
| 74 self._delegate = delegate | |
| 75 self._wakeup_event = wakeup_event | |
| 76 self._output_tee = OutputTee() | |
| 77 self._seconds_to_sleep = seconds_to_sleep | |
| 78 | |
| 79 log_date_format = "%Y-%m-%d %H:%M:%S" | |
| 80 handled_error_code = 2 | |
| 81 | |
| 82 # Child processes exit with a special code to the parent queue process can d
etect the error was handled. | |
| 83 @classmethod | |
| 84 def exit_after_handled_error(cls, error): | |
| 85 _log.error(error) | |
| 86 sys.exit(cls.handled_error_code) | |
| 87 | |
| 88 def run(self): | |
| 89 self._begin_logging() | |
| 90 | |
| 91 self._delegate.begin_work_queue() | |
| 92 while (self._delegate.should_continue_work_queue()): | |
| 93 try: | |
| 94 self._ensure_work_log_closed() | |
| 95 work_item = self._delegate.next_work_item() | |
| 96 if not work_item: | |
| 97 self._sleep("No work item.") | |
| 98 continue | |
| 99 | |
| 100 # FIXME: Work logs should not depend on bug_id specificaly. | |
| 101 # This looks fixed, no? | |
| 102 self._open_work_log(work_item) | |
| 103 try: | |
| 104 if not self._delegate.process_work_item(work_item): | |
| 105 _log.warning("Unable to process work item.") | |
| 106 continue | |
| 107 except ScriptError, e: | |
| 108 # Use a special exit code to indicate that the error was alr
eady | |
| 109 # handled in the child process and we should just keep loopi
ng. | |
| 110 if e.exit_code == self.handled_error_code: | |
| 111 continue | |
| 112 message = "Unexpected failure when processing patch! Please
file a bug against webkit-patch.\n%s" % e.message_with_output() | |
| 113 self._delegate.handle_unexpected_error(work_item, message) | |
| 114 except TerminateQueue, e: | |
| 115 self._stopping("TerminateQueue exception received.") | |
| 116 return 0 | |
| 117 except KeyboardInterrupt, e: | |
| 118 self._stopping("User terminated queue.") | |
| 119 return 1 | |
| 120 except Exception, e: | |
| 121 traceback.print_exc() | |
| 122 # Don't try tell the status bot, in case telling it causes an ex
ception. | |
| 123 self._sleep("Exception while preparing queue") | |
| 124 self._stopping("Delegate terminated queue.") | |
| 125 return 0 | |
| 126 | |
| 127 def _stopping(self, message): | |
| 128 _log.info("\n%s" % message) | |
| 129 self._delegate.stop_work_queue(message) | |
| 130 # Be careful to shut down our OutputTee or the unit tests will be unhapp
y. | |
| 131 self._ensure_work_log_closed() | |
| 132 self._output_tee.remove_log(self._queue_log) | |
| 133 | |
| 134 def _begin_logging(self): | |
| 135 self._queue_log = self._output_tee.add_log(self._delegate.queue_log_path
()) | |
| 136 self._work_log = None | |
| 137 | |
| 138 def _open_work_log(self, work_item): | |
| 139 work_item_log_path = self._delegate.work_item_log_path(work_item) | |
| 140 if not work_item_log_path: | |
| 141 return | |
| 142 self._work_log = self._output_tee.add_log(work_item_log_path) | |
| 143 | |
| 144 def _ensure_work_log_closed(self): | |
| 145 # If we still have a bug log open, close it. | |
| 146 if self._work_log: | |
| 147 self._output_tee.remove_log(self._work_log) | |
| 148 self._work_log = None | |
| 149 | |
| 150 def _now(self): | |
| 151 """Overriden by the unit tests to allow testing _sleep_message""" | |
| 152 return datetime.now() | |
| 153 | |
| 154 def _sleep_message(self, message): | |
| 155 wake_time = self._now() + timedelta(seconds=self._seconds_to_sleep) | |
| 156 if self._seconds_to_sleep < 3 * 60: | |
| 157 sleep_duration_text = str(self._seconds_to_sleep) + ' seconds' | |
| 158 else: | |
| 159 sleep_duration_text = str(round(self._seconds_to_sleep / 60)) + ' mi
nutes' | |
| 160 return "%s Sleeping until %s (%s)." % (message, wake_time.strftime(self.
log_date_format), sleep_duration_text) | |
| 161 | |
| 162 def _sleep(self, message): | |
| 163 _log.info(self._sleep_message(message)) | |
| 164 self._wakeup_event.wait(self._seconds_to_sleep) | |
| 165 self._wakeup_event.clear() | |
| OLD | NEW |