move start_daemon code to main script

This commit is contained in:
ThomasV 2014-03-11 08:33:58 +01:00
parent e6bbf22003
commit 489413d358
3 changed files with 31 additions and 37 deletions

View file

@ -108,33 +108,49 @@ def print_help_cb(self, opt, value, parser):
def run_command(cmd, password=None, args=[]):
import xmlrpclib, socket
cmd_runner = Commands(wallet, network)
func = getattr(cmd_runner, cmd.name)
cmd_runner.password = password
import socket
if cmd.requires_network and not options.offline:
daemon_started = False
while True:
try:
cmd_runner.network = NetworkProxy(config)
cmd_runner.network.start()
network = NetworkProxy(config)
break
except socket.error:
if cmd.name != 'daemon':
print "Starting daemon [%s]"%config.get('server')
start_daemon(config)
if cmd != 'daemon':
if not daemon_started:
print "Starting daemon [%s]"%config.get('server')
daemon_started = True
pid = os.fork()
if (pid == 0): # The first child.
os.chdir("/")
os.setsid()
os.umask(0)
pid2 = os.fork()
if (pid2 == 0): # Second child
server = NetworkServer(config)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
sys.exit(1)
sys.exit(0)
else:
time.sleep(0.1)
else:
print "Daemon not running"
sys.exit(1)
network = network
network.start()
if wallet:
wallet.start_threads(cmd_runner.network)
wallet.start_threads(network)
wallet.update()
else:
cmd_runner.network = None
network = None
cmd_runner = Commands(wallet, network)
func = getattr(cmd_runner, cmd.name)
cmd_runner.password = password
try:
result = func(*args[1:])
except Exception:

View file

@ -14,4 +14,4 @@ from plugins import BasePlugin
from mnemonic import mn_encode as mnemonic_encode
from mnemonic import mn_decode as mnemonic_decode
from commands import Commands, known_commands
from daemon import start_daemon, NetworkProxy
from daemon import NetworkProxy, NetworkServer

View file

@ -295,28 +295,6 @@ class NetworkServer:
def start_daemon(config):
pid = os.fork()
if (pid == 0): # The first child.
os.chdir("/")
os.setsid()
os.umask(0)
pid2 = os.fork()
if (pid2 == 0): # Second child
server = NetworkServer(config)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
sys.exit(1)
sys.exit(0)
# should use a signal
time.sleep(2)
if __name__ == '__main__':
import simple_config
config = simple_config.SimpleConfig({'verbose':True, 'server':'ecdsa.net:50002:s'})