diff --git a/internal/app/uberbringer/ntfy.go b/internal/app/uberbringer/ntfy.go index 671bb6d..a73879b 100644 --- a/internal/app/uberbringer/ntfy.go +++ b/internal/app/uberbringer/ntfy.go @@ -28,9 +28,10 @@ var weekdays = map[string]time.Weekday{ } // newSubscribers returns a subscriber for each [[ntfy.subscriptions]] -// table. It reads the credential files and makes sure the state directory -// can be written, so that a mistake in either stops the daemon at startup. -func newSubscribers(cfg *config.Config, logger *slog.Logger) ([]*ntfy.Subscriber, error) { +// table, which flushes its state file to disk with syncState. It reads the +// credential files and makes sure the state directory can be written, so +// that a mistake in either stops the daemon at startup. +func newSubscribers(cfg *config.Config, logger *slog.Logger, syncState func(*os.File) error) ([]*ntfy.Subscriber, error) { subs := cfg.Ntfy.Subscriptions if len(subs) == 0 { return nil, nil @@ -63,6 +64,7 @@ func newSubscribers(cfg *config.Config, logger *slog.Logger) ([]*ntfy.Subscriber Keepalive: sc.Keepalive, SkipUpdates: cfg.Ntfy.Format.Updates == "skip", StateFile: filepath.Join(dir, "ntfy-"+sc.Name+".json"), + Sync: syncState, Logger: logger, Report: func(err error) { reportNtfy(sc.Name, "", err) }, } diff --git a/internal/app/uberbringer/ntfy_test.go b/internal/app/uberbringer/ntfy_test.go index ff012d7..85465e6 100644 --- a/internal/app/uberbringer/ntfy_test.go +++ b/internal/app/uberbringer/ntfy_test.go @@ -26,6 +26,11 @@ import ( "uberbringer/internal/pkg/receipt" ) +// skipSync stands in for the flush of a subscription's state file to disk. +// On a busy disk a flush can take seconds, and a first start writes its +// state as soon as open arrives, before it reads the message behind it. +func skipSync(*os.File) error { return nil } + // TestServeWithSubscription runs the daemon with a subscription to a fake // ntfy server: a message on the stream prints, /healthz lists the // subscription as connected without changing its status code, and the @@ -88,7 +93,7 @@ token_file = %q ctx, cancel := context.WithCancel(context.Background()) defer cancel() served := make(chan error, 1) - go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen) }() + go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen, skipSync) }() waitFor(t, func() bool { // "Grüße" in CP437: ü is 0x81, ß is 0xE1. @@ -153,7 +158,7 @@ func TestNewSubscribersErrors(t *testing.T) { } { sub.TokenFile = tt.file cfg.Ntfy.Subscriptions = []config.NtfySubscription{sub} - if _, err := newSubscribers(&cfg, slog.New(slog.DiscardHandler)); err == nil || !strings.Contains(err.Error(), tt.want) { + if _, err := newSubscribers(&cfg, slog.New(slog.DiscardHandler), skipSync); err == nil || !strings.Contains(err.Error(), tt.want) { t.Errorf("token_file %s: err = %v, want %q", tt.file, err, tt.want) } } diff --git a/internal/app/uberbringer/serve.go b/internal/app/uberbringer/serve.go index 920452e..fb9f8ca 100644 --- a/internal/app/uberbringer/serve.go +++ b/internal/app/uberbringer/serve.go @@ -34,7 +34,7 @@ func serve(ctx context.Context, configPath string, build BuildInfo, out io.Write if err != nil { return err } - return run(ctx, cfg, build, out, listenTCP) + return run(ctx, cfg, build, out, listenTCP, (*os.File).Sync) } // listenFunc opens the HTTP listener on addr. @@ -46,8 +46,9 @@ func listenTCP(ctx context.Context, addr string) (net.Listener, error) { } // run wires every component from cfg, opens the HTTP listener with listen -// once the wiring has succeeded, and serves until ctx is cancelled. -func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer, listen listenFunc) error { +// once the wiring has succeeded, and serves until ctx is cancelled. The +// subscriptions flush their state files to disk with syncState. +func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer, listen listenFunc, syncState func(*os.File) error) error { logger := newLogger(cfg.Log, os.Stderr) printBanner(out, build) @@ -71,7 +72,7 @@ func run(ctx context.Context, cfg *config.Config, build BuildInfo, out io.Writer } defer flushSentry() - subscribers, err := newSubscribers(cfg, logger) + subscribers, err := newSubscribers(cfg, logger, syncState) if err != nil { return err } diff --git a/internal/app/uberbringer/serve_test.go b/internal/app/uberbringer/serve_test.go index 4f8a02a..9ca8abf 100644 --- a/internal/app/uberbringer/serve_test.go +++ b/internal/app/uberbringer/serve_test.go @@ -91,7 +91,7 @@ level = "error" ctx, cancel := context.WithCancel(context.Background()) defer cancel() served := make(chan error, 1) - go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen) }() + go func() { served <- run(ctx, cfg, BuildInfo{Version: "test"}, io.Discard, listen, skipSync) }() base := "http://" + ln.Addr().String() waitFor(t, func() bool {