restart tor daemon if its no longer running

This commit is contained in:
j 2016-01-24 18:01:42 +05:30
parent ac3dd595cf
commit ea40e64b9c

View file

@ -2,6 +2,7 @@ import os
import re import re
import sys import sys
import subprocess import subprocess
import time
from threading import Thread from threading import Thread
import distutils import distutils
@ -20,6 +21,7 @@ logger = logging.getLogger(__name__)
class TorDaemon(Thread): class TorDaemon(Thread):
installing = False installing = False
running = True
def __init__(self): def __init__(self):
self._status = [] self._status = []
@ -69,14 +71,17 @@ DirReqStatistics 0
tor = get_tor() tor = get_tor()
if tor: if tor:
cmd = [tor, '--defaults-torrc', defaults, '-f', torrc] cmd = [tor, '--defaults-torrc', defaults, '-f', torrc]
while self.running:
self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1, self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1,
universal_newlines=True, close_fds=True, start_new_session=True) universal_newlines=True, close_fds=True, start_new_session=True)
for line in self.p.stdout: for line in self.p.stdout:
self._status.append(line) self._status.append(line)
logger.debug(line) logger.debug(line)
time.sleep(0.5)
self.p = None self.p = None
def shutdown(self): def kill(self):
self.running = False
if self.p: if self.p:
self.p.kill() self.p.kill()
@ -154,7 +159,7 @@ class Tor(object):
#self.controller.remove_event_listener(self.connection_change) #self.controller.remove_event_listener(self.connection_change)
self.controller.close() self.controller.close()
if self.daemon: if self.daemon:
self.daemon.shutdown() self.daemon.kill()
except: except:
logger.debug('shutdown exception', exc_info=True) logger.debug('shutdown exception', exc_info=True)
pass pass