-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package job | ||
|
||
import ( | ||
"fmt" | ||
"github.com/axetroy/go-server/internal/model" | ||
"github.com/axetroy/go-server/internal/service/database" | ||
"github.com/jinzhu/gorm" | ||
"time" | ||
) | ||
|
||
func generateNewLoginTableName(date time.Time) string { | ||
year := fmt.Sprintf("%d", date.Year()) | ||
month := fmt.Sprintf("%d", date.Month()) | ||
if len(month) == 1 { | ||
month = "0" + month | ||
} | ||
|
||
loginLog := model.LoginLog{} | ||
|
||
tableName := loginLog.TableName() + "_" + year + month | ||
|
||
return tableName | ||
} | ||
|
||
// 迁移数据 | ||
func moveLoginLog(startAt time.Time, endAt time.Time) (bool, error) { | ||
var ( | ||
tx = database.Db.Begin() | ||
err error | ||
eol = true // 这个时间段的数据是否已经迁移完,当 eol == true 时,则迁移下一个时间段 | ||
) | ||
|
||
defer func() { | ||
if err != nil { | ||
_ = tx.Rollback().Error | ||
} else { | ||
_ = tx.Commit().Error | ||
} | ||
}() | ||
|
||
newTableName := generateNewLoginTableName(startAt) | ||
|
||
if err = ensureLoginLogTableExist(newTableName, tx); err != nil { | ||
return true, err | ||
} | ||
|
||
logs := make([]model.LoginLog, 0) | ||
|
||
limit := 100 // 一次迁移一百条数据 | ||
|
||
// 查找在这个时间段的数据,移动到新表中 | ||
// 最早的数据排到前面 | ||
if err = tx.Model(model.LoginLog{}).Where("created_at >= ?", startAt).Where("created_at <= ?", endAt).Limit(limit).Order("created_at ASC").Find(&logs).Error; err != nil { | ||
return true, err | ||
} | ||
|
||
for _, loginLog := range logs { | ||
dataID := loginLog.Id | ||
if err = tx.Table(newTableName).Create(&loginLog).Error; err != nil { | ||
return true, err | ||
} else { | ||
// 更新表信息 - 还原 ID/创建时间/更新时间 信息 | ||
if err = tx.Table(newTableName).Where("id = ?", loginLog.Id).UpdateColumn("id", dataID).UpdateColumn("created_at", loginLog.CreatedAt).UpdateColumn("updated_at", loginLog.UpdatedAt).Error; err != nil { | ||
return true, err | ||
} | ||
|
||
// 删除旧数据 | ||
if err = tx.Unscoped().Table(loginLog.TableName()).Delete(model.LoginLog{Id: dataID}).Error; err != nil { | ||
return true, err | ||
} | ||
} | ||
} | ||
|
||
// 如果获取的数据已经不够,那么我们就认为它已经是最后一页了 | ||
eol = len(logs) < limit | ||
|
||
return eol, nil | ||
} | ||
|
||
func ensureLoginLogTableExist(tableName string, db *gorm.DB) error { | ||
// 如果表不存在,那么创建表 | ||
if db.HasTable(tableName) == false { | ||
if err := db.Table(tableName).CreateTable(model.LoginLog{}).Error; err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// 定时切割用户登录记录 | ||
// 因为这个表的内容是在是太大了 | ||
func SplitLoginLog() { | ||
var err error | ||
now := time.Now() | ||
|
||
oldestLoginLog := model.LoginLog{} | ||
|
||
if err = database.Db.Model(oldestLoginLog).Order("created_at ASC").First(&oldestLoginLog).Error; err != nil { | ||
return | ||
} | ||
|
||
startAt := time.Date(oldestLoginLog.CreatedAt.Year(), oldestLoginLog.CreatedAt.Month(), 1, 0, 0, 0, 0, now.Location()) | ||
endAt := startAt.AddDate(0, 1, 0) | ||
|
||
// 如果最旧的数据是在本月或者上个月产生的,那么跳过任务 | ||
if endAt.After(now.AddDate(0, -1, 0)) { | ||
return | ||
} | ||
|
||
for { | ||
if eol, err := moveLoginLog(startAt, endAt); err != nil { | ||
return | ||
} else if eol == true { | ||
// 开始时间往后推一个月,继续遍历 | ||
startAt = startAt.AddDate(0, 1, 0) | ||
endAt = startAt.AddDate(0, 1, 0) | ||
|
||
// 如果最旧的数据是在本月或者上个月产生的,那么跳过任务 | ||
if endAt.After(now.AddDate(0, -1, 0)) { | ||
break | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2019-2020 Axetroy. All rights reserved. MIT license. | ||
package main | ||
|
||
import ( | ||
"github.com/axetroy/go-server/cmd/scheduled/job" | ||
"github.com/axetroy/go-server/internal/library/daemon" | ||
"github.com/jasonlvhit/gocron" | ||
"github.com/urfave/cli/v2" | ||
"log" | ||
"os" | ||
) | ||
|
||
func runJobs() error { | ||
// 每天凌晨 3 点检查 login_log 表,并且进行切割数据 | ||
// 选择半夜主要是因为怕影响性能,在用户最少的情况下执行 | ||
if err := gocron.Every(1).Day().At("03:00:01").Do(job.SplitLoginLog); err != nil { | ||
return err | ||
} | ||
|
||
// 启动定时任务 | ||
<-gocron.Start() | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
app := cli.NewApp() | ||
app.Usage = "定时任务" | ||
|
||
app.Commands = []*cli.Command{ | ||
{ | ||
Name: "start", | ||
Usage: "启动定时任务", | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{ | ||
Name: "daemon, d", | ||
Usage: "是否以守护进程运行", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
// 判断当其是否是子进程,当父进程return之后,子进程会被系统1号进程接管 | ||
return daemon.Start(runJobs, c.Bool("daemon")) | ||
}, | ||
}, | ||
{ | ||
Name: "stop", | ||
Usage: "停止定时任务", | ||
Action: func(c *cli.Context) error { | ||
return daemon.Stop() | ||
}, | ||
}, | ||
} | ||
|
||
if err := app.Run(os.Args); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ targets=( | |
user | ||
resource | ||
message_queue | ||
scheduled | ||
) | ||
|
||
get_arch() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
vendor/github.com/jasonlvhit/gocron/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.