Daemon: Replace get_server with request.
This function performs a single RPC, and may raise a DaemonNotRunning exception.
This commit is contained in:
@@ -53,6 +53,8 @@ from .logging import get_logger, Logger
|
|||||||
|
|
||||||
_logger = get_logger(__name__)
|
_logger = get_logger(__name__)
|
||||||
|
|
||||||
|
class DaemonNotRunning(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
def get_lockfile(config: SimpleConfig):
|
def get_lockfile(config: SimpleConfig):
|
||||||
return os.path.join(config.path, 'daemon')
|
return os.path.join(config.path, 'daemon')
|
||||||
@@ -74,35 +76,39 @@ def get_file_descriptor(config: SimpleConfig):
|
|||||||
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
|
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
server = get_server(config)
|
try:
|
||||||
if server is not None:
|
request(config, 'ping')
|
||||||
return
|
return None
|
||||||
# Couldn't connect; remove lockfile and try again.
|
except DaemonNotRunning:
|
||||||
remove_lockfile(lockfile)
|
# Couldn't connect; remove lockfile and try again.
|
||||||
|
remove_lockfile(lockfile)
|
||||||
|
|
||||||
|
|
||||||
def get_server(config: SimpleConfig) -> Optional[jsonrpclib.Server]:
|
def request(config: SimpleConfig, endpoint, *args, **kwargs):
|
||||||
lockfile = get_lockfile(config)
|
lockfile = get_lockfile(config)
|
||||||
while True:
|
while True:
|
||||||
create_time = None
|
create_time = None
|
||||||
try:
|
try:
|
||||||
with open(lockfile) as f:
|
with open(lockfile) as f:
|
||||||
(host, port), create_time = ast.literal_eval(f.read())
|
(host, port), create_time = ast.literal_eval(f.read())
|
||||||
rpc_user, rpc_password = get_rpc_credentials(config)
|
rpc_user, rpc_password = get_rpc_credentials(config)
|
||||||
if rpc_password == '':
|
if rpc_password == '':
|
||||||
# authentication disabled
|
# authentication disabled
|
||||||
server_url = 'http://%s:%d' % (host, port)
|
server_url = 'http://%s:%d' % (host, port)
|
||||||
else:
|
else:
|
||||||
server_url = 'http://%s:%s@%s:%d' % (
|
server_url = 'http://%s:%s@%s:%d' % (
|
||||||
rpc_user, rpc_password, host, port)
|
rpc_user, rpc_password, host, port)
|
||||||
server = jsonrpclib.Server(server_url)
|
except Exception:
|
||||||
# Test daemon is running
|
raise DaemonNotRunning()
|
||||||
server.ping()
|
server = jsonrpclib.Server(server_url)
|
||||||
return server
|
try:
|
||||||
except Exception as e:
|
# run request
|
||||||
_logger.info(f"failed to connect to JSON-RPC server: {e}")
|
f = getattr(server, endpoint)
|
||||||
if not create_time or create_time < time.time() - 1.0:
|
return f(*args, **kwargs)
|
||||||
return None
|
except ConnectionRefusedError:
|
||||||
|
_logger.info(f"failed to connect to JSON-RPC server")
|
||||||
|
if not create_time or create_time < time.time() - 1.0:
|
||||||
|
raise DaemonNotRunning()
|
||||||
# Sleep a bit and try again; it might have just been started
|
# Sleep a bit and try again; it might have just been started
|
||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
|
|
||||||
|
|||||||
23
run_electrum
23
run_electrum
@@ -364,8 +364,7 @@ if __name__ == '__main__':
|
|||||||
d.init_gui(config, plugins)
|
d.init_gui(config, plugins)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
server = daemon.get_server()
|
result = daemon.request(config, 'gui', config_options)
|
||||||
result = server.gui(config_options)
|
|
||||||
|
|
||||||
elif cmdname == 'daemon':
|
elif cmdname == 'daemon':
|
||||||
subcommand = config.get('subcommand')
|
subcommand = config.get('subcommand')
|
||||||
@@ -405,27 +404,25 @@ if __name__ == '__main__':
|
|||||||
d.join()
|
d.join()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
server = daemon.get_server(config)
|
result = daemon.request(config, 'daemon', config_options)
|
||||||
result = server.daemon(config_options)
|
|
||||||
else:
|
else:
|
||||||
server = daemon.get_server(config)
|
try:
|
||||||
if server is not None:
|
result = daemon.request(config, 'daemon', config_options)
|
||||||
result = server.daemon(config_options)
|
except daemon.DaemonNotRunning:
|
||||||
else:
|
|
||||||
print_msg("Daemon not running")
|
print_msg("Daemon not running")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
# command line
|
# command line
|
||||||
server = daemon.get_server(config)
|
try:
|
||||||
init_cmdline(config_options, server)
|
init_cmdline(config_options, True)
|
||||||
if server is not None:
|
result = daemon.request(config, 'run_cmdline', config_options)
|
||||||
result = server.run_cmdline(config_options)
|
except daemon.DaemonNotRunning:
|
||||||
else:
|
|
||||||
cmd = known_commands[cmdname]
|
cmd = known_commands[cmdname]
|
||||||
if cmd.requires_network:
|
if cmd.requires_network:
|
||||||
print_msg("Daemon not running; try 'electrum daemon start'")
|
print_msg("Daemon not running; try 'electrum daemon start'")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
|
init_cmdline(config_options, False)
|
||||||
plugins = init_plugins(config, 'cmdline')
|
plugins = init_plugins(config, 'cmdline')
|
||||||
result = run_offline_command(config, config_options, plugins)
|
result = run_offline_command(config, config_options, plugins)
|
||||||
# print result
|
# print result
|
||||||
|
|||||||
Reference in New Issue
Block a user