##// END OF EJS Templates
hooks: generate bind port before init of TcpServer for better logging.
super-admin -
r4855:2a635a31 default
parent child Browse files
Show More
@@ -24,6 +24,7 b' import logging'
24 24 import tempfile
25 25 import traceback
26 26 import threading
27 import socket
27 28
28 29 from BaseHTTPServer import BaseHTTPRequestHandler
29 30 from SocketServer import TCPServer
@@ -152,20 +153,34 b' class HttpHooksCallbackDaemon(ThreadedHo'
152 153 # request and wastes cpu at all other times.
153 154 POLL_INTERVAL = 0.01
154 155
156 def get_available_port():
157 family = socket.AF_INET
158 socktype = socket.SOCK_STREAM
159 host = '127.0.0.1'
160
161 mysocket = socket.socket(family, socktype)
162 mysocket.bind((host, 0))
163 port = mysocket.getsockname()[1]
164 mysocket.close()
165 del mysocket
166 return port
167
155 168 def _prepare(self, txn_id=None, host=None, port=None):
156 169
157 170 host = host or '127.0.0.1'
158 self._done = False
171 port = port or self.get_available_port()
172 server_address = (host, port)
159 173 self.hooks_uri = '{}:{}'.format(host, port)
160 174 self.txn_id = txn_id
175 self._done = False
176
177 log.debug(
178 "Preparing HTTP callback daemon at `%s` and registering hook object: %s",
179 self.hooks_uri, HooksHttpHandler)
180
181 self._daemon = TCPServer(server_address, HooksHttpHandler)
161 182 # inject transaction_id for later verification
162 183 self._daemon.txn_id = self.txn_id
163 log.debug(
164 "Preparing HTTP callback daemon at `%s` and registering hook object",
165 self.hooks_uri)
166
167 self._daemon = TCPServer((host, port or 0), HooksHttpHandler)
168 _, port = self._daemon.server_address
169 184
170 185 def _run(self):
171 186 log.debug("Running event loop of callback daemon in background thread")
General Comments 0
You need to be logged in to leave comments. Login now