services: use wants dependency where possible
Let A be a service that depends on another service B. When A can gracefully handle failures and restarts of B, use ``` wants = [ "B.service" ]; after = [ "B.service" ]; ``` instead of ``` requires = [ "B.service" ]; after = [ "B.service" ]; ``` in the definition of A. This way, A keeps running when B is stopped or restarted after a failure. With `requires`, A is instead stopped when B is stopped or restarted due to a failure. This brings two benefits: 1. Improved uptime Examples: - RTL keeps running when one lightning node has failed - btcpayserver keeps running and accepting on-chain payments when the lightning node has crashed 2. Avoids a systemd bug where depending units (`A.service` in the above example) are not restarted when their dependency fails (issue github/systemd#18856, no full link to avoid spamming the issue). In real world nix-bitcoin deployments, this issue was only likely to appear when clightning failed during activation, causing depending units (like `RTL`) to stop and not be restarted. All services depending on `clightning` have now been changed to use `wants`, thereby avoiding the bug. Services `electrs` and `lightning-loop` fail when their respective dependencies stop, so these services have not been changed. I also haven't changed services `joinmarket` and `joinmarket-yieldgenerator`. Further manual testing is needed to determine if they can be switched to `wants`.
This commit is contained in:
parent
c7e5de3b2b
commit
4aaef5fdf4
7 changed files with 19 additions and 17 deletions
|
|
@ -171,8 +171,9 @@ in {
|
||||||
'';
|
'';
|
||||||
in rec {
|
in rec {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = [ "bitcoind.service" "postgresql.service" ] ++ optional cfg.btcpayserver.lbtc "liquidd.service";
|
requires = [ "postgresql.service" ];
|
||||||
after = requires ++ [ "nix-bitcoin-secrets.target" ];
|
wants = [ "bitcoind.service" ] ++ optional cfg.btcpayserver.lbtc "liquidd.service";
|
||||||
|
after = requires ++ wants ++ [ "nix-bitcoin-secrets.target" ];
|
||||||
preStart = ''
|
preStart = ''
|
||||||
install -m 600 ${configFile} '${cfg.nbxplorer.dataDir}/settings.config'
|
install -m 600 ${configFile} '${cfg.nbxplorer.dataDir}/settings.config'
|
||||||
{
|
{
|
||||||
|
|
@ -223,11 +224,12 @@ in {
|
||||||
lbtcexplorerurl=${nbExplorerUrl}
|
lbtcexplorerurl=${nbExplorerUrl}
|
||||||
lbtcexplorercookiefile=${nbExplorerCookie}
|
lbtcexplorercookiefile=${nbExplorerCookie}
|
||||||
'');
|
'');
|
||||||
in let self = {
|
in rec {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = [ "nbxplorer.service" "postgresql.service" ]
|
requires = [ "postgresql.service" ];
|
||||||
|
wants = [ "nbxplorer.service" ]
|
||||||
++ optional (cfg.btcpayserver.lightningBackend != null) "${cfg.btcpayserver.lightningBackend}.service";
|
++ optional (cfg.btcpayserver.lightningBackend != null) "${cfg.btcpayserver.lightningBackend}.service";
|
||||||
after = self.requires;
|
after = requires ++ wants;
|
||||||
serviceConfig = nbLib.defaultHardening // {
|
serviceConfig = nbLib.defaultHardening // {
|
||||||
ExecStart = ''
|
ExecStart = ''
|
||||||
${cfg.btcpayserver.package}/bin/btcpayserver --conf=${configFile} \
|
${cfg.btcpayserver.package}/bin/btcpayserver --conf=${configFile} \
|
||||||
|
|
@ -244,7 +246,7 @@ in {
|
||||||
} // nbLib.allowedIPAddresses cfg.btcpayserver.tor.enforce;
|
} // nbLib.allowedIPAddresses cfg.btcpayserver.tor.enforce;
|
||||||
startLimitIntervalSec = 30;
|
startLimitIntervalSec = 30;
|
||||||
startLimitBurst = 10;
|
startLimitBurst = 10;
|
||||||
}; in self;
|
};
|
||||||
|
|
||||||
users.users.${cfg.nbxplorer.user} = {
|
users.users.${cfg.nbxplorer.user} = {
|
||||||
isSystemUser = true;
|
isSystemUser = true;
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,7 @@ in {
|
||||||
|
|
||||||
systemd.services.clightning-rest = mkIf cfg.enable {
|
systemd.services.clightning-rest = mkIf cfg.enable {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = [ "clightning.service" ];
|
wants = [ "clightning.service" ];
|
||||||
after = [ "clightning.service" ];
|
after = [ "clightning.service" ];
|
||||||
path = [ pkgs.openssl ];
|
path = [ pkgs.openssl ];
|
||||||
environment.CL_REST_STATE_DIR = cfg.dataDir;
|
environment.CL_REST_STATE_DIR = cfg.dataDir;
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,7 @@ in {
|
||||||
|
|
||||||
systemd.services.fulcrum = {
|
systemd.services.fulcrum = {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = [ "bitcoind.service" ];
|
wants = [ "bitcoind.service" ];
|
||||||
after = [ "bitcoind.service" "nix-bitcoin-secrets.target" ];
|
after = [ "bitcoind.service" "nix-bitcoin-secrets.target" ];
|
||||||
preStart = ''
|
preStart = ''
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -255,7 +255,7 @@ in {
|
||||||
];
|
];
|
||||||
|
|
||||||
systemd.services.liquidd = {
|
systemd.services.liquidd = {
|
||||||
requires = [ "bitcoind.service" ];
|
wants = [ "bitcoind.service" ];
|
||||||
after = [ "bitcoind.service" "nix-bitcoin-secrets.target" ];
|
after = [ "bitcoind.service" "nix-bitcoin-secrets.target" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
preStart = ''
|
preStart = ''
|
||||||
|
|
|
||||||
|
|
@ -275,10 +275,11 @@ in {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.services.mempool = {
|
systemd.services.mempool = rec {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = [ "${cfg.electrumServer}.service" ];
|
requires = [ "mysql.service" ];
|
||||||
after = [ "${cfg.electrumServer}.service" "mysql.service" ];
|
wants = [ "${cfg.electrumServer}.service" ];
|
||||||
|
after = requires ++ wants;
|
||||||
preStart = ''
|
preStart = ''
|
||||||
mkdir -p '${cacheDir}/cache'
|
mkdir -p '${cacheDir}/cache'
|
||||||
<${configFile} sed \
|
<${configFile} sed \
|
||||||
|
|
|
||||||
|
|
@ -190,9 +190,9 @@ in {
|
||||||
|
|
||||||
systemd.services.rtl = rec {
|
systemd.services.rtl = rec {
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
requires = optional cfg.nodes.clightning.enable "clightning.service" ++
|
wants = optional cfg.nodes.clightning.enable "clightning.service" ++
|
||||||
optional cfg.nodes.lnd.enable "lnd.service";
|
optional cfg.nodes.lnd.enable "lnd.service";
|
||||||
after = requires ++ [ "nix-bitcoin-secrets.target" ];
|
after = wants ++ [ "nix-bitcoin-secrets.target" ];
|
||||||
environment.RTL_CONFIG_PATH = cfg.dataDir;
|
environment.RTL_CONFIG_PATH = cfg.dataDir;
|
||||||
environment.DB_DIRECTORY_PATH = cfg.dataDir;
|
environment.DB_DIRECTORY_PATH = cfg.dataDir;
|
||||||
serviceConfig = nbLib.defaultHardening // {
|
serviceConfig = nbLib.defaultHardening // {
|
||||||
|
|
|
||||||
|
|
@ -386,7 +386,6 @@ def _():
|
||||||
@test("restart-bitcoind")
|
@test("restart-bitcoind")
|
||||||
def _():
|
def _():
|
||||||
# Sanity-check system by restarting bitcoind.
|
# Sanity-check system by restarting bitcoind.
|
||||||
# This also restarts all services depending on bitcoind.
|
|
||||||
succeed("systemctl restart bitcoind")
|
succeed("systemctl restart bitcoind")
|
||||||
|
|
||||||
@test("regtest")
|
@test("regtest")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue