optional store traffic time

This commit is contained in:
Alireza Ahmadi
2024-04-25 21:10:02 +02:00
parent abb869c75b
commit 9a02e6593c
10 changed files with 41 additions and 6 deletions
+7 -1
View File
@@ -51,7 +51,13 @@ func (a *APP) Start() error {
if err != nil {
return err
}
err = a.cronJob.Start(loc)
trafficAge, err := a.SettingService.GetTrafficAge()
if err != nil {
return err
}
err = a.cronJob.Start(loc, trafficAge)
if err != nil {
return err
}
+2 -2
View File
@@ -14,7 +14,7 @@ func NewCronJob() *CronJob {
return &CronJob{}
}
func (c *CronJob) Start(loc *time.Location) error {
func (c *CronJob) Start(loc *time.Location, trafficAge int) error {
c.cron = cron.New(cron.WithLocation(loc), cron.WithSeconds())
c.cron.Start()
@@ -24,7 +24,7 @@ func (c *CronJob) Start(loc *time.Location) error {
// Start expiry job
c.cron.AddJob("@every 1m", NewDepleteJob())
// Start deleting old stats
c.cron.AddJob("@daily", NewDelStatsJob())
c.cron.AddJob("@daily", NewDelStatsJob(trafficAge))
}()
return nil
+6 -3
View File
@@ -7,14 +7,17 @@ import (
type DelStatsJob struct {
service.StatsService
trafficAge int
}
func NewDelStatsJob() *DelStatsJob {
return &DelStatsJob{}
func NewDelStatsJob(ta int) *DelStatsJob {
return &DelStatsJob{
trafficAge: ta,
}
}
func (s *DelStatsJob) Run() {
err := s.StatsService.DelOldStats(30)
err := s.StatsService.DelOldStats(s.trafficAge)
if err != nil {
logger.Warning("Deleting old statistics failed: ", err)
return
+5
View File
@@ -24,6 +24,7 @@ var defaultValueMap = map[string]string{
"webPath": "/app/",
"webURI": "",
"sessionMaxAge": "0",
"trafficAge": "30",
"timeLocation": "Asia/Tehran",
"subListen": "",
"subPort": "2096",
@@ -204,6 +205,10 @@ func (s *SettingService) GetSessionMaxAge() (int, error) {
return s.getInt("sessionMaxAge")
}
func (s *SettingService) GetTrafficAge() (int, error) {
return s.getInt("trafficAge")
}
func (s *SettingService) GetTimeLocation() (*time.Location, error) {
l, err := s.getString("timeLocation")
if err != nil {