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.outputtee import OutputTee
39 _log = logging.getLogger(__name__)
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 not recommended).
44 class TerminateQueue(Exception):
48 class QueueEngineDelegate:
49 def queue_log_path(self):
50 raise NotImplementedError, "subclasses must implement"
52 def work_item_log_path(self, work_item):
53 raise NotImplementedError, "subclasses must implement"
55 def begin_work_queue(self):
56 raise NotImplementedError, "subclasses must implement"
58 def should_continue_work_queue(self):
59 raise NotImplementedError, "subclasses must implement"
61 def next_work_item(self):
62 raise NotImplementedError, "subclasses must implement"
64 def process_work_item(self, work_item):
65 raise NotImplementedError, "subclasses must implement"
67 def handle_unexpected_error(self, work_item, message):
68 raise NotImplementedError, "subclasses must implement"
72 def __init__(self, name, delegate, wakeup_event):
74 self._delegate = delegate
75 self._wakeup_event = wakeup_event
76 self._output_tee = OutputTee()
78 log_date_format = "%Y-%m-%d %H:%M:%S"
79 sleep_duration_text = "2 mins" # This could be generated from seconds_to_sleep
80 seconds_to_sleep = 120
81 handled_error_code = 2
83 # Child processes exit with a special code to the parent queue process can detect the error was handled.
85 def exit_after_handled_error(cls, error):
87 sys.exit(cls.handled_error_code)
92 self._delegate.begin_work_queue()
93 while (self._delegate.should_continue_work_queue()):
95 self._ensure_work_log_closed()
96 work_item = self._delegate.next_work_item()
98 self._sleep("No work item.")
101 # FIXME: Work logs should not depend on bug_id specificaly.
102 # This looks fixed, no?
103 self._open_work_log(work_item)
105 if not self._delegate.process_work_item(work_item):
106 _log.warning("Unable to process work item.")
108 except ScriptError, e:
109 # Use a special exit code to indicate that the error was already
110 # handled in the child process and we should just keep looping.
111 if e.exit_code == self.handled_error_code:
113 message = "Unexpected failure when processing patch! Please file a bug against webkit-patch.\n%s" % e.message_with_output()
114 self._delegate.handle_unexpected_error(work_item, message)
115 except TerminateQueue, e:
116 self._stopping("TerminateQueue exception received.")
118 except KeyboardInterrupt, e:
119 self._stopping("User terminated queue.")
122 traceback.print_exc()
123 # Don't try tell the status bot, in case telling it causes an exception.
124 self._sleep("Exception while preparing queue")
125 self._stopping("Delegate terminated queue.")
128 def _stopping(self, message):
129 _log.info("\n%s" % message)
130 self._delegate.stop_work_queue(message)
131 # Be careful to shut down our OutputTee or the unit tests will be unhappy.
132 self._ensure_work_log_closed()
133 self._output_tee.remove_log(self._queue_log)
135 def _begin_logging(self):
136 self._queue_log = self._output_tee.add_log(self._delegate.queue_log_path())
137 self._work_log = None
139 def _open_work_log(self, work_item):
140 work_item_log_path = self._delegate.work_item_log_path(work_item)
141 if not work_item_log_path:
143 self._work_log = self._output_tee.add_log(work_item_log_path)
145 def _ensure_work_log_closed(self):
146 # If we still have a bug log open, close it.
148 self._output_tee.remove_log(self._work_log)
149 self._work_log = None
152 """Overriden by the unit tests to allow testing _sleep_message"""
153 return datetime.now()
155 def _sleep_message(self, message):
156 wake_time = self._now() + timedelta(seconds=self.seconds_to_sleep)
157 return "%s Sleeping until %s (%s)." % (message, wake_time.strftime(self.log_date_format), self.sleep_duration_text)
159 def _sleep(self, message):
160 _log.info(self._sleep_message(message))
161 self._wakeup_event.wait(self.seconds_to_sleep)
162 self._wakeup_event.clear()