mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
daemon: simplify get_fd_or_server
This commit is contained in:
parent
a9239bd40f
commit
b81feb6550
2 changed files with 10 additions and 7 deletions
|
@ -62,7 +62,7 @@ def remove_lockfile(lockfile):
|
||||||
os.unlink(lockfile)
|
os.unlink(lockfile)
|
||||||
|
|
||||||
|
|
||||||
def get_fd_or_server(config: SimpleConfig):
|
def get_file_descriptor(config: SimpleConfig):
|
||||||
'''Tries to create the lockfile, using O_EXCL to
|
'''Tries to create the lockfile, using O_EXCL to
|
||||||
prevent races. If it succeeds it returns the FD.
|
prevent races. If it succeeds it returns the FD.
|
||||||
Otherwise try and connect to the server specified in the lockfile.
|
Otherwise try and connect to the server specified in the lockfile.
|
||||||
|
@ -71,12 +71,12 @@ def get_fd_or_server(config: SimpleConfig):
|
||||||
lockfile = get_lockfile(config)
|
lockfile = get_lockfile(config)
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644), None
|
return os.open(lockfile, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644)
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
server = get_server(config)
|
server = get_server(config)
|
||||||
if server is not None:
|
if server is not None:
|
||||||
return None, server
|
return
|
||||||
# Couldn't connect; remove lockfile and try again.
|
# Couldn't connect; remove lockfile and try again.
|
||||||
remove_lockfile(lockfile)
|
remove_lockfile(lockfile)
|
||||||
|
|
||||||
|
@ -170,8 +170,9 @@ class Daemon(DaemonThread):
|
||||||
DaemonThread.__init__(self)
|
DaemonThread.__init__(self)
|
||||||
self.config = config
|
self.config = config
|
||||||
if fd is None and listen_jsonrpc:
|
if fd is None and listen_jsonrpc:
|
||||||
fd, server = get_fd_or_server(config)
|
fd = get_file_descriptor(config)
|
||||||
if fd is None: raise Exception('failed to lock daemon; already running?')
|
if fd is None:
|
||||||
|
raise Exception('failed to lock daemon; already running?')
|
||||||
self.asyncio_loop, self._stop_loop, self._loop_thread = create_and_start_event_loop()
|
self.asyncio_loop, self._stop_loop, self._loop_thread = create_and_start_event_loop()
|
||||||
if config.get('offline'):
|
if config.get('offline'):
|
||||||
self.network = None
|
self.network = None
|
||||||
|
|
|
@ -357,13 +357,14 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
if cmdname == 'gui':
|
if cmdname == 'gui':
|
||||||
configure_logging(config)
|
configure_logging(config)
|
||||||
fd, server = daemon.get_fd_or_server(config)
|
fd = daemon.get_file_descriptor(config)
|
||||||
if fd is not None:
|
if fd is not None:
|
||||||
plugins = init_plugins(config, config.get('gui', 'qt'))
|
plugins = init_plugins(config, config.get('gui', 'qt'))
|
||||||
d = daemon.Daemon(config, fd)
|
d = daemon.Daemon(config, fd)
|
||||||
d.init_gui(config, plugins)
|
d.init_gui(config, plugins)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
|
server = daemon.get_server()
|
||||||
result = server.gui(config_options)
|
result = server.gui(config_options)
|
||||||
|
|
||||||
elif cmdname == 'daemon':
|
elif cmdname == 'daemon':
|
||||||
|
@ -373,7 +374,7 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
if subcommand in [None, 'start']:
|
if subcommand in [None, 'start']:
|
||||||
configure_logging(config)
|
configure_logging(config)
|
||||||
fd, server = daemon.get_fd_or_server(config)
|
fd = daemon.get_file_descriptor(config)
|
||||||
if fd is not None:
|
if fd is not None:
|
||||||
if subcommand == 'start':
|
if subcommand == 'start':
|
||||||
pid = os.fork()
|
pid = os.fork()
|
||||||
|
@ -404,6 +405,7 @@ if __name__ == '__main__':
|
||||||
d.join()
|
d.join()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
else:
|
else:
|
||||||
|
server = daemon.get_server(config)
|
||||||
result = server.daemon(config_options)
|
result = server.daemon(config_options)
|
||||||
else:
|
else:
|
||||||
server = daemon.get_server(config)
|
server = daemon.get_server(config)
|
||||||
|
|
Loading…
Add table
Reference in a new issue