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
package coco
import (
"os"
"path/filepath"
"strings"
"time"
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/proxy"
"cocogo/pkg/service"
)
func Initial() {
conf := config.GetConf()
if conf.UploadFailedReplay {
go uploadFailedReplay(conf.RootPath)
}
go keepHeartbeat(conf.HeartbeatDuration)
}
func uploadFailedReplay(rootPath string) {
replayDir := filepath.Join(rootPath, "data", "replays")
err := common.EnsureDirExist(replayDir)
if err != nil {
logger.Debugf("upload failed replay err: %s", err.Error())
return
}
_ = filepath.Walk(replayDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
filename := info.Name()
if strings.HasSuffix(filename, ".replay.gz") {
sid := strings.Split(filename, ".")[0]
if len(sid) == 36 {
relayRecord := proxy.NewReplyRecord(sid)
relayRecord.AbsGzFilePath = path
relayRecord.Target, _ = filepath.Rel(path, rootPath)
go relayRecord.UploadGzipFile(3)
}
}
return nil
})
logger.Debug("upload Replay Done")
}
func keepHeartbeat(interval int) {
tick := time.Tick(time.Duration(interval) * time.Second)
for {
select {
case <-tick:
data := proxy.GetAliveSessions()
tasks := service.TerminalHeartBeat(data)
if len(tasks) != 0 {
for _, task := range tasks {
proxy.HandlerSessionTask(task)
}
}
}
}
}