|
@@ -7,7 +7,6 @@ import (
|
|
|
"io/ioutil"
|
|
|
"os"
|
|
|
"path"
|
|
|
- "strconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -56,18 +55,23 @@ func (this *command) call(host string) bool {
|
|
|
}
|
|
|
|
|
|
type (
|
|
|
+ historyFile struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ LoadTime int64 `json:"loadTime"`
|
|
|
+ }
|
|
|
server struct {
|
|
|
- dirCommand string
|
|
|
- dirHistory string
|
|
|
- logs []string
|
|
|
+ dirCommand string
|
|
|
+ fileHistory string
|
|
|
+ mapHistoryFile map[string]*historyFile
|
|
|
+ logs []string
|
|
|
}
|
|
|
)
|
|
|
|
|
|
-func (this *server) setDirCommand(dir string) {
|
|
|
- this.dirCommand = dir
|
|
|
+func (this *historyFile) setName(name string) {
|
|
|
+ this.Name = name
|
|
|
}
|
|
|
-func (this *server) setDirHistory(dir string) {
|
|
|
- this.dirHistory = dir
|
|
|
+func (this *historyFile) setLoadTime(loadTime int64) {
|
|
|
+ this.LoadTime = loadTime
|
|
|
}
|
|
|
|
|
|
func (this *server) getLogs() []string {
|
|
@@ -80,19 +84,59 @@ func (this *server) printLogs() {
|
|
|
this.logs = this.logs[:0]
|
|
|
}
|
|
|
|
|
|
+func (this *server) loadHistoryFile() bool {
|
|
|
+ var sli []*historyFile
|
|
|
+ if err := common.LoadFileUnmarshal(this.fileHistory, &sli); nil != err {
|
|
|
+ common.Log.Info(err)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ for _, v := range sli {
|
|
|
+ this.mapHistoryFile[v.Name] = v
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
func (this *server) init() bool {
|
|
|
+ this.mapHistoryFile = make(map[string]*historyFile)
|
|
|
conf := common.GetConf()
|
|
|
dirCommand := conf.GetCommandDir()
|
|
|
- dirHistory := path.Join(path.Dir(dirCommand), ".history")
|
|
|
- if err := os.MkdirAll(dirHistory, 0755); nil != err {
|
|
|
- this.logs = append(this.logs, fmt.Sprintf("mkdir history dir:%v", err))
|
|
|
+ this.dirCommand = dirCommand
|
|
|
+ this.fileHistory = path.Join(path.Dir(dirCommand), ".history")
|
|
|
+ if _, err := os.Stat(this.fileHistory); os.IsNotExist(err) {
|
|
|
+ if _, err = os.Create(this.fileHistory); nil != err {
|
|
|
+ common.Log.Info(err)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return this.loadHistoryFile()
|
|
|
+}
|
|
|
+
|
|
|
+func (this *server) saveHistoryFile() bool {
|
|
|
+ var sli []*historyFile
|
|
|
+ for _, v := range this.mapHistoryFile {
|
|
|
+ sli = append(sli, v)
|
|
|
+ }
|
|
|
+ err := common.SaveFileMarshal(this.fileHistory, sli)
|
|
|
+ if nil != err {
|
|
|
+ common.Log.Info(err)
|
|
|
return false
|
|
|
}
|
|
|
- this.dirCommand = dirCommand
|
|
|
- this.dirHistory = dirHistory
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
+func (this *server) isUpdate(info os.FileInfo) bool {
|
|
|
+ v := this.mapHistoryFile[info.Name()]
|
|
|
+ if nil == v {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ if v.LoadTime < info.ModTime().Unix() {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
func (this *server) processDir() bool {
|
|
|
infos, err := ioutil.ReadDir(this.dirCommand)
|
|
|
if nil != err {
|
|
@@ -102,30 +146,32 @@ func (this *server) processDir() bool {
|
|
|
conf := common.GetConf()
|
|
|
host := fmt.Sprintf(`http://%s:%d`, conf.GetIp(), conf.GetPort())
|
|
|
for _, info := range infos {
|
|
|
+ if !this.isUpdate(info) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ hisFile := new(historyFile)
|
|
|
+ hisFile.setName(info.Name())
|
|
|
+ hisFile.setLoadTime(time.Now().Unix())
|
|
|
+ this.mapHistoryFile[info.Name()] = hisFile
|
|
|
+
|
|
|
filePath := path.Join(this.dirCommand, info.Name())
|
|
|
file := new(fileData)
|
|
|
- sliByte, err := ioutil.ReadFile(filePath)
|
|
|
+ err = common.LoadFileUnmarshal(filePath, file)
|
|
|
if nil != err {
|
|
|
this.logs = append(this.logs, fmt.Sprintf("load command file:%v", err))
|
|
|
return false
|
|
|
}
|
|
|
- err = json.Unmarshal(sliByte, file)
|
|
|
- if nil != err {
|
|
|
- this.logs = append(this.logs, fmt.Sprintf("unmarshal command file:%v", err))
|
|
|
- return false
|
|
|
- }
|
|
|
|
|
|
for _, command := range file.Commands {
|
|
|
flag := command.call(host)
|
|
|
this.logs = append(this.logs, command.getLog())
|
|
|
if !flag {
|
|
|
- return false
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
- newFileName := info.Name() + "_" + strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
- newFilePath := path.Join(this.dirHistory, newFileName)
|
|
|
- os.Rename(filePath, newFilePath)
|
|
|
}
|
|
|
+ this.saveHistoryFile()
|
|
|
return true
|
|
|
}
|
|
|
|