Permalink
Browse files

fixed location of embedded squid binary + added sanity checking for r…

…obustness
  • Loading branch information...
1 parent b835d8a commit 413ce172496ff2829c57561075fb2b27a4ec85a3 @lirazsiri lirazsiri committed Aug 10, 2012
Showing with 30 additions and 11 deletions.
  1. +30 −11 squid.py
View
@@ -7,22 +7,28 @@
import errno
-def _find_free_port(port_from):
- def _is_listening(localport):
- sock = socket.socket()
- try:
- sock.connect(('127.0.0.1', localport))
- return True
- except socket.error, e:
- if e.errno == errno.ECONNREFUSED:
- return False
+PATH_DEPS = os.environ.get('TKLBAM_DEPS', '/usr/lib/tklbam/deps')
+SQUID_BIN = os.path.join(PATH_DEPS, "usr/sbin/tklbam-squid")
+
+def _is_listening(localport):
+ sock = socket.socket()
+ try:
+ sock.connect(('127.0.0.1', localport))
+ return True
+ except socket.error, e:
+ if e.errno == errno.ECONNREFUSED:
+ return False
+def _find_free_port(port_from):
while True:
if _is_listening(port_from) is False:
return port_from
port_from += 1
+class Error(Exception):
+ pass
+
class Squid:
def __init__(self, cache_size, cache_dir):
@@ -34,10 +40,23 @@ def __init__(self, cache_size, cache_dir):
def start(self):
os.environ['TKLBAM_SQUID_CACHE_DIR'] = self.cache_dir
- self.address = "127.0.0.1:%d" % _find_free_port(33128)
- self.command = command.Command(("/usr/local/sbin/tklbam-squid", self.address, self.cache_size),
+ localport = _find_free_port(33128)
+ self.address = "127.0.0.1:%d" % localport
+ self.command = command.Command((SQUID_BIN, self.address, self.cache_size),
setpgrp=True, pty=True)
+ def cb():
+ if _is_listening(localport):
+ continue_waiting = False
+ else:
+ continue_waiting = True
+
+ return continue_waiting
+
+ finished = self.command.wait(timeout=10, callback=cb)
+ if not self.command.running or not _is_listening(localport):
+ self.command.terminate()
+ raise Error("%s failed to start\n" % SQUID_BIN + self.command.output)
def stop(self):
if self.command:

0 comments on commit 413ce17

Please sign in to comment.