From 4aba1ba5fd765327bb7b8d865f66b9d9eba74821 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Mon, 15 Jul 2019 20:49:01 -0400 Subject: [PATCH] simplify cleanup command --- activity.go | 4 ++-- docs/admin.txt | 4 ++-- honk.go | 39 +++++++++++++++------------------------ upgradedb.go | 2 +- 4 files changed, 20 insertions(+), 29 deletions(-) diff --git a/activity.go b/activity.go index 41b04c3..db0de40 100644 --- a/activity.go +++ b/activity.go @@ -67,7 +67,7 @@ func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error if err != nil { return err } - req.Header.Set("User-Agent", "honksnonk/5.0; " + serverName) + req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) req.Header.Set("Content-Type", theonetruename) zig(keyname, key, req, msg) resp, err := client.Do(req) @@ -120,7 +120,7 @@ func GetJunkTimeout(url string, timeout time.Duration) (junk.Junk, error) { } req.Header.Set("Accept", at) req.Header.Set("Accept-Encoding", "gzip") - req.Header.Set("User-Agent", "honksnonk/5.0; " + serverName) + req.Header.Set("User-Agent", "honksnonk/5.0; "+serverName) if timeout > 0 { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() diff --git a/docs/admin.txt b/docs/admin.txt index aab6a8d..4f4a758 100644 --- a/docs/admin.txt +++ b/docs/admin.txt @@ -27,9 +27,9 @@ A list of available emus and memes appears in the funzone. One should occasionally run `honk cleanup` to free up internal space in the database. This deletes honks older than 30 days, but not those posted by a -user. +user. `honk cleanup [days]` may be used to adjust the timeframe. -One may also run `honk reduce [honker]` to delete honks older than 3 days. +One may also run `honk cleanup [honker]` to delete honks older than 3 days. This is useful to reduce the space requirements from following image bots. (Neither command runs vacuum, so the file size will not immediately shrink.) diff --git a/honk.go b/honk.go index cd80086..3d0db35 100644 --- a/honk.go +++ b/honk.go @@ -1507,19 +1507,19 @@ func serve() { } } -func cleanupdb(days int) { +func cleanupdb(arg string) { db := opendatabase() - expdate := time.Now().UTC().Add(-time.Duration(days) * 24 * time.Hour).Format(dbtimeformat) - doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3))", expdate) - doordie(db, "delete from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3)", expdate) - doordie(db, "delete from files where fileid not in (select fileid from donks)") -} - -func reducedb(honker string) { - db := opendatabase() - expdate := time.Now().UTC().Add(-3 * 24 * time.Hour).Format(dbtimeformat) - doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and honker = ?)", expdate, honker) - doordie(db, "delete from honks where dt < ? and whofore = 0 and honker = ?", expdate, honker) + days, err := strconv.Atoi(arg) + if err != nil { + honker := arg + expdate := time.Now().UTC().Add(-3 * 24 * time.Hour).Format(dbtimeformat) + doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and honker = ?)", expdate, honker) + doordie(db, "delete from honks where dt < ? and whofore = 0 and honker = ?", expdate, honker) + } else { + expdate := time.Now().UTC().Add(-time.Duration(days) * 24 * time.Hour).Format(dbtimeformat) + doordie(db, "delete from donks where honkid in (select honkid from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3))", expdate) + doordie(db, "delete from honks where dt < ? and whofore = 0 and convoy not in (select convoy from honks where whofore = 2 or whofore = 3)", expdate) + } doordie(db, "delete from files where fileid not in (select fileid from donks)") } @@ -1589,7 +1589,6 @@ func ElaborateUnitTests() { } func main() { - var err error cmd := "run" if len(os.Args) > 1 { cmd = os.Args[1] @@ -1614,19 +1613,11 @@ func main() { case "adduser": adduser() case "cleanup": - days := 30 + arg := "30" if len(os.Args) > 2 { - days, err = strconv.Atoi(os.Args[2]) - if err != nil { - log.Fatal(err) - } + arg = os.Args[2] } - cleanupdb(days) - case "reduce": - if len(os.Args) < 3 { - log.Fatal("need a honker name") - } - reducedb(os.Args[2]) + cleanupdb(arg) case "ping": if len(os.Args) < 4 { fmt.Printf("usage: honk ping from to\n") diff --git a/upgradedb.go b/upgradedb.go index 9e5744f..ff761f8 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -112,6 +112,6 @@ func upgradedb() { default: log.Fatalf("can't upgrade unknown version %d", dbversion) } - cleanupdb(30) + cleanupdb("30") os.Exit(0) }