Skip to content

Commit

Permalink
fix: 新增记录变更后的版本ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambition9186 committed Sep 12, 2024
1 parent c60c11c commit eb0fb08
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
22 changes: 20 additions & 2 deletions client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,25 @@ func (r *Release) Execute(steps ...Function) error {
}

// sendVersionChangeMessaging 发送客户端版本变更信息
func (r *Release) sendVersionChangeMessaging(bd *sfs.BasicData) error {
func (r *Release) sendVersionChangeMessaging(bd *sfs.BasicData) (err error) {
r.AppMate.FailedDetailReason = util.TruncateString(r.AppMate.FailedDetailReason, 1024)

defer func(r *Release) {
// 在上报完消息后记录变更的ID(处理上报中)
if r.AppMate.ReleaseChangeStatus != sfs.Processing {
err = eventmeta.RecordChangedReleaseID(r.AppDir, r.AppMate.TargetReleaseID)
}
}(r)

rid, err := eventmeta.GetLatestReleaseIDFromFile(r.AppDir)
if err != nil {
return err
}

if rid > 0 {
r.AppMate.CurrentReleaseID = rid
}

pullPayload := sfs.VersionChangePayload{
BasicData: bd,
Application: r.AppMate,
Expand All @@ -429,7 +446,8 @@ func (r *Release) sendVersionChangeMessaging(bd *sfs.BasicData) error {
if err != nil {
return err
}
return nil

return err
}

// handleBasicData 处理基础数据
Expand Down
5 changes: 2 additions & 3 deletions client/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ func (w *watcher) OnReleaseChange(event *sfs.ReleaseChangeEvent) { // nolint
}
}
}(ctx)

// 不管是否成功都需要把当前ID变更成目标ID
subscriber.CurrentReleaseID = pl.ReleaseMeta.ReleaseID
subscriber.ReleaseChangeStatus = sfs.Processing
if err := subscriber.Callback(release); err != nil {
cancel()
Expand All @@ -353,8 +354,6 @@ func (w *watcher) OnReleaseChange(event *sfs.ReleaseChangeEvent) { // nolint
cancel()
subscriber.ReleaseChangeStatus = sfs.Success
subscriber.reportReleaseChangeCallbackMetrics("success", start)

subscriber.CurrentReleaseID = pl.ReleaseMeta.ReleaseID
}
}
}
Expand Down
84 changes: 84 additions & 0 deletions internal/util/eventmeta/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"

sfs "github.com/TencentBlueKing/bk-bcs/bcs-services/bcs-bscp/pkg/sf-share"
"golang.org/x/exp/slog"
Expand Down Expand Up @@ -142,3 +143,86 @@ func GetLatestMetadataFromFile(tempDir string) (*EventMeta, bool, error) {

return metadata, true, nil
}

// RecordChangedReleaseID 记录更改后的 release ID
// 不管是否成功
func RecordChangedReleaseID(tempDir string, releaseID uint32) error {

if tempDir == "" {
return sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.FilePathNotFound,
Err: errors.New("the file path for recording change release ID is empty")})
}

// prepare temp dir, make sure it exists
if err := os.MkdirAll(tempDir, os.ModePerm); err != nil {
return sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.NewFolderFailed, Err: err})
}

metaFilePath := filepath.Join(tempDir, "release_id_file")

metaFile, err := os.OpenFile(metaFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
return sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.OpenFileFailed,
Err: fmt.Errorf("open release id file failed, err: %s", err.Error())})
}
defer metaFile.Close()

releaseIDStr := strconv.FormatUint(uint64(releaseID), 10)
if _, err := metaFile.WriteString(releaseIDStr + "\n"); err != nil {
return sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.WriteFileFailed,
Err: fmt.Errorf("append release id to release id file failed, err: %s", err.Error())})
}
logger.Info("append release id to release id file success", slog.String("releaseID", releaseIDStr))

return nil
}

// GetLatestReleaseIDFromFile get the last data from the file that records the release id
func GetLatestReleaseIDFromFile(tempDir string) (uint32, error) {
if tempDir == "" {
return 0, sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.DataEmpty,
Err: errors.New("the file path for recording change release ID is empty")})
}

metaFilePath := filepath.Join(tempDir, "release_id_file")

metaFile, err := os.Open(metaFilePath)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return 0, sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.OpenFileFailed,
Err: err})
}
defer metaFile.Close()
var lastLine string
scanner := bufio.NewScanner(metaFile)
for scanner.Scan() {
lastLine = scanner.Text()
}

if len(lastLine) == 0 {
return 0, nil
}

if err = scanner.Err(); err != nil {
return 0, sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.ReadFileFailed,
Err: err})
}

releaseID, err := strconv.ParseUint(lastLine, 10, 32)
if err != nil {
return 0, sfs.WrapPrimaryError(sfs.UnknownFailed,
sfs.SecondaryError{SpecificFailedReason: sfs.ReadFileFailed,
Err: err})
}

return uint32(releaseID), nil
}

0 comments on commit eb0fb08

Please sign in to comment.