-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
96 lines (78 loc) · 2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/go-redis/redis/v8"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
"gitlab.com/promptech1/infuser-author/app"
"gitlab.com/promptech1/infuser-author/app/ctx"
"gitlab.com/promptech1/infuser-author/constant"
"gitlab.com/promptech1/infuser-author/model"
)
var (
network = flag.String("network", "tcp", `one of "tcp" or "unix". Must be consistent to -endpoint`)
port = flag.Int("service port", 9090, "listen port")
)
func main() {
flag.Parse()
ctx := context.Background()
ballast := make([]byte, 10<<24)
_ = ballast
var (
err error
a *app.Application
)
a, err = app.New(ctx)
if err != nil {
log.Fatal("error ", os.Args[0]+" initialization error: "+err.Error())
os.Exit(1)
}
// 주기적 통계 데이터 저장 처리
// go runCron(a.Ctx)
a.Run(*network, fmt.Sprintf(":%d", *port))
}
func runCron(ctx *ctx.Context) {
c := cron.New()
c.AddFunc("* * * * *", func() {
_, err := ctx.RedisDB.LPop(constant.KEY_TRAFFIC_QUEUE)
if err != nil && err == redis.Nil {
fmt.Println("Ignore stat ======= ")
return
}
members, err := ctx.RedisDB.SMembers(constant.KEY_TRAFFIC_SET)
if err != nil && err == redis.Nil {
fmt.Println("no members ======= ")
return
}
var histories []model.AppTokenHistory
for _, appTokenKey := range members {
cntInfo, err := ctx.RedisDB.Get(appTokenKey, "uint")
if err == nil {
count := cntInfo.(uint)
if count > 0 {
ctx.RedisDB.Delete(appTokenKey)
temp1 := strings.Split(appTokenKey, ":")[1]
temp, _ := strconv.Atoi(temp1)
appTokenId := uint(temp)
histories = append(histories, model.AppTokenHistory{
AppTokenId: appTokenId,
CallTraffic: count,
})
}
}
}
if len(histories) > 0 {
ctx.Orm.Insert(&histories)
}
ctx.RedisDB.LPush(constant.KEY_TRAFFIC_QUEUE, "1")
time.Sleep(10 * 1000 * time.Millisecond)
fmt.Println("Run Every min: ", time.Now().String())
})
c.Start()
}