hooks: generate bind port before init of TcpServer for better logging.
This commit is contained in:
parent
b19bdaa108
commit
74efadbc57
1 changed files with 22 additions and 7 deletions
|
|
@ -24,6 +24,7 @@ import logging
|
|||
import tempfile
|
||||
import traceback
|
||||
import threading
|
||||
import socket
|
||||
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler
|
||||
from SocketServer import TCPServer
|
||||
|
|
@ -152,20 +153,34 @@ class HttpHooksCallbackDaemon(ThreadedHookCallbackDaemon):
|
|||
# request and wastes cpu at all other times.
|
||||
POLL_INTERVAL = 0.01
|
||||
|
||||
def get_available_port():
|
||||
family = socket.AF_INET
|
||||
socktype = socket.SOCK_STREAM
|
||||
host = '127.0.0.1'
|
||||
|
||||
mysocket = socket.socket(family, socktype)
|
||||
mysocket.bind((host, 0))
|
||||
port = mysocket.getsockname()[1]
|
||||
mysocket.close()
|
||||
del mysocket
|
||||
return port
|
||||
|
||||
def _prepare(self, txn_id=None, host=None, port=None):
|
||||
|
||||
host = host or '127.0.0.1'
|
||||
self._done = False
|
||||
port = port or self.get_available_port()
|
||||
server_address = (host, port)
|
||||
self.hooks_uri = '{}:{}'.format(host, port)
|
||||
self.txn_id = txn_id
|
||||
self._done = False
|
||||
|
||||
log.debug(
|
||||
"Preparing HTTP callback daemon at `%s` and registering hook object: %s",
|
||||
self.hooks_uri, HooksHttpHandler)
|
||||
|
||||
self._daemon = TCPServer(server_address, HooksHttpHandler)
|
||||
# inject transaction_id for later verification
|
||||
self._daemon.txn_id = self.txn_id
|
||||
log.debug(
|
||||
"Preparing HTTP callback daemon at `%s` and registering hook object",
|
||||
self.hooks_uri)
|
||||
|
||||
self._daemon = TCPServer((host, port or 0), HooksHttpHandler)
|
||||
_, port = self._daemon.server_address
|
||||
|
||||
def _run(self):
|
||||
log.debug("Running event loop of callback daemon in background thread")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue