Skip to content

Commit

Permalink
修复限期到期不马上生效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Dec 16, 2020
1 parent dd2c7af commit 70db163
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
21 changes: 10 additions & 11 deletions core/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,47 +201,46 @@ func (mysql *Mysql) MonthlyResetData() error {
defer db.Close()
userList, err := queryUserList(db, "SELECT * FROM users WHERE useDays != 0 AND quota != 0")
if err != nil {
fmt.Println(err)
return err
}
for _, user := range userList {
if _, err := db.Exec(fmt.Sprintf("UPDATE users SET download=0, upload=0 WHERE id=%d;", user.ID)); err != nil {
fmt.Println(err)
return err
}
}
return nil
}

// DailyCheckExpire 检查是否有过期,过期了设置流量上限为0
func (mysql *Mysql) DailyCheckExpire() error {
func (mysql *Mysql) DailyCheckExpire() (bool, error) {
needRestart := false
now := time.Now()
utc, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println(err)
return err
return false, err
}
addDay, _ := time.ParseDuration("-24h")
todayDay := now.Add(addDay).In(utc).Format("2006-01-02")
db := mysql.GetDB()
if db == nil {
return errors.New("can't connect mysql")
return false, errors.New("can't connect mysql")
}
defer db.Close()
userList, err := queryUserList(db, "SELECT * FROM users WHERE useDays != 0 AND quota != 0")
if err != nil {
fmt.Println(err)
return err
return false, err
}
for _, user := range userList {
if user.ExpiryDate == todayDay {
if _, err := db.Exec(fmt.Sprintf("UPDATE users SET quota=0 WHERE id=%d;", user.ID)); err != nil {
fmt.Println(err)
return err
return false, err
}
if !needRestart {
needRestart = true
}
}
}
return nil
return needRestart, nil
}

// CancelExpire 取消过期时间
Expand Down
11 changes: 9 additions & 2 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"trojan/core"
"trojan/trojan"
"trojan/util"
"trojan/web/controller"
)
Expand Down Expand Up @@ -144,11 +145,17 @@ func sheduleTask() {
c := cron.New()
c.AddFunc("CRON_TZ=Asia/Shanghai @monthly", func() {
mysql := core.GetMysql()
mysql.MonthlyResetData()
if err := mysql.MonthlyResetData(); err != nil {
fmt.Println("MonthlyResetError: " + err.Error())
}
})
c.AddFunc("CRON_TZ=Asia/Shanghai @daily", func() {
mysql := core.GetMysql()
mysql.DailyCheckExpire()
if needRestart, err := mysql.DailyCheckExpire(); err != nil {
fmt.Println("DailyCheckError: " + err.Error())
} else if needRestart {
trojan.Restart()
}
})
c.Start()
}
Expand Down

0 comments on commit 70db163

Please sign in to comment.