mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-28 16:01:30 +00:00
proper start and stop commands for daemon
This commit is contained in:
parent
374efd970e
commit
96070246b7
3 changed files with 36 additions and 11 deletions
28
electrum
28
electrum
|
@ -146,6 +146,12 @@ def run_command(cmd, password=None, args=None):
|
||||||
print_json(result)
|
print_json(result)
|
||||||
|
|
||||||
|
|
||||||
|
def do_start_daemon():
|
||||||
|
import subprocess
|
||||||
|
logfile = open(os.path.join(config.path, 'daemon.log'),'w')
|
||||||
|
p = subprocess.Popen([__file__,"daemon"], stderr=logfile, stdout=logfile, close_fds=True)
|
||||||
|
print "starting daemon (PID %d)"%p.pid
|
||||||
|
|
||||||
|
|
||||||
def daemon_socket(start_daemon=True):
|
def daemon_socket(start_daemon=True):
|
||||||
import socket
|
import socket
|
||||||
|
@ -161,17 +167,12 @@ def daemon_socket(start_daemon=True):
|
||||||
if not start_daemon:
|
if not start_daemon:
|
||||||
return False
|
return False
|
||||||
elif not daemon_started:
|
elif not daemon_started:
|
||||||
import subprocess
|
do_start_daemon()
|
||||||
logfile = open(os.path.join(config.path, 'daemon.log'),'w')
|
|
||||||
p = subprocess.Popen([__file__,"daemon","start"], stderr=logfile, stdout=logfile, close_fds=True)
|
|
||||||
print "starting daemon (PID %d)"%p.pid
|
|
||||||
daemon_started = True
|
daemon_started = True
|
||||||
else:
|
else:
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
wallet = None
|
wallet = None
|
||||||
|
@ -242,8 +243,7 @@ if __name__ == '__main__':
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if cmd == 'daemon':
|
if cmd == 'daemon':
|
||||||
arg = args[1]
|
if len(args) == 1:
|
||||||
if arg=='start':
|
|
||||||
from electrum import daemon, util
|
from electrum import daemon, util
|
||||||
util.set_verbosity(True)
|
util.set_verbosity(True)
|
||||||
print_stderr( "Starting daemon [%s]"%config.get('server'))
|
print_stderr( "Starting daemon [%s]"%config.get('server'))
|
||||||
|
@ -253,8 +253,18 @@ if __name__ == '__main__':
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print_msg("Ctrl C - Stopping server")
|
print_msg("Ctrl C - Stopping server")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif arg in ['status','stop']:
|
arg = args[1]
|
||||||
|
if arg not in ['start', 'stop', 'status']:
|
||||||
|
print_msg("syntax: electrum daemon <start|status|stop>")
|
||||||
|
sys.exit(1)
|
||||||
s = daemon_socket(start_daemon=False)
|
s = daemon_socket(start_daemon=False)
|
||||||
|
if arg == 'start':
|
||||||
|
if s:
|
||||||
|
print_msg("Daemon already running")
|
||||||
|
sys.exit(1)
|
||||||
|
do_start_daemon()
|
||||||
|
sys.exit(0)
|
||||||
|
elif arg in ['status','stop']:
|
||||||
if not s:
|
if not s:
|
||||||
print_msg("Daemon not running")
|
print_msg("Daemon not running")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
|
@ -72,7 +72,11 @@ class ClientThread(threading.Thread):
|
||||||
|
|
||||||
message = ''
|
message = ''
|
||||||
while True:
|
while True:
|
||||||
|
try:
|
||||||
self.send_responses()
|
self.send_responses()
|
||||||
|
except socket.error:
|
||||||
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = self.s.recv(1024)
|
data = self.s.recv(1024)
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
|
@ -100,6 +104,10 @@ class ClientThread(threading.Thread):
|
||||||
params = request['params']
|
params = request['params']
|
||||||
_id = request['id']
|
_id = request['id']
|
||||||
|
|
||||||
|
if method == ('daemon.stop'):
|
||||||
|
self.server.stop()
|
||||||
|
return
|
||||||
|
|
||||||
if method.startswith('network.'):
|
if method.startswith('network.'):
|
||||||
out = {'id':_id}
|
out = {'id':_id}
|
||||||
try:
|
try:
|
||||||
|
@ -168,6 +176,9 @@ class NetworkServer:
|
||||||
self.clients = []
|
self.clients = []
|
||||||
# daemon needs to know which client subscribed to which address
|
# daemon needs to know which client subscribed to which address
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
with self.lock:
|
||||||
|
self.running = False
|
||||||
|
|
||||||
def add_client(self, client):
|
def add_client(self, client):
|
||||||
for key in ['status','banner','updated','servers','interfaces']:
|
for key in ['status','banner','updated','servers','interfaces']:
|
||||||
|
@ -210,6 +221,7 @@ class NetworkServer:
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
if not self.clients:
|
if not self.clients:
|
||||||
if time.time() - t > self.timeout:
|
if time.time() - t > self.timeout:
|
||||||
|
print_error("Daemon timeout")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
t = time.time()
|
t = time.time()
|
||||||
|
@ -217,8 +229,8 @@ class NetworkServer:
|
||||||
t = time.time()
|
t = time.time()
|
||||||
client = ClientThread(self, self.network, connection)
|
client = ClientThread(self, self.network, connection)
|
||||||
client.start()
|
client.start()
|
||||||
|
print_error("Daemon exiting")
|
||||||
|
|
||||||
print_error("Daemon exiting (timeout)")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -196,6 +196,9 @@ class NetworkProxy(threading.Thread):
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
|
def stop_daemon(self):
|
||||||
|
return self.send([('daemon.stop',[])], None)
|
||||||
|
|
||||||
def register_callback(self, event, callback):
|
def register_callback(self, event, callback):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
if not self.callbacks.get(event):
|
if not self.callbacks.get(event):
|
||||||
|
|
Loading…
Add table
Reference in a new issue