move static methods away from daemon class; restore initial logic
This commit is contained in:
parent
3b00f62d46
commit
690c862923
63
electrum
63
electrum
|
@ -74,7 +74,7 @@ from electrum.util import print_msg, print_stderr, json_encode, json_decode
|
||||||
from electrum.util import set_verbosity, InvalidPassword
|
from electrum.util import set_verbosity, InvalidPassword
|
||||||
from electrum.plugins import Plugins
|
from electrum.plugins import Plugins
|
||||||
from electrum.commands import get_parser, known_commands, Commands, config_variables
|
from electrum.commands import get_parser, known_commands, Commands, config_variables
|
||||||
from electrum.daemon import Daemon
|
from electrum import daemon
|
||||||
|
|
||||||
|
|
||||||
# get password routine
|
# get password routine
|
||||||
|
@ -313,48 +313,49 @@ if __name__ == '__main__':
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if cmdname == 'gui':
|
if cmdname == 'gui':
|
||||||
lockfile = Daemon.lockfile(config)
|
fd = daemon.get_fd_or_server(config)
|
||||||
fd = Daemon.get_fd_or_server(lockfile)
|
|
||||||
if isinstance(fd, int):
|
if isinstance(fd, int):
|
||||||
daemon = Daemon(config, fd)
|
d = daemon.Daemon(config, fd)
|
||||||
daemon.start()
|
d.start()
|
||||||
daemon.init_gui(config, plugins)
|
d.init_gui(config, plugins)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
server = fd
|
server = fd
|
||||||
result = server.gui(config_options)
|
result = server.gui(config_options)
|
||||||
|
|
||||||
elif cmdname == 'daemon':
|
elif cmdname == 'daemon':
|
||||||
lockfile = Daemon.lockfile(config)
|
subcommand = config.get('subcommand')
|
||||||
fd = Daemon.get_fd_or_server(lockfile)
|
assert subcommand in ['start', 'stop', 'status']
|
||||||
if isinstance(fd, int):
|
if subcommand == 'start':
|
||||||
subcommand = config.get('subcommand')
|
fd = daemon.get_fd_or_server(config)
|
||||||
assert subcommand in ['start', 'stop', 'status']
|
if isinstance(fd, int):
|
||||||
if subcommand != 'start':
|
pid = os.fork()
|
||||||
print_msg("Daemon not running")
|
if pid:
|
||||||
os.close(fd)
|
print_stderr("starting daemon (PID %d)" % pid)
|
||||||
Daemon.remove_lockfile(lockfile)
|
sys.exit(0)
|
||||||
sys.exit(1)
|
d = daemon.Daemon(config, fd)
|
||||||
pid = os.fork()
|
d.start()
|
||||||
if pid:
|
if config.get('websocket_server'):
|
||||||
print_stderr("starting daemon (PID %d)" % pid)
|
from electrum import websockets
|
||||||
|
websockets.WebSocketServer(config, d.network).start()
|
||||||
|
if config.get('requests_dir'):
|
||||||
|
util.check_www_dir(config.get('requests_dir'))
|
||||||
|
d.join()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
daemon = Daemon(config, fd)
|
else:
|
||||||
daemon.start()
|
server = fd
|
||||||
if config.get('websocket_server'):
|
result = server.daemon(config_options)
|
||||||
from electrum import websockets
|
|
||||||
websockets.WebSocketServer(config, daemon.network).start()
|
|
||||||
if config.get('requests_dir'):
|
|
||||||
util.check_www_dir(config.get('requests_dir'))
|
|
||||||
daemon.join()
|
|
||||||
sys.exit(0)
|
|
||||||
else:
|
else:
|
||||||
server = fd
|
server = daemon.get_server(config)
|
||||||
result = server.daemon(config_options)
|
if server is not None:
|
||||||
|
result = server.daemon(config_options)
|
||||||
|
else:
|
||||||
|
print_msg("Daemon not running")
|
||||||
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
# command line
|
# command line
|
||||||
init_cmdline(config_options)
|
init_cmdline(config_options)
|
||||||
server = Daemon.get_server(Daemon.lockfile(config))
|
server = daemon.get_server(config)
|
||||||
if server is not None:
|
if server is not None:
|
||||||
result = server.run_cmdline(config_options)
|
result = server.run_cmdline(config_options)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -33,6 +33,50 @@ from commands import known_commands, Commands
|
||||||
from simple_config import SimpleConfig
|
from simple_config import SimpleConfig
|
||||||
|
|
||||||
|
|
||||||
|
def get_lockfile(config):
|
||||||
|
return os.path.join(config.path, 'daemon')
|
||||||
|
|
||||||
|
def remove_lockfile(lockfile):
|
||||||
|
os.unlink(lockfile)
|
||||||
|
|
||||||
|
def get_fd_or_server(config):
|
||||||
|
'''Tries to create the lockfile, using O_EXCL to
|
||||||
|
prevent races. If it succeeds it returns the FD.
|
||||||
|
Otherwise try and connect to the server specified in the lockfile.
|
||||||
|
If this succeeds, the server is returned. Otherwise remove the
|
||||||
|
lockfile and try again.'''
|
||||||
|
lockfile = get_lockfile(config)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
server = get_server(config)
|
||||||
|
if server is not None:
|
||||||
|
return server
|
||||||
|
# Couldn't connect; remove lockfile and try again.
|
||||||
|
remove_lockfile(lockfile)
|
||||||
|
|
||||||
|
def get_server(config):
|
||||||
|
lockfile = get_lockfile(config)
|
||||||
|
while True:
|
||||||
|
create_time = None
|
||||||
|
try:
|
||||||
|
with open(lockfile) as f:
|
||||||
|
(host, port), create_time = ast.literal_eval(f.read())
|
||||||
|
server = jsonrpclib.Server('http://%s:%d' % (host, port))
|
||||||
|
# Test daemon is running
|
||||||
|
server.ping()
|
||||||
|
return server
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if not create_time or create_time < time.time() - 1.0:
|
||||||
|
return None
|
||||||
|
# Sleep a bit and try again; it might have just been started
|
||||||
|
time.sleep(1.0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class RequestHandler(SimpleJSONRPCRequestHandler):
|
class RequestHandler(SimpleJSONRPCRequestHandler):
|
||||||
|
|
||||||
def do_OPTIONS(self):
|
def do_OPTIONS(self):
|
||||||
|
@ -172,7 +216,7 @@ class Daemon(DaemonThread):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.print_error("stopping, removing lockfile")
|
self.print_error("stopping, removing lockfile")
|
||||||
Daemon.remove_lockfile(Daemon.lockfile(self.config))
|
remove_lockfile(get_lockfile(self.config))
|
||||||
DaemonThread.stop(self)
|
DaemonThread.stop(self)
|
||||||
|
|
||||||
def init_gui(self, config, plugins):
|
def init_gui(self, config, plugins):
|
||||||
|
@ -182,49 +226,3 @@ class Daemon(DaemonThread):
|
||||||
gui = __import__('electrum_gui.' + gui_name, fromlist=['electrum_gui'])
|
gui = __import__('electrum_gui.' + gui_name, fromlist=['electrum_gui'])
|
||||||
self.gui = gui.ElectrumGui(config, self, plugins)
|
self.gui = gui.ElectrumGui(config, self, plugins)
|
||||||
self.gui.main()
|
self.gui.main()
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def lockfile(config):
|
|
||||||
return os.path.join(config.path, 'daemon')
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def remove_lockfile(lockfile):
|
|
||||||
os.unlink(lockfile)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_fd_or_server(lockfile):
|
|
||||||
'''If create is True, tries to create the lockfile, using O_EXCL to
|
|
||||||
prevent races. If it succeeds it returns the FD.
|
|
||||||
|
|
||||||
Otherwise try and connect to the server specified in the lockfile.
|
|
||||||
If this succeeds, the server is returned. Otherwise remove the
|
|
||||||
lockfile and try again.'''
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
server = Daemon.get_server(lockfile)
|
|
||||||
if server is not None:
|
|
||||||
return server
|
|
||||||
# Couldn't connect; remove lockfile and try again.
|
|
||||||
Daemon.remove_lockfile(lockfile)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_server(lockfile):
|
|
||||||
while True:
|
|
||||||
create_time = None
|
|
||||||
try:
|
|
||||||
with open(lockfile) as f:
|
|
||||||
(host, port), create_time = ast.literal_eval(f.read())
|
|
||||||
server = jsonrpclib.Server('http://%s:%d' % (host, port))
|
|
||||||
# Test daemon is running
|
|
||||||
server.ping()
|
|
||||||
return server
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
if not create_time or create_time < time.time() - 1.0:
|
|
||||||
return None
|
|
||||||
# Sleep a bit and try again; it might have just been started
|
|
||||||
time.sleep(1.0)
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue