mirror of
https://gitee.com/rainbond/Rainbond.git
synced 2024-12-03 04:07:51 +08:00
[REV] update code for read build log
This commit is contained in:
parent
79991420fb
commit
74bc0f897b
@ -27,11 +27,14 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/goodrain/rainbond/builder/sources"
|
"github.com/goodrain/rainbond/builder/sources"
|
||||||
"github.com/goodrain/rainbond/event"
|
"github.com/goodrain/rainbond/event"
|
||||||
"github.com/goodrain/rainbond/util"
|
"github.com/goodrain/rainbond/util"
|
||||||
|
"github.com/pquerna/ffjson/ffjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
func slugBuilder() (Build, error) {
|
func slugBuilder() (Build, error) {
|
||||||
@ -73,15 +76,74 @@ func (s *slugBuild) Build(re *Request) (*Response, error) {
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *slugBuild) readLog(stderr io.Reader, logger event.Logger, closed chan struct{}) {
|
func (s *slugBuild) readLogFile(logfile string, logger event.Logger, closed chan struct{}) {
|
||||||
readerr := bufio.NewReader(stderr)
|
file, _ := os.Open(logfile)
|
||||||
|
watcher, _ := fsnotify.NewWatcher()
|
||||||
|
defer watcher.Close()
|
||||||
|
_ = watcher.Add(logfile)
|
||||||
|
readerr := bufio.NewReader(file)
|
||||||
for {
|
for {
|
||||||
line, _, err := readerr.ReadLine()
|
line, _, err := readerr.ReadLine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
logrus.Errorf("Read build container log error:%s", err.Error())
|
logrus.Errorf("Read build container log error:%s", err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
wait := func() error {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-closed:
|
||||||
|
return nil
|
||||||
|
case event := <-watcher.Events:
|
||||||
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case err := <-watcher.Errors:
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := wait(); err != nil {
|
||||||
|
logrus.Errorf("Read build container log error:%s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if logger != nil {
|
||||||
|
var message = make(map[string]string)
|
||||||
|
if err := ffjson.Unmarshal(line, &message); err == nil {
|
||||||
|
if m, ok := message["log"]; ok {
|
||||||
|
logger.Info(m, map[string]string{"step": "build-exector"})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-closed:
|
||||||
return
|
return
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *slugBuild) readLog(stderr io.Reader, logger event.Logger, closed chan struct{}) {
|
||||||
|
readerr := bufio.NewReader(stderr)
|
||||||
|
sleep := time.NewTimer(time.Second * 3)
|
||||||
|
defer sleep.Stop()
|
||||||
|
for {
|
||||||
|
line, _, err := readerr.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
if err != io.EOF {
|
||||||
|
logrus.Errorf("Read build container log error:%s", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
sleep.Reset(time.Second * 3)
|
||||||
|
select {
|
||||||
|
case <-closed:
|
||||||
|
return
|
||||||
|
case <-sleep.C:
|
||||||
|
continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if logger != nil {
|
if logger != nil {
|
||||||
lineStr := string(line)
|
lineStr := string(line)
|
||||||
@ -172,7 +234,7 @@ func (s *slugBuild) runBuildContainer(re *Request) error {
|
|||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
reader.Close()
|
reader.Close()
|
||||||
//os.Remove(reader.Name())
|
os.Remove(reader.Name())
|
||||||
}()
|
}()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
@ -185,6 +247,12 @@ func (s *slugBuild) runBuildContainer(re *Request) error {
|
|||||||
closed := make(chan struct{})
|
closed := make(chan struct{})
|
||||||
defer close(closed)
|
defer close(closed)
|
||||||
errchan := make(chan error, 1)
|
errchan := make(chan error, 1)
|
||||||
|
// containerLog, err := containerService.GetContainerLogPath(containerID)
|
||||||
|
// if err == nil && containerLog != "" {
|
||||||
|
// go s.readLogFile(containerLog, re.Logger, closed)
|
||||||
|
// } else {
|
||||||
|
go s.readLog(buffer, re.Logger, closed)
|
||||||
|
// }
|
||||||
close, err := containerService.AttachContainer(containerID, true, true, true, reader, buffer, buffer, &errchan)
|
close, err := containerService.AttachContainer(containerID, true, true, true, reader, buffer, buffer, &errchan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
containerService.RemoveContainer(containerID)
|
containerService.RemoveContainer(containerID)
|
||||||
@ -197,7 +265,6 @@ func (s *slugBuild) runBuildContainer(re *Request) error {
|
|||||||
containerService.RemoveContainer(containerID)
|
containerService.RemoveContainer(containerID)
|
||||||
return fmt.Errorf("start builder container error:%s", err.Error())
|
return fmt.Errorf("start builder container error:%s", err.Error())
|
||||||
}
|
}
|
||||||
go s.readLog(buffer, re.Logger, closed)
|
|
||||||
if err := <-errchan; err != nil {
|
if err := <-errchan; err != nil {
|
||||||
logrus.Debugf("Error hijack: %s", err)
|
logrus.Debugf("Error hijack: %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user