53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package trantor
|
|
|
|
import (
|
|
log "github.com/cihub/seelog"
|
|
|
|
"time"
|
|
|
|
"gitlab.com/trantor/trantor/lib/database"
|
|
)
|
|
|
|
const (
|
|
minutesUpdateTags = 11
|
|
minutesUpdateVisited = 41
|
|
minutesUpdateDownloaded = 47
|
|
minutesUpdateHourlyV = 31
|
|
minutesUpdateDailyV = 60*12 + 7
|
|
minutesUpdateMonthlyV = 60*24 + 11
|
|
minutesUpdateHourlyD = 29
|
|
minutesUpdateDailyD = 60*12 + 13
|
|
minutesUpdateMontlyD = 60*24 + 17
|
|
minutesUpdateLogger = 5
|
|
)
|
|
|
|
func InitTasks(db database.DB, loggerConfig string) {
|
|
updateLogger := func() error {
|
|
return UpdateLogger(loggerConfig)
|
|
}
|
|
periodicTask(updateLogger, minutesUpdateLogger*time.Minute)
|
|
|
|
periodicTask(db.UpdateTags, minutesUpdateTags*time.Minute)
|
|
periodicTask(db.UpdateMostVisited, minutesUpdateVisited*time.Minute)
|
|
periodicTask(db.UpdateDownloadedBooks, minutesUpdateDownloaded*time.Minute)
|
|
periodicTask(db.UpdateHourVisits, minutesUpdateHourlyV*time.Minute)
|
|
periodicTask(db.UpdateDayVisits, minutesUpdateDailyV*time.Minute)
|
|
periodicTask(db.UpdateMonthVisits, minutesUpdateMonthlyV*time.Minute)
|
|
periodicTask(db.UpdateHourDownloads, minutesUpdateHourlyD*time.Minute)
|
|
periodicTask(db.UpdateDayDownloads, minutesUpdateDailyD*time.Minute)
|
|
periodicTask(db.UpdateMonthDownloads, minutesUpdateMontlyD*time.Minute)
|
|
}
|
|
|
|
func periodicTask(task func() error, periodicity time.Duration) {
|
|
go tasker(task, periodicity)
|
|
}
|
|
|
|
func tasker(task func() error, periodicity time.Duration) {
|
|
for true {
|
|
time.Sleep(periodicity)
|
|
err := task()
|
|
if err != nil {
|
|
log.Error("Task error: ", err)
|
|
}
|
|
}
|
|
}
|