OLD | NEW |
1 # Copyright (C) 2011 Google Inc. All rights reserved. | 1 # Copyright (C) 2011 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 self._cwd = None | 75 self._cwd = None |
76 self._stdout = self._executive.PIPE | 76 self._stdout = self._executive.PIPE |
77 self._stderr = self._executive.PIPE | 77 self._stderr = self._executive.PIPE |
78 self._process = None | 78 self._process = None |
79 self._pid = None | 79 self._pid = None |
80 self._error_log_path = None | 80 self._error_log_path = None |
81 | 81 |
82 def start(self): | 82 def start(self): |
83 """Starts the server. It is an error to start an already started server. | 83 """Starts the server. It is an error to start an already started server. |
84 | 84 |
85 This method also stops any stale servers started by a previous instance.
""" | 85 This method also stops any stale servers started by a previous instance. |
| 86 """ |
86 assert not self._pid, '%s server is already running' % self._name | 87 assert not self._pid, '%s server is already running' % self._name |
87 | 88 |
88 # Stop any stale servers left over from previous instances. | 89 # Stop any stale servers left over from previous instances. |
89 if self._filesystem.exists(self._pid_file): | 90 if self._filesystem.exists(self._pid_file): |
90 try: | 91 try: |
91 self._pid = int(self._filesystem.read_text_file(self._pid_file)) | 92 self._pid = int(self._filesystem.read_text_file(self._pid_file)) |
92 _log.debug('stale %s pid file, pid %d', self._name, self._pid) | 93 _log.debug('stale %s pid file, pid %d', self._name, self._pid) |
93 self._stop_running_server() | 94 self._stop_running_server() |
94 except (ValueError, UnicodeDecodeError): | 95 except (ValueError, UnicodeDecodeError): |
95 # These could be raised if the pid file is corrupt. | 96 # These could be raised if the pid file is corrupt. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 _log.debug("Attempting to shut down %s server at pid %d", self._name
, self._pid) | 140 _log.debug("Attempting to shut down %s server at pid %d", self._name
, self._pid) |
140 self._stop_running_server() | 141 self._stop_running_server() |
141 _log.debug("%s server at pid %d stopped", self._name, self._pid) | 142 _log.debug("%s server at pid %d stopped", self._name, self._pid) |
142 self._pid = None | 143 self._pid = None |
143 finally: | 144 finally: |
144 # Make sure we delete the pid file no matter what happens. | 145 # Make sure we delete the pid file no matter what happens. |
145 self._remove_pid_file() | 146 self._remove_pid_file() |
146 | 147 |
147 def _prepare_config(self): | 148 def _prepare_config(self): |
148 """This routine can be overridden by subclasses to do any sort | 149 """This routine can be overridden by subclasses to do any sort |
149 of initialization required prior to starting the server that may fail.""
" | 150 of initialization required prior to starting the server that may fail. |
| 151 """ |
150 | 152 |
151 def _remove_stale_logs(self): | 153 def _remove_stale_logs(self): |
152 """This routine can be overridden by subclasses to try and remove logs | 154 """This routine can be overridden by subclasses to try and remove logs |
153 left over from a prior run. This routine should log warnings if the | 155 left over from a prior run. This routine should log warnings if the |
154 files cannot be deleted, but should not fail unless failure to | 156 files cannot be deleted, but should not fail unless failure to |
155 delete the logs will actually cause start() to fail.""" | 157 delete the logs will actually cause start() to fail. |
| 158 """ |
156 # Sometimes logs are open in other processes but they should clear event
ually. | 159 # Sometimes logs are open in other processes but they should clear event
ually. |
157 for log_prefix in self._log_prefixes: | 160 for log_prefix in self._log_prefixes: |
158 try: | 161 try: |
159 self._remove_log_files(self._output_dir, log_prefix) | 162 self._remove_log_files(self._output_dir, log_prefix) |
160 except OSError: | 163 except OSError: |
161 _log.warning('Failed to remove old %s %s files', self._name, log
_prefix) | 164 _log.warning('Failed to remove old %s %s files', self._name, log
_prefix) |
162 | 165 |
163 def _spawn_process(self): | 166 def _spawn_process(self): |
164 _log.debug('Starting %s server, cmd="%s"', self._name, self._start_cmd) | 167 _log.debug('Starting %s server, cmd="%s"', self._name, self._start_cmd) |
165 self._process = self._executive.popen(self._start_cmd, | 168 self._process = self._executive.popen(self._start_cmd, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 for line in error_log_text.splitlines(): | 223 for line in error_log_text.splitlines(): |
221 _log.error(' %s', line) | 224 _log.error(' %s', line) |
222 else: | 225 else: |
223 _log.error('%s error log empty', self._name) | 226 _log.error('%s error log empty', self._name) |
224 _log.error('') | 227 _log.error('') |
225 else: | 228 else: |
226 _log.error('%s no error log', self._name) | 229 _log.error('%s no error log', self._name) |
227 | 230 |
228 def _wait_for_action(self, action, wait_secs=20.0, sleep_secs=1.0): | 231 def _wait_for_action(self, action, wait_secs=20.0, sleep_secs=1.0): |
229 """Repeat the action for wait_sec or until it succeeds, sleeping for sle
ep_secs | 232 """Repeat the action for wait_sec or until it succeeds, sleeping for sle
ep_secs |
230 in between each attempt. Returns whether it succeeded.""" | 233 in between each attempt. Returns whether it succeeded. |
| 234 """ |
231 start_time = time.time() | 235 start_time = time.time() |
232 while time.time() - start_time < wait_secs: | 236 while time.time() - start_time < wait_secs: |
233 if action(): | 237 if action(): |
234 return True | 238 return True |
235 _log.debug("Waiting for action: %s", action) | 239 _log.debug("Waiting for action: %s", action) |
236 time.sleep(sleep_secs) | 240 time.sleep(sleep_secs) |
237 | 241 |
238 return False | 242 return False |
239 | 243 |
240 def _is_server_running_on_all_ports(self): | 244 def _is_server_running_on_all_ports(self): |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 except IOError as e: | 276 except IOError as e: |
273 if e.errno in (errno.EALREADY, errno.EADDRINUSE): | 277 if e.errno in (errno.EALREADY, errno.EADDRINUSE): |
274 raise ServerError('Port %d is already in use.' % port) | 278 raise ServerError('Port %d is already in use.' % port) |
275 elif self._platform.is_win() and e.errno in (errno.WSAEACCES,):
# pylint: disable=E1101 | 279 elif self._platform.is_win() and e.errno in (errno.WSAEACCES,):
# pylint: disable=E1101 |
276 raise ServerError('Port %d is already in use.' % port) | 280 raise ServerError('Port %d is already in use.' % port) |
277 else: | 281 else: |
278 raise | 282 raise |
279 finally: | 283 finally: |
280 s.close() | 284 s.close() |
281 _log.debug('all ports are available') | 285 _log.debug('all ports are available') |
OLD | NEW |