diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-07-06 15:53:34 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-07-06 15:55:08 +0530 |
commit | 4ef5f7b379b37baeb3b6de1999be16d089e29738 (patch) | |
tree | 48a8fcf17ff70e645544a7a9ca27e6ab0b4eeb8b /cmd | |
parent | 9e4f86953d7a39e4d32801a328c1f8424ca6c969 (diff) |
usage: don't handle signals directly
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/redq/main.go | 17 |
1 files changed, 16 insertions, 1 deletions
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) } |