From ad9bba4f934d2ff51c06d2dc2bc96c74c99201d7 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 26 Oct 2025 20:34:52 +0000 Subject: [PATCH] logtap: add WIP backfill command --- pkgs/logtap/Makefile | 7 +++-- pkgs/logtap/backfill.fnl | 57 ++++++++++++++++++++++++++++++++++++++++ pkgs/logtap/default.nix | 6 +++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 pkgs/logtap/backfill.fnl diff --git a/pkgs/logtap/Makefile b/pkgs/logtap/Makefile index ed31fbe..1eb8360 100644 --- a/pkgs/logtap/Makefile +++ b/pkgs/logtap/Makefile @@ -1,6 +1,9 @@ -TARGETS=logtap +TARGETS=logtap backfill CFLAGS=-Wall default: $(TARGETS) +backfill: backfill.fnl + fennelc $< + install: $(TARGETS) - install -Dt $(PREFIX)/bin/ logtap + install -Dt $(PREFIX)/bin/ $(TARGETS) diff --git a/pkgs/logtap/backfill.fnl b/pkgs/logtap/backfill.fnl new file mode 100644 index 0000000..c9f6d3a --- /dev/null +++ b/pkgs/logtap/backfill.fnl @@ -0,0 +1,57 @@ +(local { : file-exists? } (require :anoia)) +(local tai64 (require :anoia.tai64)) +(local { : write &as ll } (require :lualinux)) + +(local O_WRONLY 1) +(local O_RDWR 2) +(local O_CREAT 64) + +(fn send-message [fd message] + (case (write fd message) + n (if (> (# message) n) (send-message (string.sub message n)) true) + (nil code) nil)) + +(fn parse-timestamp [message] + (let [{ : s : n } (tai64.from-timestamp message)] + (+ (* s 1000000000) n))) + +(fn spit [filename body] + (with-open [f (assert (io.open filename :w))] + (f:write body))) + +(fn write-timestamp [file ts] + (spit file (string.format "%d" ts)) + ts) + +(fn slurp [filename] + (with-open [f (assert (io.open filename :r))] + (f:read "a"))) + +(let [[fifo-name stampfile] arg + fifo (ll.open fifo-name O_WRONLY)] + (var backfill? true) + (var next-ts + (if (file-exists? stampfile) + (tonumber (slurp stampfile)) + 0)) + + (each [l (: (io.input) :lines "L")] + (case l + "START SHIPPING" + (set backfill? false) + "STOP SHIPPING" + (set backfill? true) + message + (let [message-ts (parse-timestamp message)] + (when (> message-ts next-ts) + (set next-ts (write-timestamp stampfile message-ts))) + (when (>= message-ts next-ts) + (io.stderr:write (.. "writing " message backfill?)) + (if backfill? + (when (not (send-message fifo message)) + ;; If the fifo write fails, it could be a glitch or it + ;; could be some kind of wider outage. Give up, + ;; and rely on the supervisor to restart us when + ;; conditions are again auspicious + (io.stderr:write "write failed\n") + (os.exit 1)))))))) diff --git a/pkgs/logtap/default.nix b/pkgs/logtap/default.nix index 754eb34..e0a80c7 100644 --- a/pkgs/logtap/default.nix +++ b/pkgs/logtap/default.nix @@ -1,8 +1,14 @@ { stdenv, + fennelc, + lualinux, + lua, + anoia }: stdenv.mkDerivation { name = "logtap"; + nativeBuildInputs = [ fennelc ]; + buildInputs = [ lua lualinux anoia ]; makeFlags = [ "PREFIX=${placeholder "out"}" ]; src = ./.; }