simplify cleanup command

This commit is contained in:
Ted Unangst 2019-07-15 20:49:01 -04:00
parent a5aa1dbdcb
commit 4aba1ba5fd
4 changed files with 20 additions and 29 deletions

View File

@ -67,7 +67,7 @@ func PostMsg(keyname string, key *rsa.PrivateKey, url string, msg []byte) error
if err != nil { if err != nil {
return err 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) req.Header.Set("Content-Type", theonetruename)
zig(keyname, key, req, msg) zig(keyname, key, req, msg)
resp, err := client.Do(req) 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", at)
req.Header.Set("Accept-Encoding", "gzip") 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 { if timeout > 0 {
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()

View File

@ -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 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 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. This is useful to reduce the space requirements from following image bots.
(Neither command runs vacuum, so the file size will not immediately shrink.) (Neither command runs vacuum, so the file size will not immediately shrink.)

33
honk.go
View File

@ -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() db := opendatabase()
days, err := strconv.Atoi(arg)
if err != nil {
honker := arg
expdate := time.Now().UTC().Add(-3 * 24 * time.Hour).Format(dbtimeformat) 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 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) 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)") doordie(db, "delete from files where fileid not in (select fileid from donks)")
} }
@ -1589,7 +1589,6 @@ func ElaborateUnitTests() {
} }
func main() { func main() {
var err error
cmd := "run" cmd := "run"
if len(os.Args) > 1 { if len(os.Args) > 1 {
cmd = os.Args[1] cmd = os.Args[1]
@ -1614,19 +1613,11 @@ func main() {
case "adduser": case "adduser":
adduser() adduser()
case "cleanup": case "cleanup":
days := 30 arg := "30"
if len(os.Args) > 2 { if len(os.Args) > 2 {
days, err = strconv.Atoi(os.Args[2]) arg = os.Args[2]
if err != nil {
log.Fatal(err)
} }
} cleanupdb(arg)
cleanupdb(days)
case "reduce":
if len(os.Args) < 3 {
log.Fatal("need a honker name")
}
reducedb(os.Args[2])
case "ping": case "ping":
if len(os.Args) < 4 { if len(os.Args) < 4 {
fmt.Printf("usage: honk ping from to\n") fmt.Printf("usage: honk ping from to\n")

View File

@ -112,6 +112,6 @@ func upgradedb() {
default: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }
cleanupdb(30) cleanupdb("30")
os.Exit(0) os.Exit(0)
} }