ntfy-waiter: more verbosity/debugging
This commit is contained in:
parent
230ca20017
commit
523e859ee4
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import socket
|
import socket
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
@ -50,6 +51,10 @@ class Adapter:
|
||||||
self.topic = topic
|
self.topic = topic
|
||||||
self.clients = set()
|
self.clients = set()
|
||||||
|
|
||||||
|
def log_clients(self):
|
||||||
|
clients_str = '\n'.join(f' {c.addr_info}' for c in self.clients)
|
||||||
|
logger.debug(f"clients alive ({len(self.clients)}):\n{clients_str}")
|
||||||
|
|
||||||
def listener_loop(self):
|
def listener_loop(self):
|
||||||
logger.info(f"listening for connections on {self.host}:{self.port}")
|
logger.info(f"listening for connections on {self.host}:{self.port}")
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
@ -70,6 +75,8 @@ class Adapter:
|
||||||
for c in dead_clients:
|
for c in dead_clients:
|
||||||
self.clients.remove(c)
|
self.clients.remove(c)
|
||||||
|
|
||||||
|
self.log_clients()
|
||||||
|
|
||||||
def notify_loop(self):
|
def notify_loop(self):
|
||||||
logger.info("waiting for notification events")
|
logger.info("waiting for notification events")
|
||||||
ntfy_proc = subprocess.Popen(
|
ntfy_proc = subprocess.Popen(
|
||||||
|
@ -88,9 +95,14 @@ def get_topic() -> str:
|
||||||
return open('/run/secrets/ntfy-sh-topic', 'rt').read().strip()
|
return open('/run/secrets/ntfy-sh-topic', 'rt').read().strip()
|
||||||
|
|
||||||
def run_forever(callable):
|
def run_forever(callable):
|
||||||
callable()
|
try:
|
||||||
logger.error(f"{callable} unexpectedly returned")
|
callable()
|
||||||
sys.exit(1)
|
except Exception as e:
|
||||||
|
logger.error(f"{callable} failed: {e}")
|
||||||
|
else:
|
||||||
|
logger.error(f"{callable} unexpectedly returned")
|
||||||
|
# sys.exit(1)
|
||||||
|
os._exit(1) # sometimes `sys.exit()` doesn't actually exit...
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
logging.basicConfig()
|
logging.basicConfig()
|
||||||
|
@ -114,7 +126,7 @@ def main():
|
||||||
listener_loop = threading.Thread(target=run_forever, name="listener_loop", args=(adapter.listener_loop,))
|
listener_loop = threading.Thread(target=run_forever, name="listener_loop", args=(adapter.listener_loop,))
|
||||||
notify_loop = threading.Thread(target=run_forever, name="notify_loop", args=(adapter.notify_loop,))
|
notify_loop = threading.Thread(target=run_forever, name="notify_loop", args=(adapter.notify_loop,))
|
||||||
|
|
||||||
# TODO: this method of exiting seems to leave the listener behind,
|
# TODO: this method of exiting seems to sometimes leave the listener behind (?)
|
||||||
# preventing anyone else from re-binding the port.
|
# preventing anyone else from re-binding the port.
|
||||||
listener_loop.start()
|
listener_loop.start()
|
||||||
notify_loop.start()
|
notify_loop.start()
|
||||||
|
|
|
@ -12,6 +12,14 @@ let
|
||||||
numPorts = portHigh - portLow + 1;
|
numPorts = portHigh - portLow + 1;
|
||||||
mkService = port: let
|
mkService = port: let
|
||||||
silence = port - portLow;
|
silence = port - portLow;
|
||||||
|
flags = lib.optional cfg.verbose "--verbose";
|
||||||
|
cli = [
|
||||||
|
"${cfg.package}/bin/ntfy-waiter"
|
||||||
|
"--port"
|
||||||
|
"${builtins.toString port}"
|
||||||
|
"--silence"
|
||||||
|
"${builtins.toString silence}"
|
||||||
|
] ++ flags;
|
||||||
in {
|
in {
|
||||||
"ntfy-waiter-${builtins.toString silence}" = {
|
"ntfy-waiter-${builtins.toString silence}" = {
|
||||||
# TODO: run not as root (e.g. as ntfy-sh)
|
# TODO: run not as root (e.g. as ntfy-sh)
|
||||||
|
@ -19,7 +27,8 @@ let
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
Restart = "always";
|
Restart = "always";
|
||||||
ExecStart = "${cfg.package}/bin/ntfy-waiter --port ${builtins.toString port} --silence ${builtins.toString silence}";
|
RestartSec = "5s";
|
||||||
|
ExecStart = lib.concatStringsSep " " cli;
|
||||||
};
|
};
|
||||||
after = [ "network.target" ];
|
after = [ "network.target" ];
|
||||||
wantedBy = [ "default.target" ];
|
wantedBy = [ "default.target" ];
|
||||||
|
@ -32,6 +41,10 @@ in
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
};
|
};
|
||||||
|
sane.ntfy-waiter.verbose = mkOption {
|
||||||
|
type = types.bool;
|
||||||
|
default = true;
|
||||||
|
};
|
||||||
sane.ntfy-waiter.package = mkOption {
|
sane.ntfy-waiter.package = mkOption {
|
||||||
type = types.package;
|
type = types.package;
|
||||||
default = pkgs.static-nix-shell.mkPython3Bin {
|
default = pkgs.static-nix-shell.mkPython3Bin {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user