commit cdd5361f50667dd4215faa81012861f036f85411
parent ba7660ba7a4c6614251b0ce7af051f8a7c9a6332
Author: Silas Brack <silasbrack@gmail.com>
Date: Tue, 30 Sep 2025 18:24:32 +0200
Add job routing logs from simple-web-app to Hetzner mounted volume
Diffstat:
3 files changed, 60 insertions(+), 18 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: deploy
+.PHONY: deploy-digitalocean deploy-hetzner
## Deploy configuration to production server
deploy-digitalocean:
@@ -6,3 +6,4 @@ deploy-digitalocean:
deploy-hetzner:
nixos-rebuild test --show-trace --no-reexec --flake .#debian-2gb-hel1-1 --target-host root@46.62.150.48
+
diff --git a/nixos/configuration-debian-2gb-hel1-1.nix b/nixos/configuration-debian-2gb-hel1-1.nix
@@ -37,6 +37,10 @@
device = "/dev/sda1";
fsType = "ext4";
};
+ fileSystems."/mnt/volume-hel1-1" = {
+ device = "/dev/sdb";
+ fsType = "ext4";
+ };
nix.gc = {
automatic = true;
dates = "weekly";
diff --git a/nixos/simple-web-app.nix b/nixos/simple-web-app.nix
@@ -1,6 +1,7 @@
{
config,
lib,
+ pkgs,
...
}:
@@ -47,25 +48,61 @@ in
config = mkIf cfg.enable {
networking.firewall.allowedTCPPorts = [ cfg.port ];
- systemd.services = {
- simple-web-app = {
- wantedBy = [ "multi-user.target" ];
- description = "Run simple web app.";
- after = [ "network.target" ];
- serviceConfig = {
- Type = "simple";
- ExecStart = "${cfg.package}/bin/simple-web-app";
- DynamicUser = true;
- StateDirectory = "simple-web-app"; # Ensures /var/lib/simple-web-app/
- };
- environment = {
- UVICORN_PORT = builtins.toString cfg.port;
- UVICORN_LOG_CONFIG = "${cfg.package}/share/logging.json";
- DEBUG = "false";
- DATABASE_PATH = "/var/lib/simple-web-app/db.sqlite3";
- };
+ # Run service
+ systemd.services.simple-web-app = {
+ wantedBy = [ "multi-user.target" ];
+ description = "Run simple web app.";
+ after = [ "network.target" ];
+ serviceConfig = {
+ Type = "simple";
+ ExecStart = "${cfg.package}/bin/simple-web-app";
+ DynamicUser = true;
+ StateDirectory = "simple-web-app"; # Ensures /var/lib/simple-web-app/
+ };
+ environment = {
+ UVICORN_PORT = builtins.toString cfg.port;
+ UVICORN_LOG_CONFIG = "${cfg.package}/share/logging.json";
+ DEBUG = "false";
+ DATABASE_PATH = "/var/lib/simple-web-app/db.sqlite3";
};
+ };
+
+ # Archive logs to volume
+ systemd.services.simple-web-app-log-collection = {
+ script = ''
+ # Add a small 5 minute buffer to catch delayed logs
+ export PREVIOUS_HOUR=$(${pkgs.coreutils}/bin/date -d '1 hour ago 5 minutes ago' +%Y-%m-%dT%H:00:00)
+ export CURRENT_HOUR=$(${pkgs.coreutils}/bin/date -d '5 minutes ago' +%Y-%m-%dT%H:00:00)
+
+ # Create log storage folders if they don't exist
+ mkdir -p $LOG_STORAGE_LOCATION/$(date -d "$PREVIOUS_HOUR" +%Y/%m/%d)
+ # Extract logs from journalctl, compress, and save to volume
+ journalctl -u $SERVICE_NAME \
+ --since=$PREVIOUS_HOUR \
+ --until=$CURRENT_HOUR \
+ -o json | \
+ ${pkgs.gzip}/bin/gzip \
+ > $LOG_STORAGE_LOCATION/$(date -d "$PREVIOUS_HOUR" +%Y/%m/%d/%H).json.gz
+ '';
+ environment = {
+ SERVICE_NAME = "simple-web-app";
+ LOG_STORAGE_LOCATION = "/mnt/volume-hel1-1/simple-web-app/logs";
+ };
+ serviceConfig = {
+ Type = "oneshot";
+ User = "root";
+ };
+ };
+ systemd.timers = {
+ simple-web-app-log-collection = {
+ wantedBy = [ "timers.target" ];
+ timerConfig = {
+ OnCalendar = "hourly";
+ Persistent = true;
+ Unit = "simple-web-app-log-collection.service";
+ };
+ };
};
};
}