initial import
[vuplus_webkit] / Tools / Scripts / webkitpy / tool / bot / queueengine.py
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.system.executive import ScriptError
37 from webkitpy.common.system.deprecated_logging import log, OutputTee
38
39
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):
43     pass
44
45
46 class QueueEngineDelegate:
47     def queue_log_path(self):
48         raise NotImplementedError, "subclasses must implement"
49
50     def work_item_log_path(self, work_item):
51         raise NotImplementedError, "subclasses must implement"
52
53     def begin_work_queue(self):
54         raise NotImplementedError, "subclasses must implement"
55
56     def should_continue_work_queue(self):
57         raise NotImplementedError, "subclasses must implement"
58
59     def next_work_item(self):
60         raise NotImplementedError, "subclasses must implement"
61
62     def should_proceed_with_work_item(self, work_item):
63         # returns (safe_to_proceed, waiting_message, patch)
64         raise NotImplementedError, "subclasses must implement"
65
66     def process_work_item(self, work_item):
67         raise NotImplementedError, "subclasses must implement"
68
69     def handle_unexpected_error(self, work_item, message):
70         raise NotImplementedError, "subclasses must implement"
71
72
73 class QueueEngine:
74     def __init__(self, name, delegate, wakeup_event):
75         self._name = name
76         self._delegate = delegate
77         self._wakeup_event = wakeup_event
78         self._output_tee = OutputTee()
79
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
84
85     # Child processes exit with a special code to the parent queue process can detect the error was handled.
86     @classmethod
87     def exit_after_handled_error(cls, error):
88         log(error)
89         exit(cls.handled_error_code)
90
91     def run(self):
92         self._begin_logging()
93
94         self._delegate.begin_work_queue()
95         while (self._delegate.should_continue_work_queue()):
96             try:
97                 self._ensure_work_log_closed()
98                 work_item = self._delegate.next_work_item()
99                 if not work_item:
100                     self._sleep("No work item.")
101                     continue
102                 if not self._delegate.should_proceed_with_work_item(work_item):
103                     self._sleep("Not proceeding with work item.")
104                     continue
105
106                 # FIXME: Work logs should not depend on bug_id specificaly.
107                 #        This looks fixed, no?
108                 self._open_work_log(work_item)
109                 try:
110                     if not self._delegate.process_work_item(work_item):
111                         log("Unable to process work item.")
112                         continue
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:
117                         continue
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.")
122                 return 0
123             except KeyboardInterrupt, e:
124                 self._stopping("User terminated queue.")
125                 return 1
126             except Exception, e:
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.")
131         return 0
132
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)
139
140     def _begin_logging(self):
141         self._queue_log = self._output_tee.add_log(self._delegate.queue_log_path())
142         self._work_log = None
143
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:
147             return
148         self._work_log = self._output_tee.add_log(work_item_log_path)
149
150     def _ensure_work_log_closed(self):
151         # If we still have a bug log open, close it.
152         if self._work_log:
153             self._output_tee.remove_log(self._work_log)
154             self._work_log = None
155
156     def _now(self):
157         """Overriden by the unit tests to allow testing _sleep_message"""
158         return datetime.now()
159
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)
163
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()