houston 업데이트를 스크립트로 하자
This commit is contained in:
@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@ -21,8 +20,6 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/djherbis/times"
|
||||
|
||||
"github.com/Knetic/govaluate"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/metric"
|
||||
@ -46,83 +43,12 @@ func lastExecutionArgs(verpath string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
var errUploadZipLogFailed = errors.New("not ok")
|
||||
|
||||
func (hc *houstonClient) uploadLogFile(logFile string, name string, version string) error {
|
||||
file, err := os.Open(logFile)
|
||||
if err != nil {
|
||||
return err
|
||||
func (hc *houstonClient) uploadToAppendLog(logFile string, name string, version string) {
|
||||
hc.uploadChan <- uploadRequest{
|
||||
logFile: logFile,
|
||||
name: name,
|
||||
version: version,
|
||||
}
|
||||
|
||||
if file == nil {
|
||||
return errors.New("uploadLogFile failed : " + logFile)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
req, err := http.NewRequest("POST", hc.config.HttpAddress+"/upload", file)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
|
||||
hn, _ := os.Hostname()
|
||||
// createTime := file.
|
||||
req.Header.Set("Houston-Service-Name", name)
|
||||
req.Header.Set("Houston-Service-Version", version)
|
||||
req.Header.Set("Houston-Service-Filename", path.Base(logFile)+"."+hn+path.Ext(logFile))
|
||||
req.Header.Set("Content-Type", "application/zip")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errUploadZipLogFailed
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hc *houstonClient) uploadToAppendLog(logFile string, name string, version string) error {
|
||||
t, err := times.Stat(logFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.Open(logFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if file == nil {
|
||||
return errors.New("uploadRuploadLogFileawLogFile failed : " + logFile)
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
req, err := http.NewRequest("POST", hc.config.HttpAddress+"/upload", file)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
|
||||
hn, _ := os.Hostname()
|
||||
// createTime := file.
|
||||
req.Header.Set("Houston-Service-Name", name)
|
||||
req.Header.Set("Houston-Service-Version", version)
|
||||
req.Header.Set("Houston-Service-Filename", t.BirthTime().UTC().Format(time.DateOnly)+"."+hn+path.Ext(logFile))
|
||||
req.Header.Set("Content-Type", "application/zip")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errUploadZipLogFailed
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func findMatchFiles(storageRoot, name, version, filter string) (string, []string) {
|
||||
@ -377,8 +303,9 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
}()
|
||||
|
||||
total := 0
|
||||
var targetFile *os.File
|
||||
var currentFilePath string
|
||||
var logFile *os.File
|
||||
var logFilePath string
|
||||
var logFileTimestamp time.Time
|
||||
|
||||
ext := path.Ext(logfilePath)
|
||||
head := logfilePath[:len(logfilePath)-len(ext)]
|
||||
@ -391,12 +318,14 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
var metricBuffer []byte
|
||||
|
||||
wipeLogFile := func() {
|
||||
total = 0
|
||||
if targetFile != nil {
|
||||
targetFile.Close()
|
||||
targetFile = nil
|
||||
go hc.uploadToAppendLog(currentFilePath, meta.name, meta.version)
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
logFile = nil
|
||||
if total > 0 {
|
||||
hc.uploadToAppendLog(logFilePath, meta.name, meta.version)
|
||||
}
|
||||
}
|
||||
total = 0
|
||||
}
|
||||
|
||||
defer func() {
|
||||
@ -404,17 +333,15 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
metricExporter.Shutdown()
|
||||
}()
|
||||
|
||||
currentTime := time.Now().UTC()
|
||||
for {
|
||||
now := time.Now().UTC()
|
||||
if now.YearDay() != currentTime.YearDay() {
|
||||
if time.Since(logFileTimestamp) > time.Minute {
|
||||
wipeLogFile()
|
||||
}
|
||||
|
||||
if targetFile == nil {
|
||||
currentTime = time.Now().UTC()
|
||||
currentFilePath = head + currentTime.Format("2006-01-02.150405") + ext
|
||||
targetFile, _ = os.OpenFile(currentFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if logFile == nil {
|
||||
logFileTimestamp = time.Now()
|
||||
logFilePath = head + logFileTimestamp.UTC().Format("2006-01-02.150405") + ext
|
||||
logFile, _ = os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
}
|
||||
|
||||
buff, err := reader.ReadBytes('\n')
|
||||
@ -460,9 +387,9 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
|
||||
metricBuffer = metricBuffer[:0]
|
||||
}
|
||||
} else if targetFile != nil && len(buff) > 0 {
|
||||
} else if logFile != nil && len(buff) > 0 {
|
||||
for written := 0; written < len(buff); {
|
||||
n, err := targetFile.Write(buff[written:])
|
||||
n, err := logFile.Write(buff[written:])
|
||||
if err != nil {
|
||||
logger.Println("write log file failed :", logfilePath, err)
|
||||
break
|
||||
@ -471,10 +398,6 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
}
|
||||
}
|
||||
total += len(buff)
|
||||
|
||||
if total > 1024*1024 {
|
||||
wipeLogFile()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -489,40 +412,38 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
defer r.Close()
|
||||
|
||||
total := 0
|
||||
var logFile *os.File
|
||||
var logFilePath string
|
||||
var logFileTimestamp time.Time
|
||||
|
||||
var targetFile *os.File
|
||||
var currentFilePath string
|
||||
ext := path.Ext(logfilePath)
|
||||
head := logfilePath[:len(logfilePath)-len(ext)]
|
||||
if len(head) > 0 {
|
||||
if len(head) > 0 && !strings.HasSuffix(head, "/") {
|
||||
head += "."
|
||||
}
|
||||
reader := bufio.NewReader(r)
|
||||
|
||||
wipeLogFile := func() {
|
||||
total = 0
|
||||
if targetFile != nil {
|
||||
targetFile.Close()
|
||||
targetFile = nil
|
||||
go hc.uploadToAppendLog(currentFilePath, meta.name, meta.version)
|
||||
if logFile != nil {
|
||||
logFile.Close()
|
||||
logFile = nil
|
||||
if total > 0 {
|
||||
hc.uploadToAppendLog(logFilePath, meta.name, meta.version)
|
||||
}
|
||||
}
|
||||
total = 0
|
||||
}
|
||||
defer wipeLogFile()
|
||||
|
||||
defer func() {
|
||||
wipeLogFile()
|
||||
}()
|
||||
|
||||
currentTime := time.Now().UTC()
|
||||
for {
|
||||
now := time.Now().UTC()
|
||||
if now.YearDay() != currentTime.YearDay() {
|
||||
if time.Since(logFileTimestamp) > time.Minute {
|
||||
wipeLogFile()
|
||||
}
|
||||
|
||||
if targetFile == nil {
|
||||
currentTime = time.Now().UTC()
|
||||
currentFilePath = head + currentTime.Format("2006-01-02.150405") + ext
|
||||
targetFile, _ = os.OpenFile(currentFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if logFile == nil {
|
||||
logFileTimestamp = time.Now()
|
||||
logFilePath = head + logFileTimestamp.UTC().Format("2006-01-02.150405") + ext
|
||||
logFile, _ = os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
||||
}
|
||||
|
||||
buff, errRead := reader.ReadBytes('\n')
|
||||
@ -531,9 +452,9 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
break
|
||||
}
|
||||
|
||||
if targetFile != nil && len(buff) > 0 {
|
||||
if logFile != nil && len(buff) > 0 {
|
||||
for written := 0; written < len(buff); {
|
||||
n, err := targetFile.Write(buff[written:])
|
||||
n, err := logFile.Write(buff[written:])
|
||||
if err != nil {
|
||||
logger.Println("write log file failed :", logfilePath, err)
|
||||
break
|
||||
@ -542,10 +463,6 @@ func (hc *houstonClient) launch(meta *procmeta) error {
|
||||
}
|
||||
}
|
||||
total += len(buff)
|
||||
|
||||
if total > 1024*1024 {
|
||||
wipeLogFile()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -717,42 +634,3 @@ func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest,
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hc *houstonClient) uploadProcFiles(child *procmeta, filter string, deleteAfterUpload bool) {
|
||||
logger.Println("uploadFiles found :", child.version, child.name)
|
||||
_, matches := findMatchFiles(hc.config.StorageRoot, child.name, child.version, filter)
|
||||
go func() {
|
||||
for _, filename := range matches {
|
||||
if err := hc.uploadLogFile(filename, child.name, child.version); err != nil {
|
||||
break
|
||||
}
|
||||
if deleteAfterUpload {
|
||||
os.Remove(filename)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (hc *houstonClient) uploadFiles(req *shared.UploadRequest) error {
|
||||
logger.Println("uploadFiles req :", *req)
|
||||
for _, child := range hc.childProcs {
|
||||
if child.version == req.Version && child.name == req.Name {
|
||||
hc.uploadProcFiles(child, req.Filter, false)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 실행 중이 아닌 폴더에서도 대상을 찾는다
|
||||
// 전체 파일을 대상으로
|
||||
_, matches := findMatchFiles(hc.config.StorageRoot, req.Name, req.Version, req.Filter)
|
||||
go func() {
|
||||
for _, filename := range matches {
|
||||
if err := hc.uploadLogFile(filename, req.Name, req.Version); err != nil {
|
||||
break
|
||||
}
|
||||
os.Remove(filename)
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user