You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
package timer
|
|
|
|
import (
|
|
"context"
|
|
"yunyan/comm"
|
|
redissys "yunyan/lego/sys/redis"
|
|
"yunyan/modules"
|
|
"fmt"
|
|
"time"
|
|
|
|
"yunyan/lego/base"
|
|
"yunyan/lego/core"
|
|
"yunyan/lego/sys/cron"
|
|
)
|
|
|
|
type uselogTimer struct {
|
|
modules.MCompHttpGate
|
|
service base.IRPCXService
|
|
module *Timer
|
|
options *Options
|
|
}
|
|
|
|
func (this *uselogTimer) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
|
|
this.MCompHttpGate.Init(service, module, comp, options)
|
|
this.service = service.(base.IRPCXService)
|
|
this.module = module.(*Timer)
|
|
this.options = options.(*Options)
|
|
|
|
return
|
|
}
|
|
|
|
func (this *uselogTimer) Start() (err error) {
|
|
err = this.MCompHttpGate.Start()
|
|
//凌晨1分1秒执行
|
|
cron.AddFunc("1 1 0 * * ?", this.timer)
|
|
return
|
|
}
|
|
|
|
func (this *uselogTimer) timer() {
|
|
fmt.Println("开始读取 log:* 数据...", time.Now().Format("15:04:05"))
|
|
ctx := context.Background()
|
|
// 使用 SCAN 而非 KEYS(更安全)
|
|
var (
|
|
cursor uint64
|
|
datas []map[string]string = make([]map[string]string, 0)
|
|
)
|
|
for {
|
|
keys, newCursor, err := redissys.Conn().Scan(ctx, cursor, redissys.RKey(fmt.Sprintf("%s:*", comm.TableUseRecordLog)), 100).Result()
|
|
if err != nil {
|
|
fmt.Println("Scan error:", err)
|
|
break
|
|
}
|
|
|
|
for _, key := range keys {
|
|
data, err := redissys.Conn().HGetAll(ctx, key).Result()
|
|
if err != nil {
|
|
fmt.Println("HGETALL error:", err)
|
|
continue
|
|
}
|
|
datas = append(datas, data)
|
|
// ✅ 这里处理数据,比如打印 / 存储到 DB
|
|
fmt.Printf("处理 %s: %+v\n", key, data)
|
|
|
|
// 删除日志 key
|
|
if err := redissys.Conn().Del(ctx, key).Err(); err != nil {
|
|
fmt.Println("DEL error:", err)
|
|
} else {
|
|
fmt.Printf("已删除 %s\n", key)
|
|
}
|
|
}
|
|
|
|
if newCursor == 0 {
|
|
break
|
|
}
|
|
cursor = newCursor
|
|
}
|
|
|
|
fmt.Println("日志处理完成。")
|
|
}
|
|
|