From 4ef5f7b379b37baeb3b6de1999be16d089e29738 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sat, 6 Jul 2024 15:53:34 +0530 Subject: usage: don't handle signals directly --- cmd/redq/main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'cmd') diff --git a/cmd/redq/main.go b/cmd/redq/main.go index 8807105..df275d6 100644 --- a/cmd/redq/main.go +++ b/cmd/redq/main.go @@ -4,10 +4,13 @@ import ( "context" "log" "net" + "os" + "os/signal" + "syscall" "github.com/jackc/pgx/v5" - "sinanmohd.com/redq/usage" "sinanmohd.com/redq/db" + "sinanmohd.com/redq/usage" ) func main() { @@ -15,6 +18,7 @@ func main() { Data : make(usage.UsageMap), } + iface, err := net.InterfaceByName("wlan0") if err != nil { log.Fatalf("lookup network: %s", err) @@ -28,5 +32,16 @@ func main() { defer conn.Close(ctx) queries := db.New(conn) + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, os.Kill, syscall.SIGTERM) + go func () { + <- sigs + err := u.UpdateDb(queries, ctx, false) + if err != nil { + log.Printf("updating Database: %s", err) + } + os.Exit(0) + }() + u.Run(iface, queries, ctx) } -- cgit v1.2.3