Archived
1
0
Fork 0

Keep track and remove stale products

Products not updated since a while are not supposed to stay in the database nor
exposed via the API. This commit automatically updates all detected products
with the current date and adds a "retention" flag (number of days) to remove old
products. This flag is disabled by default.

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2021-04-06 09:22:22 +02:00
parent 7c2c1032cb
commit 81258ed935
No known key found for this signature in database
GPG key ID: FF42D23B580C89F7

25
main.go
View file

@ -52,6 +52,7 @@ func main() {
workers := flag.Int("workers", 1, "number of workers for parsing shops")
pidFile := flag.String("pid-file", "", "write process ID to this file to disable concurrent executions")
pidWaitTimeout := flag.Int("pid-wait-timeout", 0, "seconds to wait before giving up when another instance is running")
retention := flag.Int("retention", 0, "Automatically remove products from the database with this number of days old (disabled by default)")
api := flag.Bool("api", false, "Start the HTTP API")
flag.Parse()
@ -115,6 +116,23 @@ func main() {
log.Fatalf("cannot create shops table")
}
// delete products not updated since retention
if *retention != 0 {
var oldProducts []Product
retentionDate := time.Now().Local().Add(-time.Hour * 24 * time.Duration(*retention))
trx := db.Where("updated_at < ?", retentionDate).Find(&oldProducts)
if trx.Error != nil {
log.Warnf("cannot find stale products: %s", trx.Error)
}
for _, p := range oldProducts {
log.Debugf("found old product: %s", p.Name)
if trx = db.Unscoped().Delete(&p); trx.Error != nil {
log.Warnf("cannot remove stale product %: %s", p, trx.Error)
}
log.Printf("stale product %s (%s) removed from database", p.Name, p.URL)
}
}
// start the api
if *api {
log.Fatal(StartAPI(db, config.APIConfig))
@ -294,6 +312,13 @@ func handleProducts(parser Parser, notifiers []Notifier, db *gorm.DB, wg *sync.W
}
}
}
// keep track of active products
dbProduct.UpdatedAt = time.Now().Local()
if trx := db.Save(&dbProduct); trx.Error != nil {
log.Warnf("cannot update product %s to database: %s", dbProduct.Name, trx.Error)
}
}
}