gitee.com/quant1x/engine@v1.8.4/services/services_state.go (about)

     1  package services
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/cache"
     6  	"gitee.com/quant1x/exchange"
     7  	"gitee.com/quant1x/gox/api"
     8  	"os"
     9  	"path/filepath"
    10  	"time"
    11  )
    12  
    13  const (
    14  	// 状态文件时间格式
    15  	timeLayoutOfState = "150405"
    16  )
    17  
    18  func stateFilename(date, timestamp string) string {
    19  	date = exchange.FixTradeDate(date)
    20  	t, _ := time.ParseInLocation(exchange.CN_SERVERTIME_FORMAT, timestamp, time.Local)
    21  	timestamp = t.Format(timeLayoutOfState)
    22  	tm := date + "T" + timestamp
    23  	filename := fmt.Sprintf("%s/update.%s", cache.GetVariablePath(), tm)
    24  	return filename
    25  }
    26  
    27  // 状态文件不存在则可更新, 反之不可更新
    28  func checkUpdateState(date, timestamp string) bool {
    29  	filename := stateFilename(date, timestamp)
    30  	return !api.FileExist(filename)
    31  }
    32  
    33  // 确定完成当期更新状态
    34  func doneUpdate(date, timestamp string) {
    35  	filename := stateFilename(date, timestamp)
    36  	file, err := os.Create(filename)
    37  	if err != nil {
    38  		return
    39  	}
    40  	api.CloseQuietly(file)
    41  }
    42  
    43  // 清理过期的状态文件
    44  func cleanExpiredStateFiles() error {
    45  	statePath := cache.GetVariablePath()
    46  	pattern := filepath.Join(statePath, "update.*")
    47  	filePaths, err := filepath.Glob(pattern)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	for _, filePath := range filePaths {
    52  		_ = os.Remove(filePath)
    53  	}
    54  	return nil
    55  }