mirror of
https://github.com/aljazceru/plugins.git
synced 2025-12-20 06:34:20 +01:00
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:
@@ -11,6 +11,7 @@ import struct
|
|||||||
import sys
|
import sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import time
|
import time
|
||||||
|
import psutil
|
||||||
|
|
||||||
|
|
||||||
plugin = Plugin()
|
plugin = Plugin()
|
||||||
@@ -245,19 +246,12 @@ def get_backend(destination, create=False, require_init=False):
|
|||||||
backend = backend_cl(destination, create=create)
|
backend = backend_cl(destination, create=create)
|
||||||
initialized = backend.initialize()
|
initialized = backend.initialize()
|
||||||
if require_init and not initialized:
|
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.version is not None)
|
||||||
assert(backend.prev_version is not None)
|
assert(backend.prev_version is not None)
|
||||||
return backend
|
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):
|
def check_first_write(plugin, data_version):
|
||||||
"""Verify that we are up-to-date and c-lightning didn't forget writes.
|
"""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
|
return True
|
||||||
|
|
||||||
elif backend.prev_version > data_version - 1:
|
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:
|
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')
|
@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
|
# Ensure that we don't inadventently switch the destination
|
||||||
if not os.path.exists("backup.lock"):
|
if not os.path.exists("backup.lock"):
|
||||||
print("Files in the current directory {}".format(", ".join(os.listdir("."))))
|
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'))
|
d = json.load(open("backup.lock", 'r'))
|
||||||
if destination is None or destination == 'null':
|
if destination is None or destination == 'null':
|
||||||
destination = d['backend_url']
|
destination = d['backend_url']
|
||||||
elif destination != d['backend_url']:
|
elif destination != d['backend_url']:
|
||||||
abort(
|
kill(
|
||||||
"The destination specified as option does not match the one "
|
"The destination specified as option does not match the one "
|
||||||
"specified in backup.lock. Please check your settings"
|
"specified in backup.lock. Please check your settings"
|
||||||
)
|
)
|
||||||
|
|
||||||
if not plugin.db_path.startswith('sqlite3'):
|
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)
|
plugin.backend = get_backend(destination, require_init=True)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user