summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-06 15:53:34 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-06 15:55:08 +0530
commit4ef5f7b379b37baeb3b6de1999be16d089e29738 (patch)
tree48a8fcf17ff70e645544a7a9ca27e6ab0b4eeb8b /cmd
parent9e4f86953d7a39e4d32801a328c1f8424ca6c969 (diff)
usage: don't handle signals directly
Diffstat (limited to 'cmd')
-rw-r--r--cmd/redq/main.go17
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)
}