mirror of
https://github.com/muety/wakapi.git
synced 2023-08-10 21:12:56 +03:00
Add database cleanup functionality.
This commit is contained in:
parent
c7556fa9a5
commit
33ebb8a771
@ -2,6 +2,9 @@
|
|||||||
listen = 127.0.0.1
|
listen = 127.0.0.1
|
||||||
port = 3000
|
port = 3000
|
||||||
|
|
||||||
|
[app]
|
||||||
|
cleanup = true
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
max_connections = 2
|
max_connections = 2
|
||||||
|
|
||||||
|
9
main.go
9
main.go
@ -55,6 +55,8 @@ func readConfig() *models.Config {
|
|||||||
port = cfg.Section("server").Key("port").MustInt()
|
port = cfg.Section("server").Key("port").MustInt()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cleanUp := cfg.Section("app").Key("cleanup").MustBool(false)
|
||||||
|
|
||||||
// Read custom languages
|
// Read custom languages
|
||||||
customLangs := make(map[string]string)
|
customLangs := make(map[string]string)
|
||||||
languageKeys := cfg.Section("languages").Keys()
|
languageKeys := cfg.Section("languages").Keys()
|
||||||
@ -73,6 +75,7 @@ func readConfig() *models.Config {
|
|||||||
DbName: dbName,
|
DbName: dbName,
|
||||||
DbDialect: "mysql",
|
DbDialect: "mysql",
|
||||||
DbMaxConn: dbMaxConn,
|
DbMaxConn: dbMaxConn,
|
||||||
|
CleanUp: cleanUp,
|
||||||
CustomLanguages: customLangs,
|
CustomLanguages: customLangs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +86,7 @@ func main() {
|
|||||||
|
|
||||||
// Connect to database
|
// Connect to database
|
||||||
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
|
db, err := gorm.Open(config.DbDialect, utils.MakeConnectionString(config))
|
||||||
db.LogMode(false)
|
db.LogMode(config.IsDev())
|
||||||
db.DB().SetMaxIdleConns(int(config.DbMaxConn))
|
db.DB().SetMaxIdleConns(int(config.DbMaxConn))
|
||||||
db.DB().SetMaxOpenConns(int(config.DbMaxConn))
|
db.DB().SetMaxOpenConns(int(config.DbMaxConn))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -118,6 +121,10 @@ func main() {
|
|||||||
// Aggregate heartbeats to summaries and persist them
|
// Aggregate heartbeats to summaries and persist them
|
||||||
go aggregationSrvc.Schedule()
|
go aggregationSrvc.Schedule()
|
||||||
|
|
||||||
|
if config.CleanUp {
|
||||||
|
go heartbeatSrvc.ScheduleCleanUp()
|
||||||
|
}
|
||||||
|
|
||||||
// Handlers
|
// Handlers
|
||||||
heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc}
|
heartbeatHandler := &routes.HeartbeatHandler{HeartbeatSrvc: heartbeatSrvc}
|
||||||
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
summaryHandler := &routes.SummaryHandler{SummarySrvc: summarySrvc}
|
||||||
|
@ -11,6 +11,7 @@ type Config struct {
|
|||||||
DbName string
|
DbName string
|
||||||
DbDialect string
|
DbDialect string
|
||||||
DbMaxConn uint
|
DbMaxConn uint
|
||||||
|
CleanUp bool
|
||||||
CustomLanguages map[string]string
|
CustomLanguages map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
intervalDays int = 1 // TODO: Make configurable
|
aggregateIntervalDays int = 1 // TODO: Make configurable
|
||||||
)
|
)
|
||||||
|
|
||||||
type AggregationService struct {
|
type AggregationService struct {
|
||||||
@ -126,7 +126,7 @@ func generateUserJobs(userId string, lastAggregation time.Time, jobs chan<- *Agg
|
|||||||
from = time.Date(
|
from = time.Date(
|
||||||
lastAggregation.Year(),
|
lastAggregation.Year(),
|
||||||
lastAggregation.Month(),
|
lastAggregation.Month(),
|
||||||
lastAggregation.Day()+intervalDays,
|
lastAggregation.Day()+aggregateIntervalDays,
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
lastAggregation.Location(),
|
lastAggregation.Location(),
|
||||||
)
|
)
|
||||||
@ -136,7 +136,7 @@ func generateUserJobs(userId string, lastAggregation time.Time, jobs chan<- *Agg
|
|||||||
to = time.Date(
|
to = time.Date(
|
||||||
from.Year(),
|
from.Year(),
|
||||||
from.Month(),
|
from.Month(),
|
||||||
from.Day()+intervalDays,
|
from.Day()+aggregateIntervalDays,
|
||||||
0, 0, 0, 0,
|
0, 0, 0, 0,
|
||||||
from.Location(),
|
from.Location(),
|
||||||
)
|
)
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/jasonlvhit/gocron"
|
||||||
|
"github.com/n1try/wakapi/utils"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
@ -8,7 +11,10 @@ import (
|
|||||||
gormbulk "github.com/t-tiger/gorm-bulk-insert"
|
gormbulk "github.com/t-tiger/gorm-bulk-insert"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TableHeartbeat = "heartbeat"
|
const (
|
||||||
|
TableHeartbeat = "heartbeat"
|
||||||
|
cleanUpInterval = time.Duration(aggregateIntervalDays) * 2 * 24 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
type HeartbeatService struct {
|
type HeartbeatService struct {
|
||||||
Config *models.Config
|
Config *models.Config
|
||||||
@ -55,3 +61,29 @@ func (srv *HeartbeatService) GetFirstUserHeartbeats(userIds []string) ([]*models
|
|||||||
}
|
}
|
||||||
return heartbeats, nil
|
return heartbeats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (srv *HeartbeatService) DeleteBefore(t time.Time) error {
|
||||||
|
if err := srv.Db.
|
||||||
|
Where("time <= ?", t).
|
||||||
|
Delete(models.Heartbeat{}).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *HeartbeatService) CleanUp() error {
|
||||||
|
refTime := utils.StartOfDay().Add(-cleanUpInterval)
|
||||||
|
if err := srv.DeleteBefore(refTime); err != nil {
|
||||||
|
log.Printf("Failed to clean up heartbeats older than %v – %v\n", refTime, err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
log.Printf("Successfully cleaned up heartbeats older than %v\n", refTime)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *HeartbeatService) ScheduleCleanUp() {
|
||||||
|
srv.CleanUp()
|
||||||
|
|
||||||
|
gocron.Every(1).Day().At("02:30").Do(srv.CleanUp)
|
||||||
|
<-gocron.Start()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user