2016-05-02 21:36:49 -04:00
|
|
|
package trantor
|
2014-02-09 23:22:45 +01:00
|
|
|
|
|
|
|
import (
|
2014-08-30 13:17:50 -05:00
|
|
|
log "github.com/cihub/seelog"
|
|
|
|
|
2014-02-09 23:22:45 +01:00
|
|
|
"time"
|
2014-08-30 13:17:50 -05:00
|
|
|
|
2016-05-02 21:36:49 -04:00
|
|
|
"gitlab.com/trantor/trantor/lib/database"
|
2014-02-09 23:22:45 +01:00
|
|
|
)
|
|
|
|
|
2014-06-29 19:41:29 -05:00
|
|
|
func InitTasks(db *database.DB) {
|
2016-05-02 21:36:49 -04:00
|
|
|
periodicTask(UpdateLogger, MINUTES_UPDATE_LOGGER*time.Minute)
|
2014-02-11 11:02:22 +01:00
|
|
|
periodicTask(db.UpdateTags, MINUTES_UPDATE_TAGS*time.Minute)
|
|
|
|
periodicTask(db.UpdateMostVisited, MINUTES_UPDATE_VISITED*time.Minute)
|
|
|
|
periodicTask(db.UpdateDownloadedBooks, MINUTES_UPDATE_DOWNLOADED*time.Minute)
|
2014-02-11 11:12:36 +01:00
|
|
|
periodicTask(db.UpdateHourVisits, MINUTES_UPDATE_HOURLY_V*time.Minute)
|
|
|
|
periodicTask(db.UpdateDayVisits, MINUTES_UPDATE_DAILY_V*time.Minute)
|
|
|
|
periodicTask(db.UpdateMonthVisits, MINUTES_UPDATE_MONTHLY_V*time.Minute)
|
|
|
|
periodicTask(db.UpdateHourDownloads, MINUTES_UPDATE_HOURLY_D*time.Minute)
|
|
|
|
periodicTask(db.UpdateDayDownloads, MINUTES_UPDATE_DAILY_D*time.Minute)
|
|
|
|
periodicTask(db.UpdateMonthDownloads, MINUTES_UPDATE_MONTHLY_D*time.Minute)
|
2014-02-11 11:02:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func periodicTask(task func() error, periodicity time.Duration) {
|
2014-02-09 23:22:45 +01:00
|
|
|
go tasker(task, periodicity)
|
|
|
|
}
|
|
|
|
|
2014-02-11 11:02:22 +01:00
|
|
|
func tasker(task func() error, periodicity time.Duration) {
|
2014-02-09 23:22:45 +01:00
|
|
|
for true {
|
|
|
|
time.Sleep(periodicity)
|
2014-02-11 11:20:38 +01:00
|
|
|
err := task()
|
|
|
|
if err != nil {
|
2014-02-11 13:13:43 +01:00
|
|
|
log.Error("Task error: ", err)
|
2014-02-11 11:20:38 +01:00
|
|
|
}
|
2014-02-09 23:22:45 +01:00
|
|
|
}
|
|
|
|
}
|