Skip to content

Instantly share code, notes, and snippets.

@pzillmann
Created August 20, 2018 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pzillmann/f124f7c1932093c976ed3727615cc07a to your computer and use it in GitHub Desktop.
Save pzillmann/f124f7c1932093c976ed3727615cc07a to your computer and use it in GitHub Desktop.
Remove rspamd history values from redis database after x hours
package main
import (
"encoding/json"
"os"
"time"
"github.com/mediocregopher/radix.v2/redis"
)
func main() {
const dataset string = "rs_historyHOSTNAME"
const hours int = 24
var j interface{}
var r *redis.Resp
client, err := redis.Dial("unix", "/var/run/redis/redis.sock")
if err != nil {
panic(err)
}
for {
r = client.Cmd("lindex", dataset, "-1")
if r.Err != nil {
panic(r.Err)
}
b, err := r.Bytes()
err = json.Unmarshal(b, &j)
if err != nil {
os.Exit(1)
}
j_ta := j.(map[string]interface{})
timestamp_int := (int64)(j_ta["unix_time"].(float64))
mailtime := time.Unix(timestamp_int, 0).Add(time.Duration(hours) * time.Hour)
if mailtime.Before(time.Now()) {
if client.Cmd("RPOP", dataset).Err != nil {
os.Exit(3)
}
} else {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment