electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 96070246b72acb4c97d8e0726c636cea3adc4fea
parent 374efd970e8248331c1dc08d666c1674d68b0e4e
Author: ThomasV <thomasv@gitorious>
Date:   Fri, 25 Jul 2014 15:42:28 +0200

proper start and stop commands for daemon

Diffstat:
Melectrum | 28+++++++++++++++++++---------
Mlib/daemon.py | 16++++++++++++++--
Mlib/network_proxy.py | 3+++
3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/electrum b/electrum @@ -146,6 +146,12 @@ def run_command(cmd, password=None, args=None): 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): import socket @@ -161,17 +167,12 @@ def daemon_socket(start_daemon=True): if not start_daemon: return False elif not daemon_started: - import subprocess - 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 + do_start_daemon() daemon_started = True else: time.sleep(0.1) - - if __name__ == '__main__': wallet = None @@ -242,8 +243,7 @@ if __name__ == '__main__': sys.exit(0) if cmd == 'daemon': - arg = args[1] - if arg=='start': + if len(args) == 1: from electrum import daemon, util util.set_verbosity(True) print_stderr( "Starting daemon [%s]"%config.get('server')) @@ -253,8 +253,18 @@ if __name__ == '__main__': except KeyboardInterrupt: print_msg("Ctrl C - Stopping server") sys.exit(0) + 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) + if arg == 'start': + if s: + print_msg("Daemon already running") + sys.exit(1) + do_start_daemon() + sys.exit(0) elif arg in ['status','stop']: - s = daemon_socket(start_daemon=False) if not s: print_msg("Daemon not running") sys.exit(1) diff --git a/lib/daemon.py b/lib/daemon.py @@ -72,7 +72,11 @@ class ClientThread(threading.Thread): message = '' while True: - self.send_responses() + try: + self.send_responses() + except socket.error: + break + try: data = self.s.recv(1024) except socket.timeout: @@ -100,6 +104,10 @@ class ClientThread(threading.Thread): params = request['params'] _id = request['id'] + if method == ('daemon.stop'): + self.server.stop() + return + if method.startswith('network.'): out = {'id':_id} try: @@ -168,6 +176,9 @@ class NetworkServer: self.clients = [] # daemon needs to know which client subscribed to which address + def stop(self): + with self.lock: + self.running = False def add_client(self, client): for key in ['status','banner','updated','servers','interfaces']: @@ -210,6 +221,7 @@ class NetworkServer: except socket.timeout: if not self.clients: if time.time() - t > self.timeout: + print_error("Daemon timeout") break else: t = time.time() @@ -217,8 +229,8 @@ class NetworkServer: t = time.time() client = ClientThread(self, self.network, connection) client.start() + print_error("Daemon exiting") - print_error("Daemon exiting (timeout)") diff --git a/lib/network_proxy.py b/lib/network_proxy.py @@ -196,6 +196,9 @@ class NetworkProxy(threading.Thread): def stop(self): self.running = False + def stop_daemon(self): + return self.send([('daemon.stop',[])], None) + def register_callback(self, event, callback): with self.lock: if not self.callbacks.get(event):