trigger.go 3.6 KB
Newer Older
D
dangyifei 已提交
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package transfer

import (
	"encoding/json"
	"fmt"
	"github.com/Badangel/logex"
	"io/ioutil"
	"strconv"
	"strings"
	"time"
	"transfer/dict"
)

func TriggerStart(addr string) (version dict.DictVersionInfo, err error) {
	return GetDoneFileInfo(addr)
}

func GetDoneFileInfo(addr string) (version dict.DictVersionInfo, err error) {
	fmt.Printf("[entry]start trigger\n")
	logex.Noticef("[entry]start trigger")
	//if donefile is in ftp, first download to local
	if strings.HasPrefix(addr, dict.FTP_HEADER) || strings.HasPrefix(addr, dict.HTTP_HEADER) {
		donefileAddr := Dict.TmpAddress + "/../donefile"
		Wget(addr, donefileAddr)
		addr = donefileAddr
	}

	baseDonefile := addr + "/base.txt"
	fmt.Printf("[trigrer]donefile path:%v \n", baseDonefile)
	logex.Noticef("[trigrer]base donefile path:%v", baseDonefile)
	contents, err := ioutil.ReadFile(baseDonefile)
	VersionLen := len(Dict.CurrentVersionInfo)
	version.DictName = Dict.DictName
	if err != nil {
		fmt.Printf("[trigrer]read files err:%v \n", err)
		logex.Fatalf("[trigrer]read files err:%v ", err)
		return
	} else {
		contentss := string(contents)
		lines := strings.Split(contentss, "\n")
		index := len(lines) - 2
		var donefileInfo dict.DonefileInfo
		fmt.Printf("line %v: %v\n", index, lines[index])
		if err = json.Unmarshal([]byte(lines[index]), &donefileInfo); err != nil {
			return
		}
		logex.Noticef("[trigrer]donfile info:%v", donefileInfo)
		newId, _ := strconv.Atoi(donefileInfo.Id)
		if VersionLen == 0 || newId > Dict.CurrentVersionInfo[VersionLen-1].Key {
			version.Id = newId
			version.Key, _ = strconv.Atoi(donefileInfo.Key)
			version.Input = donefileInfo.Input
			deployVersion := int(time.Now().Unix())
			version.CreateTime = deployVersion
			version.Version = deployVersion
			version.Depend = deployVersion
			version.Mode = dict.BASE
			return
		}
	}
	if Dict.DictMode == dict.BASR_DELTA && VersionLen > 0 {
		patchDonefile := addr + "/patch.txt"
		fmt.Printf("[trigrer]patchDonefile path:%v \n", patchDonefile)
		logex.Noticef("[trigrer]patch donefile path:%v", patchDonefile)
		contents, err = ioutil.ReadFile(patchDonefile)
		if err != nil {
			fmt.Printf("read files err:%v \n", err)
			return
		} else {
			contentss := string(contents)
			lines := strings.Split(contentss, "\n")
			for index := 0; index < len(lines)-1; index++ {
				var donefileInfo dict.DonefileInfo
				if err = json.Unmarshal([]byte(lines[index]), &donefileInfo); err != nil {
					return
				}
				logex.Noticef("[trigrer]donfile info:%v", donefileInfo)
				newId, _ := strconv.Atoi(donefileInfo.Id)
				newKey, _ := strconv.Atoi(donefileInfo.Key)
				if newId > Dict.CurrentVersionInfo[VersionLen-1].Id && newKey == Dict.CurrentVersionInfo[VersionLen-1].Key {
					version.Id = newId
					version.Key, _ = strconv.Atoi(donefileInfo.Key)
					version.Input = donefileInfo.Input
					deployVersion := int(time.Now().Unix())
					version.CreateTime = deployVersion
					version.Version = deployVersion
					version.Depend = Dict.CurrentVersionInfo[VersionLen-1].Depend
					version.Mode = dict.DELTA
					return
				}
			}
		}
	}
	return
}