backup: Kill lightningd instead of the roundabout way around

Looking up the process parents and searching for `lightnignd` allows us to
kill directly. It also means we no longer have to wait for the RPC to be
enabled to abort.
This commit is contained in:
Christian Decker
2020-05-15 21:01:38 +02:00
parent 63c9872ec2
commit fbc494ec23

View File

@@ -11,6 +11,7 @@ import struct
import sys
import sqlite3
import time
import psutil
plugin = Plugin()
@@ -245,19 +246,12 @@ def get_backend(destination, create=False, require_init=False):
backend = backend_cl(destination, create=create)
initialized = backend.initialize()
if require_init and not initialized:
abort("Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(destination))
kill("Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(destination))
assert(backend.version is not None)
assert(backend.prev_version is not None)
return backend
def abort(reason: str) -> None:
plugin.log(reason)
time.sleep(1)
plugin.rpc.stop()
raise ValueError()
def check_first_write(plugin, data_version):
"""Verify that we are up-to-date and c-lightning didn't forget writes.
@@ -286,10 +280,10 @@ def check_first_write(plugin, data_version):
return True
elif backend.prev_version > data_version - 1:
abort("c-lightning seems to have lost some state (failed restore?). Emergency shutdown.")
kill("c-lightning seems to have lost some state (failed restore?). Emergency shutdown.")
else:
abort("Backup is out of date, we cannot continue safely. Emergency shutdown.")
kill("Backup is out of date, we cannot continue safely. Emergency shutdown.")
@plugin.hook('db_write')
@@ -321,19 +315,19 @@ def on_init(options: Mapping[str, str], plugin: Plugin, **kwargs):
# Ensure that we don't inadventently switch the destination
if not os.path.exists("backup.lock"):
print("Files in the current directory {}".format(", ".join(os.listdir("."))))
return abort("Could not find backup.lock in the lightning-dir, have you initialized using the backup-cli utility?")
kill("Could not find backup.lock in the lightning-dir, have you initialized using the backup-cli utility?")
d = json.load(open("backup.lock", 'r'))
if destination is None or destination == 'null':
destination = d['backend_url']
elif destination != d['backend_url']:
abort(
kill(
"The destination specified as option does not match the one "
"specified in backup.lock. Please check your settings"
)
if not plugin.db_path.startswith('sqlite3'):
abort("The backup plugin only works with the sqlite3 database.")
kill("The backup plugin only works with the sqlite3 database.")
plugin.backend = get_backend(destination, require_init=True)