2015-03-25 00:16:52 -07:00
|
|
|
// Copyright (C) 2014-2015 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cleansrv struct {
|
|
|
|
intv time.Duration
|
|
|
|
db *sql.DB
|
|
|
|
prep map[string]*sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cleansrv) Serve() {
|
|
|
|
for {
|
|
|
|
time.Sleep(next(s.intv))
|
|
|
|
|
2015-07-21 15:56:27 -07:00
|
|
|
err := s.cleanOldEntries()
|
2015-03-25 00:16:52 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Clean:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *cleansrv) Stop() {
|
|
|
|
panic("stop unimplemented")
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:56:27 -07:00
|
|
|
func (s *cleansrv) cleanOldEntries() (err error) {
|
2015-03-25 00:16:52 -07:00
|
|
|
var tx *sql.Tx
|
|
|
|
tx, err = s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil {
|
|
|
|
err = tx.Commit()
|
|
|
|
} else {
|
|
|
|
tx.Rollback()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
res, err := tx.Stmt(s.prep["cleanAddress"]).Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rows, _ := res.RowsAffected(); rows > 0 {
|
|
|
|
log.Printf("Clean: %d old addresses", rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = tx.Stmt(s.prep["cleanDevice"]).Exec()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rows, _ := res.RowsAffected(); rows > 0 {
|
|
|
|
log.Printf("Clean: %d old devices", rows)
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:46:43 -07:00
|
|
|
var devs, addrs int
|
2015-03-25 00:16:52 -07:00
|
|
|
row := tx.Stmt(s.prep["countDevice"]).QueryRow()
|
|
|
|
if err = row.Scan(&devs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
row = tx.Stmt(s.prep["countAddress"]).QueryRow()
|
|
|
|
if err = row.Scan(&addrs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:46:43 -07:00
|
|
|
log.Printf("Database: %d devices, %d addresses", devs, addrs)
|
2015-03-25 00:16:52 -07:00
|
|
|
return nil
|
|
|
|
}
|