|
@@ -65,46 +65,53 @@ class Rdb(Pdb):
|
|
_sock = None
|
|
_sock = None
|
|
|
|
|
|
def __init__(self, host=CELERY_RDB_HOST, port=CELERY_RDB_PORT,
|
|
def __init__(self, host=CELERY_RDB_HOST, port=CELERY_RDB_PORT,
|
|
- port_search_limit=100, port_skew=+0):
|
|
|
|
|
|
+ port_search_limit=100, port_skew=+0, out=sys.stdout):
|
|
self.active = True
|
|
self.active = True
|
|
|
|
+ self.out = out
|
|
|
|
|
|
|
|
+ self._prev_handles = sys.stdin, sys.stdout
|
|
|
|
+
|
|
|
|
+ self._sock, this_port = self.get_avail_port(host, port,
|
|
|
|
+ port_search_limit, port_skew)
|
|
|
|
+ self._sock.listen(1)
|
|
|
|
+ me = "%s:%s" % (self.me, this_port)
|
|
|
|
+ context = self.context = {"me": me, "host": host, "port": this_port}
|
|
|
|
+ self.say("%(me)s: Please telnet %(host)s %(port)s."
|
|
|
|
+ " Type `exit` in session to continue." % context)
|
|
|
|
+ self.say("%(me)s: Waiting for client..." % context)
|
|
|
|
+
|
|
|
|
+ self._client, address = self._sock.accept()
|
|
|
|
+ context["remote_addr"] = ":".join(map(str, address))
|
|
|
|
+ self.say("%(me)s: In session with %(remote_addr)s" % context)
|
|
|
|
+ self._handle = sys.stdin = sys.stdout = self._client.makefile("rw")
|
|
|
|
+ Pdb.__init__(self, completekey="tab",
|
|
|
|
+ stdin=self._handle, stdout=self._handle)
|
|
|
|
+
|
|
|
|
+ def get_avail_port(self, host, port, search_limit=100, skew=+0):
|
|
try:
|
|
try:
|
|
- _, port_skew = current_process().name.split('-')
|
|
|
|
- port_skew = int(port_skew)
|
|
|
|
|
|
+ _, skew = current_process().name.split('-')
|
|
|
|
+ skew = int(skew)
|
|
except ValueError:
|
|
except ValueError:
|
|
pass
|
|
pass
|
|
-
|
|
|
|
- self._prev_handles = sys.stdin, sys.stdout
|
|
|
|
this_port = None
|
|
this_port = None
|
|
- for i in xrange(port_search_limit):
|
|
|
|
- self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
- this_port = port + port_skew + i
|
|
|
|
|
|
+ for i in xrange(search_limit):
|
|
|
|
+ _sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
+ this_port = port + skew + i
|
|
try:
|
|
try:
|
|
- self._sock.bind((host, this_port))
|
|
|
|
|
|
+ _sock.bind((host, this_port))
|
|
except socket.error, exc:
|
|
except socket.error, exc:
|
|
if exc.errno in [errno.EADDRINUSE, errno.EINVAL]:
|
|
if exc.errno in [errno.EADDRINUSE, errno.EINVAL]:
|
|
continue
|
|
continue
|
|
raise
|
|
raise
|
|
else:
|
|
else:
|
|
- break
|
|
|
|
|
|
+ return _sock, this_port
|
|
else:
|
|
else:
|
|
raise Exception(
|
|
raise Exception(
|
|
"%s: Could not find available port. Please set using "
|
|
"%s: Could not find available port. Please set using "
|
|
"environment variable CELERY_RDB_PORT" % (self.me, ))
|
|
"environment variable CELERY_RDB_PORT" % (self.me, ))
|
|
|
|
|
|
- self._sock.listen(1)
|
|
|
|
- me = "%s:%s" % (self.me, this_port)
|
|
|
|
- context = self.context = {"me": me, "host": host, "port": this_port}
|
|
|
|
- print("%(me)s: Please telnet %(host)s %(port)s."
|
|
|
|
- " Type `exit` in session to continue." % context)
|
|
|
|
- print("%(me)s: Waiting for client..." % context)
|
|
|
|
-
|
|
|
|
- self._client, address = self._sock.accept()
|
|
|
|
- context["remote_addr"] = ":".join(map(str, address))
|
|
|
|
- print("%(me)s: In session with %(remote_addr)s" % context)
|
|
|
|
- self._handle = sys.stdin = sys.stdout = self._client.makefile("rw")
|
|
|
|
- Pdb.__init__(self, completekey="tab",
|
|
|
|
- stdin=self._handle, stdout=self._handle)
|
|
|
|
|
|
+ def say(self, m):
|
|
|
|
+ self.out.write(m + "\n")
|
|
|
|
|
|
def _close_session(self):
|
|
def _close_session(self):
|
|
self.stdin, self.stdout = sys.stdin, sys.stdout = self._prev_handles
|
|
self.stdin, self.stdout = sys.stdin, sys.stdout = self._prev_handles
|
|
@@ -112,7 +119,7 @@ class Rdb(Pdb):
|
|
self._client.close()
|
|
self._client.close()
|
|
self._sock.close()
|
|
self._sock.close()
|
|
self.active = False
|
|
self.active = False
|
|
- print("%(me)s: Session %(remote_addr)s ended." % self.context)
|
|
|
|
|
|
+ self.say("%(me)s: Session %(remote_addr)s ended." % self.context)
|
|
|
|
|
|
def do_continue(self, arg):
|
|
def do_continue(self, arg):
|
|
self._close_session()
|
|
self._close_session()
|