From e3e296fc6849a0984ae9ed8e282f865917a99915 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 20 Apr 2018 22:46:57 +0200 Subject: [PATCH] Added patch to delete old or user-less sessions --- .../delete-old-sessions.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 patches/delete-old-sessions/delete-old-sessions.go diff --git a/patches/delete-old-sessions/delete-old-sessions.go b/patches/delete-old-sessions/delete-old-sessions.go new file mode 100644 index 00000000..50fe2c78 --- /dev/null +++ b/patches/delete-old-sessions/delete-old-sessions.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + + "github.com/aerogo/nano" + "github.com/animenotifier/arn" + "github.com/fatih/color" +) + +func main() { + color.Yellow("Deleting old sessions") + + defer color.Green("Finished.") + defer arn.Node.Close() + + // threshold := time.Now().Add(-6 * 30 * 24 * time.Hour).Format(time.RFC3339) + count := 0 + total := 0 + + for session := range streamSessions() { + data := *session + // created := data["created"].(string) + + if data["userId"] == nil { // created < threshold + arn.DB.Delete("Session", data["sid"].(string)) + count++ + } + + total++ + } + + fmt.Printf("Deleted %d / %d sessions.\n", count, total) +} + +func streamSessions() chan *arn.Session { + channel := make(chan *arn.Session, nano.ChannelBufferSize) + + go func() { + for obj := range arn.DB.All("Session") { + channel <- obj.(*arn.Session) + } + + close(channel) + }() + + return channel +}