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
1 changed files with 12 additions and 7 deletions

View File

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