Commit c62f77b1 authored by Eric's avatar Eric

[Update] fix remain replay file upload.

parent e41d81df
......@@ -30,23 +30,51 @@ func uploadRemainReplay(rootPath string) {
logger.Debugf("upload failed replay err: %s", err.Error())
return
}
allRemainFiles := make(map[string]string)
_ = filepath.Walk(replayDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
var sid string
filename := info.Name()
if len(filename) == 36{
sid = filename
}
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)
sidName := strings.Split(filename, ".")[0]
if len(sidName) == 36 {
sid = sidName
}
}
if sid != ""{
data := map[string]interface{}{"id":sid,"date_end":info.ModTime().UTC().Format(
"2006-01-02 15:04:05 +0000")}
service.FinishSession(data)
allRemainFiles[sid] = path
}
return nil
})
for sid, path := range allRemainFiles{
var absGzPath string
if strings.HasSuffix(path, ".replay.gz") {
absGzPath = path
}else if strings.HasSuffix(path, sid) {
if err := ValidateRemainReplayFile(path); err != nil{
continue
}
absGzPath = path + ".replay.gz"
if err := common.GzipCompressFile(path,absGzPath); err != nil{
continue
}
_ = os.Remove(path)
}
relayRecord := &proxy.ReplyRecorder{}
relayRecord.AbsGzFilePath = absGzPath
relayRecord.Target, _ = filepath.Rel(path, rootPath)
relayRecord.UploadGzipFile(3)
}
logger.Debug("Upload remain replay done")
}
......@@ -66,3 +94,29 @@ func keepHeartbeat(interval time.Duration) {
}
}
}
func ValidateRemainReplayFile(path string) error{
f, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND,os.ModePerm)
if err != nil{
return err
}
defer f.Close()
tmp := make([]byte,1)
_, err = f.Seek(-1,2)
if err != nil{
return err
}
_, err = f.Read(tmp)
if err != nil{
return err
}
switch string(tmp) {
case "}":
return nil
case ",":
_,err = f.Write([]byte(`"0":""}`))
default:
_,err = f.Write([]byte(`}`))
}
return err
}
\ No newline at end of file
......@@ -105,7 +105,6 @@ type ReplyRecorder struct {
}
func (r *ReplyRecorder) initial() {
r.storage = NewReplayStorage()
r.backOffStorage = defaultReplayStorage
r.prepare()
}
......@@ -171,6 +170,9 @@ func (r *ReplyRecorder) uploadReplay() {
}
func (r *ReplyRecorder) UploadGzipFile(maxRetry int) {
if r.storage == nil{
r.storage = NewReplayStorage()
}
for i := 0; i <= maxRetry; i++ {
logger.Debug("Upload replay file: ", r.AbsGzFilePath)
err := r.storage.Upload(r.AbsGzFilePath, r.Target)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment