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:
parent
7c2c1032cb
commit
81258ed935
1 changed files with 25 additions and 0 deletions
25
main.go
25
main.go
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue